Java第二次实验第一题
一. 实验目的
建立类的层次结构。
二.实验要求
创建如下图所示的类(使用类层次结构中的类)
Employee ( 姓名,性别,保险号、工作年限) 构造函数——一组set方法,一组get方法; 从键盘输入属性的方法; 屏幕输出属性的方法;
SalariedEmployee ( 每月绩效 、月薪、工作年限、年终奖金) 构造函数 ——一组set方法,一组get方法; 从键盘输入属性的方法; 计算年休假天数; 计算年终奖金(根据每月绩效,月薪和工作年限); 屏幕输出属性的方法
CommissionEmployee ( 佣金率、销售总额、年终奖金、每月销售额) 构造函数—— 一组set方法,一组get方法; 从键盘输入属性的方法; 计算年休假天数(根据年销售总额); 计算年终奖金(根据每月的月销售额和销售总额); 屏幕输出属性的方法;
HourlyEmployee ( 每小时的工资、工作小时数) 构造函数—— 一组set方法,一组get方法 屏幕输出属性的方法
BaseplusEmployee ( 每周的基本工资数、佣金率、销售总额、年终奖金、每月销售额) 构造函数 —— 一组set方法,一组get方法; 从键盘输入属性的方法; 计算年休假天数(根据年销售总额); 计算年终奖金(根据每月的月销售额和销售总额); 屏幕输出属性的方法。
三.代码展示
1. 结构层次
2. empoyee.java
代码如下(示例):
package employee;
import java.util.*;
/*
* Employee :
姓名,性别,保险号、工作年限
构造函数
一组set方法,一组get方法
从键盘输入属性的方法
屏幕输出属性的方法
*/
public class employee {
String name;// 姓名
String sex;//性别
double InsuranceID;//保险号
int WorkingYear;//工作年限
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public double getInsuranceID() {
return InsuranceID;
}
public void setInsuranceID(double insuranceID) {
InsuranceID = insuranceID;
}
public int getWorkingYear() {
return WorkingYear;
}
public void setWorkingYear(int workingYear) {
WorkingYear = workingYear;
}
/*
* 从键盘输入属性的方法
*/
public void input(){
Scanner in = new Scanner(System.in);
System.out.println("请输入姓名:");
setName(in.nextLine());
System.out.println("请输入性别:");
setSex(in.nextLine());
System.out.println("请输入保险号:");
setInsuranceID(in.nextDouble());
System.out.println("请输入工作年限:");
setWorkingYear(in.nextInt());
in.close();
}
public employee(String name, String sex, double insuranceID, int workingYear) {
super();
this.name = name;
this.sex = sex;
InsuranceID = insuranceID;
WorkingYear = workingYear;
}
public employee() {
super();
this.name = "丈夫";
this.sex = "nan1";
InsuranceID = 195184;
WorkingYear = 15;
}
/*
* 屏幕输出属性的方法
*/
public void output(){
System.out.println("姓名: "+getName());
System.out.println("性别: "+getSex());
System.out.println("保险号: "+getInsuranceID());
System.out.println("工作年限: "+getWorkingYear());
}
}
3.SalariedEmployee.java
代码如下(示例):
package employee;
import java.util.Scanner;
/*
* SalariedEmployee
每月绩效 月薪,工作年限,年终奖金
构造函数
一组set方法,一组get方法
从键盘输入属性的方法
计算年休假天数
计算年终奖金(根据每月绩效,月薪和工作年限)
屏幕输出属性的方法
*/
public class SalariedEmployee extends employee {
int performance;//每月绩效 五分制绩效
//5分超常, 4分达标, 3分可接受 , 2分需改进 , 1-0分不良
double salary;//月薪
double annualBonus;//年终奖
public int getPerformance() {
return performance;
}
public void setPerformance(int performance) {
this.performance = performance;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getAnnualBonus() {
return annualBonus;
}
public void setAnnualBonus(double annualBonus) {
this.annualBonus = annualBonus;
}
/*
* 从键盘输入属性的方法
*/
public void input1()
{
// super.input();//super调用父类
Scanner in = new Scanner(System.in);
System.out.println("请输入姓名:");
setName(in.nextLine());
System.out.println("请输入性别:");
setSex(in.nextLine());
System.out.println("请输入保险号:");
setInsuranceID(in.nextDouble());
System.out.println("请输入工作年限:");
setWorkingYear(in.nextInt());
System.out.println("输入每月绩效(5分制):");
setPerformance(in.nextInt());
System.out.println("输入月薪:");
setSalary(in.nextDouble());
in.close();
}
/*
* 屏幕输出属性的方法
*/
public void output1()
{
super.output();
System.out.println("每月绩效: "+getPerformance() );
System.out.println("月薪:"+getSalary());
Bonus();
System.out.println("年终奖: "+getAnnualBonus());
Holidays();
}
/*
* 计算年终奖金(根据每月绩效,月薪和工作年限)
*/
public void Bonus()
{
if(WorkingYear<5)
setAnnualBonus( salary*WorkingYear*performance) ;
else
setAnnualBonus(salary*performance*5);
}
/*
* 计算年休假天数
*/
public void Holidays()
{
if(WorkingYear>=1 && WorkingYear<10)
System.out.println("年假休5天");
else if(WorkingYear>=10 && WorkingYear<20)
System.out.println("年假休10天");
else
System.out.println("年假休20天");
}
public SalariedEmployee() {
super();
this.performance =5;
this.salary = 3000;
this.annualBonus = 0;
}
public SalariedEmployee(String name, String sex, double insuranceID, int workingYear, int performance,
double salary, double annualBonus) {
super(name, sex, insuranceID, workingYear