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 hgjs;
public abstract class Shape {
double area;
double per;
char color;
public Shape() {
super();
}
public Shape(char color) {
super();
this.color = color;
}
public abstract double getArea();
public abstract double getPer();
public abstract void showAll();
public char getColor() {
return color;
}
}
package hgjs;
public class Rectangle extends Shape {
double Width;
double height;
public Rectangle() {
super();
}
public Rectangle(double width, double height,char color) {
super(color);
Width = width;
this.height = height;
}
public double getArea() {
double area=Width*height;
return area;
}
public double getPer() {
double per=2*(Width+height);
return per;
}
public void showAll() {
System.out.println("长是"+Width);
System.out.println("宽是"+height);
System.out.println("面积是"+getArea());
System.out.println("周长是"+getPer());
}
}
package hgjs;
public class Circle extends Shape {
double radius;
public Circle() {
super();
}
public Circle(double radius,char color) {
super(color);
this.radius = radius;
}
public double getArea() {
double area=radius*radius*3.14;
return area;
}
public double getPer() {
double per=2*3.14*radius;
return per;
}
public void showAll() {
System.out.println("半径是"+radius);
System.out.println("面积是"+getArea());
System.out.println("周长是"+getPer());
}
}
package hgjs;
public class main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle p1=new Rectangle(2,4,'白');
p1.showAll();
Circle p2=new Circle(4,'黑');
p2.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 nb;
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 nb;
public class SalariedEmployee extends ColaEmployee{
double salary;
public SalariedEmployee() {
super();
}
public SalariedEmployee(String name,int birthday,double salary1) {
super(name,birthday);
salary=salary1;
}
public double getSalary(int month) {
if(birthday==month) {
salary=salary+100;
}else
salary=salary;
return salary;
}
}
package nb;
public class HourlyEmployee extends ColaEmployee {
double H_salary;
double time;
public HourlyEmployee() {
super();
}
public HourlyEmployee(String name,int birthday,double H_salary1,double time1) {
super(name,birthday);
H_salary=H_salary1;
time = time1;
}
public double getSalary(int month) {
double salary=0;
if(birthday==month) {
if(time>160)
salary=H_salary*160+(time-160)*H_salary*1.5+100;
else
salary=H_salary*time+100;
}else {
if(time>160)
salary=H_salary*160+(time-160)*H_salary*1.5;
else
salary=H_salary*time;
}
return salary;
}
}
package nb;
public class SalesEmployee extends ColaEmployee {
double sales;
double ticheng;
public SalesEmployee() {
super();
}
public SalesEmployee(String name,int birthday,double sales1, double ticheng1) {
super(name,birthday);
sales = sales1;
ticheng = ticheng1;
}
public double getSalary(int month) {
double salary=0;
if(birthday==month) {
salary=sales*ticheng+100;
}else {
salary=sales*ticheng;
}
return salary;
}
}
package nb;
public class Company {
public void show(ColaEmployee test,int month) {
System.out.println("员工:"+test.name+"在"+month+"月的工资是:"+test.getSalary(month));
}
}
package nb;
public class testcompany {
public static void main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee[] t =new ColaEmployee[3];
t[0]=new SalariedEmployee("zhutianqi",6,12000);
t[1]=new HourlyEmployee("zhixiang",8,500,165);
t[2]=new SalesEmployee("taoxinyu",7,130000,0.4);
Company show1=new Company();
show1.show(t[0], 7);
show1.show(t[1], 8);
show1.show(t[2], 7);
}
}