// Employee hierarchy test program. ============================= import java.text.DecimalFormat; import javax.swing.JOptionPane;
public class EmployeeTest {
public static void main( String[] args ) {
DecimalFormat twoDigits = new DecimalFormat( \
// Create employees
/* Create SalariedEmployee John Smith with social security number 111-11-1111 and weekly salary $800.00. */
/* Create SalariedEmployee Sue Jones with social security number
222-22-2222 with gross sales of $10000 and a commission rate of .06. */ /* Create SalariedEmployee Karen Price with social security number 444-44-4444 an hourly salary of $16.75 and 40 hours worked. */
// output each employee
/* Create a String called output and assign it the String representation of the three employee objects separated by newlines. */
JOptionPane.showMessageDialog( null, output ); // display output System.exit( 0 );
} // end main
} // end class EmployeeTest
===================== //Employee.java
// Employee superclass.
=====================
public class Employee {
/* Declare instance variables for the first name, last name and social security number. */
// constructor
/* Declare a constructor with three parameters that are used to initialize the first name, last name and social security number. */
// set methods
/* Create set methods for every instance variable. */
// get methods
/* Create get methods for every instance variable. */
// return String representation of Employee object
/* Create a toString method that returns a String containing the first name and last name separated by a space. Then, append a newline and the social security number. */
} // end class Employee
=========================================
// CommissionEmployee.java
// CommissionEmployee class derived from Employee. ==========================================
/* Write a class header in which class CommissionEmployee inherits from class Employee */
/* Declare instance variables for gross sales and commission rate. */
// constructor
/* Declare a constructor that initializes all the data for a CommissionEmployee, including the data originally defined in class Employee. */
// set methods
/* Create set methods for every instance variable. */
// get methods
/* Create get methods for every instance variable. */
// return String representation of CommissionEmployee object /* Create a toString method that outputs the complete information for a CommissionEmployee. */
} // end class CommissionEmployee
==================================== // HourlyEmployee.java
// HourlyEmployee class derived from Employee. ====================================
/* Write a class header in which class HourlyEmployee inherits from class Employee */
/* Declare instance variables for wages and hours worked. */
// constructor
/* Declare a constructor that initializes all the data for a HourlyEmployee, including the data originally defined in class Employee. */
// set methods
/* Create set methods for every instance variable. */
// get methods
/* Create get methods for every instance variable. */
// return String representation of HourlyEmployee object
/* Create a toString method that outputs the complete information for a HourlyEmployee. */
} // end class HourlyEmployee
==================================== //SalariedEmployee.java
// SalariedEmployee class derived from Employee. ====================================
/* Write a class header in which class SalariedEmployee
inherits from class Employee */
/* Declare an instance variable for the weekly salary. */
// constructor
/* Declare a constructor that initializes all the data for a SalariedEmployee, including the data originally defined in class Employee. */
// set methods
/* Create a set method for the instance variable. */
// get methods
/* Create a get method for the instance variable. */
// return String representation of SalariedEmployee object
/* Create a toString method that outputs the complete information for a SalariedEmployeex. */
} // end class SalariedEmployee
第4章 Java系统类库和常用数据结构
一. 概念复习和巩固(请在课后和上机前完成下面的练习)
1.定义字符串s:String s=”Micrsoft公司”; 执行下面的语句,c的值为(B)。 char c=s.charAt(9);
A) 产生数组下标越界异常。B) 司 C) null D) 公 2.以下语句执行后输出的结果是(B)。 String s0 = new String( \System.out.println(s0.length() );
A)10 B)11 C) 9 D)15
3.应用程序的main方法中有以下语句,则输出的结果是 ( A )。 String s1=new String(\String s2=new String(\ boolean b1=s1.equals(s2);
boolean b2=(s1==s2);
System.out.print(b1+\ \
A)true false B) false true C) true true D)false false 4.下面的程序段输出的结果是(A)。 String s1=\
System.out.println( s1.equalsIgnoreCase( s2 ));
A)true B)false C) Java D)java 5.下面的程序段输出的结果是(A)。 String s1=\
System.out.println( s1.compareTo( s2 ));
A)-1 B)1 C) false D)true 6.下面的程序段执行完后,cont的值是多少(C)?
String strings[]={ \int cont=0;
for ( int i = 0; i < strings.length; i++ ) if ( strings[ i ].endsWith( \ cont++;
A)1 B)2 C) 3 D)4
7.定义字符串:String str = \则str.indexOf( 'd' ) 的结果是(C)。 A)’d’ B)true C) 3 D)4 8.下面的程序段输出的结果是(A)。 StringBuffer buf1;
String str=\北京2008\buf1=new StringBuffer(str);
System.out.println(buf1.charAt(2));
A)2 B) 京 C) 0 D)null 9.下面的程序段输出的结果是(C)。 StringBuffer buf1;
String str=\buf1=new StringBuffer(str); buf1.setCharAt(0,'6');
System.out.println( buf1.toString() );
A) 007bei0jing B) 007beijing C) 607beijing D) 667beijing 10. 若有如下赋值语句:
x = new StringBuffer().append(\x的类型是_______;它的值是_______。 答案:String; a4c。
11.下面的程序段希望把字符串str中的类名、对象名和方法名分离出来单独显示,因此创建了一个字符串分析器对象stoken,请把stoken的分隔符填写在程序中的空中,程序中定义的变量k的值是__(1)___。
String str=\StringTokenizer stoken=new StringTokenizer(str,___ (2)___); int k=stoken.countTokens(); 答案:(1)7 (2)\
12.应用程序的main方法中有以下语句,则输出的结果是 (B)。 Hashtable hashtable=new Hashtable(); hashtable.put(\hashtable.put(\hashtable.put(\
System.out.println(hashtable.get(\
A) aaa B) bbb C) 200 D) 3
二. 本章上机实验
上机实验七
1.编写应用程序,在其main方法中使用JOptionPane类的静态方法showInputDialog(String s)创建一个文本输入对话框,输入一个文件名,判断该文件是java源文件、字节码文件还是其他文件,并将判断结果显示在命令行界面。 2.编写应用程序,在其main方法中使用JOptionPane类的静态方法showInputDialog(String s)创建一个文本输入对话框,输入用两个@分隔的字符串,通过substring()方法得到@分隔的3个子串,按照图示将结果输出在命令行界面。
3.把以下字符串str中有4个字母的单词显示在命令行(每行输出一个):
String str =\
+\ +\
4.查阅Java API文档,了解java.text.SimpleDateFormat类,使用该类及相关方法,将系统当前时间格式化为图示的格式输出。
5.编写命令行界面的猜数游戏:猜数范围由命令行输入,将猜数的结果输出并提示,直到猜出正确的结果。
6.编写应用程序,用1到6之间的随机数来模拟掷骰子游戏,统计扔5000次后出现骰子各点的次数并输出。
上机实验八
1.编写应用程序,在其main方法中定义以下两个字符串数组:
String mpgStr[]={\String pngStr[]={\
然后将他们的每个元素间隔添加到ArrayList对象中,并在命令行界面显示所有元素;然后将ArrayList对象中所有.png的元素都删除,再显示ArrayList对象中的所有元素,如图所示。
2.编写应用程序,程序运行后生成100组36选7的数组,并统计1~36中的各个数字出现的次数。
3.编写应用程序,按照图示的输出结果完成程序:在main方法中创建ArrayList对象list,并添加15个Integer对象后输出;对列表中的元素随机排序后输出;对列表中的元素降序排序后输出;对列表中的元素升序排序后输出;查找列表中是否由元素12和-9,并分别输出查找结果。