《Java基础入门》教材第4章课后作业

题目1(P144 编程题)

1 题目描述

某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下。

(1) Employee :这是所有员工的父类。

①属性:员工的姓名、员工的生日月份。

方法:getSalary ( int month )根据参数月份确定工资。如果该月员工过生日,则公司会额外发放100元。

(2) SalariedEmployee : Employee 的子类,拿固定工资的员工。属性:月薪。

(3) HourlyEmployee : Employee 的子类,按小时拿工资的员工,每月工作超出160h的部分按照1.5倍工资发放。

属性:每小时的工资、每月工作的小时数。

(4) SalesEmployee : Employee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率。

(5) BasePlusSalesEmployee : SalesEmployee 的子类,有固定底薪的销售人员,工资为底薪加上销售提成。

属性:底薪。

本题要求根据上述员工分类,编写一个程序,实现以下功能;

(1)创建一个 Employee 数组,分别创建若十不同的 Employee 对象,并打印某个月的工资。

(2)每个类都完全封装,不允许有非私有化属性。

2运行界面截图与说明

3 代码

class Employee{
    private String name;
    private int birthdaymonth;
    private double salary;
    void em(String name,int birthdaymonth,int salary){
        this.name=name;
        this.birthdaymonth=birthdaymonth;
        this.salary=salary;
    }
    double getSalary(int month){
        if(month==birthdaymonth) {
            System.out.println("员工"+name+"本月生日,工资额外加100");
            return salary+100;
        }
        return salary;
    };
    String getName()
    {return name;}
    int getBirthdaymonth()
    {return birthdaymonth;}
 }

 class SalariedEmployee extends Employee{
    private double monthlysalary;
    public double getsalary(int month){
        return monthlysalary=super.getSalary(month);
    }
 }
 class HourlyEmployee extends Employee{
    private double hourlysalary=25;
    private int monthlyworktime;
    public double getSalary(int month){
        if(monthlyworktime<160){
            return monthlyworktime*hourlysalary+super.getSalary(month);
        }else {
            return (1.5*monthlyworktime-80)*hourlysalary+super.getSalary(month);
        }
    }
    public void em(String name,int birthdaymonth,int salary,int monthlyworktime){
         super.em(name,birthdaymonth,salary);
         this.monthlyworktime=monthlyworktime;
    }
 }
 class SalesEmployee extends Employee{
    private int sale;
    private double ticheng=0.7;
    public double getSalary(int month){
         return sale*ticheng+super.getSalary(month);
    }
     public void em(String name,int birthdaymonth,int salary,int sale){
         super.em(name,birthdaymonth,salary);
         this.sale=sale;
     }
 }
 class BasePlusSalesEmployee extends  SalesEmployee{
    private double basesalary;
     public double getSalary(int month){
         return basesalary+super.getSalary(month);
     }
     public void em(String name,int birthdaymonth,int salary,int sale,double basesalary){
         super.em(name,birthdaymonth,salary,sale);
         this.basesalary=basesalary;
     }
 }

public class p144 {
    public static void main(String[] args) {
        int  month=4;
        Employee employees[];
        employees=new Employee[4];
        SalariedEmployee salaried=new SalariedEmployee();
        HourlyEmployee he=new HourlyEmployee();
        SalesEmployee sales=new SalesEmployee();
        BasePlusSalesEmployee be=new BasePlusSalesEmployee();
        salaried.em("高珊珊",1,2000);
        he.em("艾莉",2,0,180);
        sales.em("洪世贤",3,0,8500);
        be.em("林品如",4,0,6000,2000);

        employees[0]=salaried;
        employees[1]=he;
        employees[2]=sales;
        employees[3]=be;

        System.out.println("本月:"+month+"月");
        System.out.println("固定工资员工:"+employees[0].getName()+",该员工生日月份为:"+employees[0].getBirthdaymonth()+",该月工资为:"+employees[0].getSalary(month));
        System.out.println("时薪工资员工:"+employees[1].getName()+",该员工生日月份为:"+employees[1].getBirthdaymonth()+",该月工资为:"+""+employees[1].getSalary(month));
        System.out.println("销售员工:"+employees[2].getName()+",该员工生日月份为:"+employees[2].getBirthdaymonth()+",该月工资为:"+""+employees[2].getSalary(month));
        System.out.println("固定底薪的销售员工:"+employees[3].getName()+",该员工生日月份为:"+employees[3].getBirthdaymonth()+",该月工资为:"+""+employees[3].getSalary(month));
    }
}

 

题目2

1 题目描述

定义一个接口,其中包含一个 display ( )方法用于显示信息。现在通知类、汽车类、广告类均要实现该接口,以输出显示“通知内容”、“汽车油量”、“广告消息”。试编程实现并编写主程序。

2 运行界面截图与说明

3 代码 

interface jiekou
{
    public void display();
}

class notification implements jiekou {
    public void display() {
        System.out.println("通知内容");
    }
}
class car implements jiekou{
    public void display(){
        System.out.println("汽车油量");
    }
}
class advertisement implements jiekou{
    public void display(){
        System.out.println("广告消息");
    }
}

public class work3 {
    public static void main(String[] args) {
        notification n=new notification();
        car c=new car();
        advertisement a=new advertisement();
        n.display();
        c.display();
        a.display();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值