第九次作业

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 wen;

 

public abstract class Shape {

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 wen;

 

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 wen;

 

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 wen;

 

public class test {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

Rectangle r=new Rectangle(2,4,'蓝');

r.showAll();

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

c.showAll();

 

}

 

}

 

4、Cola公司的雇员分为以下若干类:(知识点:多态)[必做题]
•4.1 ColaEmployee:这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(intmonth)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
•4.2 SalariedEmployee:ColaEmployee的子类,拿固定工资的员工。属性:月薪
•4.3 HourlyEmployee:ColaEmployee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。属性:每小时的工资、每月工作的小时数
•4.4SalesEmployee:ColaEmployee的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
•4.5定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee数组里,并单元出数组中每个员工当月的工资。

package wen;

 

public abstract class ColaEmployee {

protected String name;

protected int month;

public ColaEmployee() {

super();

}

 

public ColaEmployee(String name, int month) {

super();

this.name = name;

        this.month = month;

    }

 

public String getName() {

        return name;

    }

 

public void setName(String name) {

this.name = name;

}

 

    public int getMonth() {

     return month;

     }

 

    public void setMonth(int month) {

     this.month = month;

     }

 

    public double getSalary(int month) {

     return 0;

 

}

 

}

package wen;

 

public class SalariedEmployee extends ColaEmployee {

private double salary;

 

public SalariedEmployee(double salary) {

super();

this.salary = salary;

}

 

public SalariedEmployee(String name, int month, double salary) {

super(name, month);

this.salary = salary;

}

 

public double getSalary(int month) {

if (super.month == month) {

return month + 100;

else {

return salary;

}

}

}

package wen;

 

public class HourlyEmployee extends ColaEmployee{

protected int hour;

protected int hoursalary;

public HourlyEmployee() {

super();

}

 

public HourlyEmployee(String name, int month, int hour, int hoursalary) {

super(name, month);        

this.hour = hour;

        this.hoursalary = hoursalary;

        }

 

public double getSalary(int month) {

if (super.month == month) {

if (hour > 160) {

return hoursalary * 160 + hoursalary * (hour - 160) * 1.5 + 100;

else {

return hoursalary * hour + 100;

 }

else {

if (hour > 160) {

return hoursalary * 160 + hoursalary * (hour - 160) * 1.5;

else {

return hoursalary * hour;

 

}

}

 

}

 

}

package wen;

 

public class SalesEmployee extends ColaEmployee {

protected int yuexiaoshou;

protected double tuchenglv;

public SalesEmployee() {

super();

}

 

public SalesEmployee(String name, int month, int yuexiaoshou, double tuchenglv) {

super(name, month);

this.yuexiaoshou = yuexiaoshou;

this.tuchenglv = tuchenglv;  

}

 

public double getSalary(int month) {

if (super.month == month) {

return yuexiaoshou * tuchenglv + 100;

else {

return yuexiaoshou * tuchenglv;

}

}

}

 

package wen;

 

public class company {

 

public void getSalary(ColaEmployee c, int month) {

System.out.println(c.name + "在" + month + "月的月薪为" + c.getSalary(month) + "元");

 

}

 

}

package wen;

 

public class testCompany {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

ColaEmployee c1[] = { new SalariedEmployee("a", 10, 20000), new HourlyEmployee("b", 6, 200, 200),new SalesEmployee("c", 5, 340000, 0.5) };

for (int i = 0; i < c1.length; i++) {

new company().getSalary(c1[i], 5);

}

 

}

 

}

 

posted @ 2023-06-05 12:51  计科2107孟展冰  阅读(7)  评论(0编辑  收藏  举报