第九次作业
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 test2;
public abstract class shape {
double area;
double per;
String color;
public shape() {
super();
}
public shape(String color) {
super();
this.color = color;
}
public abstract double getarea();
public abstract double getper();
public abstract void showall();
public String getcolor() {
return color;
}
}
public class rectangle extends shape {
double width;
double heigth;
public rectangle() {
super();
}
public rectangle(String color, double width, double heigth) {
super(color);
this.width = width;
this.heigth = heigth;
}
public double getarea() {
double area=width*heigth;
return area;
}
public double getper() {
double per=(width+heigth)*2;
return per;
}
public void showall() {
System.out.println("长"+heigth);
System.out.println("宽"+width);
System.out.println("面积"+getarea());
System.out.println("周长"+getper());
System.out.println("颜色"+color);
}
}
public class circle extends shape {
double radius;
public circle(String color, double radius) {
super(color);
this.radius = radius;
}
public circle() {
super();
}
public double getper() {
double per=2*3.14*radius;
return per;
}
public double getarea() {
double area=3.14*radius*radius;
return area;
}
public void showall() {
System.out.println("半径"+radius);
System.out.println("面积"+getarea());
System.out.println("周长"+getper());
}
}
public class A {
public static void main(String[] args) {
// TODO Auto-generated method stub
shape t=new rectangle("红色",2,3);
shape t1=new circle("白色",3);
t.showall();
t1.showall();
}
}
3、 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 test3;
public abstract class colaemployee {
String name;
int birthday;
public colaemployee() {
super();
}
public colaemployee(String name, int birthday) {
super();
this.name = name;
this.birthday = birthday;
}
public abstract double getsalary(int month);
}
package test3;
public class salariedemployee extends colaemployee {
double salary;
public salariedemployee() {
super();
}
public salariedemployee(String name, int birthday, double salary) {
super(name, birthday);
this.salary = salary;
}
public double getsalary(int month) {
if(birthday==month) {
salary=salary+100;
}
return salary;
}
}
package test3;
public class hourlyemployee extends colaemployee{
double gz;
double time;
public hourlyemployee() {
super();
}
public hourlyemployee(String name, int birthday, double gz, double time) {
super(name, birthday);
this.gz = gz;
this.time = time;
}
public double getsalary(int month) {
double salary=0;
if(time>160) {
salary=gz*time+(time-160)*gz*1.5;
}else
if(time<=160){
salary=gz*time;
}
if(birthday==month) {
salary=salary+100;
}
return salary;
}
}
package test3;
public class salaryemployee extends colaemployee{
int moon;
double commission;
public salaryemployee() {
super();
}
public salaryemployee(String name, int birthday, int moon, double commission) {
super(name, birthday);
this.moon = moon;
this.commission = commission;
}
public double getsalary(int month) {
double salary=0;
salary=moon*commission;
if(birthday==month) {
salary=salary+100;
}
return salary;
}
}
package test3;
public class company {
public void showsalary(colaemployee test,int month) {
System.out.println("员工:"+test.name+"\n月份:"+month+"\n工资状况:"+test.getsalary(month));
}
}
package test3;
public class testcompany {
public static void main(String[] args) {
// TODO Auto-generated method stub
colaemployee[]t=new colaemployee[3];
t[0]=new salaryemployee("gg",10,3500,0.3);
t[1]=new hourlyemployee("cc",5,200,300);
t[2]=new salariedemployee("qq",40,4000);
company f=new company();
f.showsalary(t[0], 5);
f.showsalary(t[1], 3);
f.showsalary(t[1], 4);
f.showsalary(t[2], 2);
f.showsalary(t[2], 10);
}
}