第九次java作业

发布时间 2023-06-15 20:47:04作者: 崔huaqian

3、设计三个类,分别如下:(知识点:抽象类及抽象方法)[必做题]

•3.1设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。

•3.2设计2个子类:

•3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。

•3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

•3.3在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

 

package lll;

public abstract  class lll {
    double area;

double per;

char color;

public Shape() {

super();

}

public Shape(char color) {

super();

this.color=color;

}

public abstract double getPer();

public abstract double  getArea();

public abstract void showAll();

public char getColor() {

return color;

}
}
package zuoye;

public class Rectangle extends Shape{

double length;

double width;

public double getPer() {

return(length+width)*2;

}

public double getArea() {

return length*width;

}

public Rectangle() {

super();

}

public Rectangle(double length,double width,char color) {

super();

this.length=length;

this.width=width;

this.color=color;

}

public void showAll() {

System.out.println("矩形的长是"+length+"宽是"+width+"颜色是"+color+"周长是"+getPer()+"面积是"+getArea());

}
}
package zuoye;

public class Circle extends Shape{

double radius;

public double getPer() {

return 3.14*2*radius;

}

public double getArea() {

return 3.14*radius*radius;

}

public Circle(double radius,char color) {

super();

this.radius=radius;

this.color=color;

}

@Override

public void showAll() {

System.out.println("圆形的半径是"+radius+"颜色是"+color+"周长是"+getPer()+"面积是"+getArea());

 

}
}
package lll;

public class lll {

    public static void main(String[] args) {
        Rectangle r=new Rectangle(4,6,'');

        r.showAll();

        Circle c=new Circle(1,'');

        c.showAll();    

    }

}

·4、 Cola 公司的雇员分为以下若干类:(知识点:多态)[必做

题]

·4.1 ColaEmployee :这是所有员工总的父类,属性:员工的

姓名,员工的生日月份。方法: getSalary ( int month )根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励

100元。

4.2 SalariedEmployee :ColaEmployee 的子类,拿固定工

4.3 HourlyEmployee : ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数

·4.4 SalesEmployee : ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率.4.5定义一个类 Company ,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类 TestCompany ,在 main 方法,把若干各种类型的员工在一个 ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

资的员工。属性:月薪

package zuoye9.two;

public class SalesEmployee extends ColaEmployee {
    int Moon_Sales;
    double Commission;

    public SalesEmployee() {
        super();
    }

    public SalesEmployee(String name, int birthday, int moon_Sales, double commission) {
        super(name, birthday);
        Moon_Sales = moon_Sales;
        Commission = commission;
    }

    public double getSalary(int month) {
        double Salary = 0;
        Salary = Moon_Sales * Commission;
        if (birthday == month) {
            Salary = Salary + 100;
        }
        return Salary;
    }
}

package zuoye9.two;

public class TestCompany {

    public static void main(String[] args) {
        ColaEmployee[] test=new ColaEmployee[3];
        test[0]=new SalariedEmployee("leisihao",5,50000);
        test[1]=new HourlyEmployee("zhixiang",2,150,200);
        test[2]=new SalesEmployee("taoxinyv",9,120000,0.3);
        Company show_fun=new Company();
        show_fun.ShowSalary(test[0], 5);
        show_fun.ShowSalary(test[1], 2);
        show_fun.ShowSalary(test[1], 3);
        show_fun.ShowSalary(test[2], 9);
        show_fun.ShowSalary(test[2], 10);
    }

}

package zuoye9.two;

public class SalariedEmployee extends ColaEmployee{
    double Salary;

    public SalariedEmployee() {
        super();
    }

    public SalariedEmployee(String name,int birthday,double salary) {
        super(name,birthday);
        Salary = salary;
    }

    public double getSalary(int month) {
        if(birthday==month)
        {
            Salary=Salary+100;
        }
        return Salary;
    }

}

package zuoye9.two;

public class HourlyEmployee extends ColaEmployee {
    double Hour_Salary;
    double Moon_WorkHour;

    public HourlyEmployee() {
        super();
    }

    public HourlyEmployee(String name, int birthday, double hour_Salary, double moon_WorkHour) {
        super(name, birthday);
        Hour_Salary = hour_Salary;
        Moon_WorkHour = moon_WorkHour;
    }

    public double getSalary(int month) {
        double Salary = 0;
        if (Moon_WorkHour <= 160) {
            Salary = Hour_Salary * Moon_WorkHour;
        } else if (Moon_WorkHour > 160) {
            Salary = Hour_Salary * Moon_WorkHour + (Moon_WorkHour - 160) * Hour_Salary * 1.5;
        }
        if (birthday == month) {
            Salary = Salary + 100;
        }
        return Salary;
    }
}

package zuoye9.two;

public class Company {
    public void ShowSalary(ColaEmployee test,int month)
    {
        System.out.println("员工:"+test.name+"\n月份:"+month+"\n工资状况:"+test.getSalary(month));
    }
}
package zuoye9.two;

public abstract class ColaEmployee {
    String name;
    int birthday;

    public ColaEmployee() {
        super();
    }

    public ColaEmployee(String name, int birthday) {
        super();
        this.name = name;
        this.birthday = birthday;
    }
}