实验练习 Employee层次结构
程序模板是一个完整的、可实际运行的JAVA程序,其中关键的1行或者多行代码已经替换为注释。请先阅读问题描述,分析示例输出;然后研究模板的代码。参考问题解答提示,用JAVA代码替换注释。编译并执行程序,并将输出结果与提供的示例输出进行比较。然后回答后续问题。
1、 实验目的
l 建立一个类层次结构
l 创建正确调用超类构造函数的子类构造函数
l 使用类层次结构中的类
l 将某个类添加到现有的类层次结构中
2、 问题描述
创建类图中的各个类。Employee类应有姓、名和社会保险号。除以上这些外,SalariedEmployee类还应有周薪;HourlyEmployee类还应有每小时工资和工作时数;
CommissionEmployee还应有佣金率和销售总额。每个类都应有合适的构造函数、设置方法和读取方法。编写一个程序,实例化这些类的对象,并输出与每个对象相关的信息(包括所继承的信息。)
3、 示例输出
4、 程序模板
// Lab 1: 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
// Lab 1: 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
// Lab 1: 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
// Lab 1: 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
// Lab 1: EmployeeTest.java
// 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( "0.00" );
// 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
5、问题解答提示
(1) 每个扩展Employee的类的构造函数都应调用Employee构造函数,并将姓、名和社会保险号传递给它。
(2) 每个扩展Employee的类的toString方法都应调用Employee类的toString方法,以获取姓、名和社会保险号的String表示,然后在返回的String中添加额外的信息。
(3) 练习中有任何问题请及时询问老师。
6、强化练习
(1)改进实验练习1的类层次结构,使其包含BasePlusCommissionEmployee类。BasePlusCommissionEmployee类与CommissionEmployee类具有相同的特征,但是还包括每星期的基本工资。该类的构造函数应调用CommissionEmployee类的构造函数;而该类的toString方法应调用CommissionEmployee类的toString方法。在创建BasePlusCommissionEmployee类后,改进实验1的程序,以实例化这个新类的一个对象,并输出该对象的信息(包括它所继承的信息)。输出结果应如下所示。
下面是我自己写的代码,基本能够实现实验一的内容, Employee1.0
Employee类是下面所有******Employee类的父类
package com.whj.exam;
public class Employee {
private String thefirstname;
private String thelastname;
private String socialnumber;
public Employee(String thefirstname, String thelastname, String socialnumber)
{
//super();继承Employee的父类的构造方法,Employee的父类是object;
this.thefirstname = thefirstname;
this.thelastname = thelastname;
this.socialnumber = socialnumber;
}
public String getThefirstname() {
return thefirstname;
}
public void setThefirstname(String thefirstname) {
this.thefirstname = thefirstname;
}
public String getThelastname() {
return thelastname;
}
public void setThelastname(String thelastname) {
this.thelastname = thelastname;
}
public String getSocialnumber() {
return socialnumber;
}
public void setSocialnumber(String socialnumber) {
this.socialnumber = socialnumber;
}
@Override
public String toString() {
return getClass().getName()+":"+getThefirstname()+getThelastname()+"\n"+"social security number:"+getSocialnumber();
}
}
测试类,包含主方法
package com.whj.exam;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
Employee employee[]=new Employee[3];
employee[0]=new SalariedEmployee("John", "Smith", "111-11-1111","$800.00");
employee[1]=new CommissionEmployee("Sue", "Jones", "222-22-2222","$10000",".06");
employee[2]=new HourlyEmployee("Karen", "Price","444-44-4444","$16.75","40");
String output;
output=employee[0].toString()+"\n"+employee[1].toString()+"\n"+employee[2].toString();
JOptionPane.showMessageDialog( null, output );
System.exit( 0 );
}
}
SalariedEmployee类,是Employee的继承子类,其中多了每周的薪金。
package com.whj.exam;
public class SalariedEmployee extends Employee {
private String salary;
public SalariedEmployee(String thefirstname, String thelastname,
String socialnumber, String salary) {
super(thefirstname, thelastname, socialnumber);
this.salary = salary;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
@Override
public String toString()
{
return super.toString()+"\n"+"weekly salary:"+getSalary()+"\n";
}
}
CommissionEmployee类,是Employee的子类,其中多了佣金率和销售总额。
package com.whj.exam;
public class CommissionEmployee extends Employee{
private String grosssales;
private String commissionrate;
public CommissionEmployee(String thefirstname, String thelastname,
String socialnumber, String grosssales, String commissionrate) {
super(thefirstname, thelastname, socialnumber);
this.grosssales = grosssales;
this.commissionrate = commissionrate;
}
public String getGrosssales() {
return grosssales;
}
public void setGrosssales(String grosssales) {
this.grosssales = grosssales;
}
public String getCommissionrate() {
return commissionrate;
}
public void setCommissionrate(String commissionrate) {
this.commissionrate = commissionrate;
}
@Override
public String toString() {
return super.toString()+"\n"+"gross sales:"+getGrosssales()+"\n"
+"a commission rate:"+getCommissionrate()+"\n";
}
}
HourlyEmployee类,是Employee的子类,其中添加了每小时工资和工作时数。
package com.whj.exam;
public class HourlyEmployee extends Employee{
private String hourlysalary;
private String workhour;
public String getHourlysalary() {
return hourlysalary;
}
public void setHourlysalary(String hourlysalary) {
this.hourlysalary = hourlysalary;
}
public String getWorkhour() {
return workhour;
}
public void setWorkhour(String workhour) {
this.workhour = workhour;
}
public HourlyEmployee(String thefirstname, String thelastname,
String socialnumber, String hourlysalary, String workhour) {
super(thefirstname, thelastname, socialnumber);
this.hourlysalary = hourlysalary;
this.workhour = workhour;
}
@Override
public String toString() {
return super.toString()+"\n"+"hourly salary:"+getHourlysalary()+"\n"
+"hours worked:"+getWorkhour()+"\n";
}
}
通过老师给的实验对java中继承的初步运用,其中的一些细节还是有点把握不清,还有实验一扩展练习没做。(employee 2.0)