第九次作业
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 girl;
public abstract class Shape {
double area;
double per;
String color;
public Shape(String color) {
super();
this.color = color;
}
public Shape() {
super();
}
public abstract double getArea() ;
public void setArea(double area) {
this.area = area;
}
public abstract double getPer() ;
public void setPer(double per) {
this.per = per;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public abstract void showAll();
}
package girl;
public class Rectangle extends Shape {
double weight;
double height;
public Rectangle(String color) {
super(color);
}
public Rectangle(String color, double weight, double height) {
super(color);
this.weight = weight;
this.height = height;
}
@Override
public double getArea() {
// TODO Auto-generated method stub
return weight*height;
}
@Override
public double getPer() {
// TODO Auto-generated method stub
return (weight+height)*2;
}
@Override
public void showAll() {
// TODO Auto-generated method stub
System.out.println("长是: "+weight);
System.out.println("宽是: "+height);
System.out.println("面积是: "+getArea());
System.out.println("周长是: "+getPer());
System.out.println("颜色是: "+getColor());
}
}
package girl;
public class Circle extends Shape {
double radius;
public Circle() {
super();
}
public Circle(double radius,String 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());
System.out.println("颜色: "+color);
}
}
package girl;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Circle i=new Circle(2,"蓝色");
Rectangle r=new Rectangle("黑色", 8, 4);
r.showAll();
i.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 girl;
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 girl;
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 girl;
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 girl;
public class SalesEmployee extends ColaEmployee{
int MoonSales;
double Royaltyrate;
public SalesEmployee() {
super();
}
public SalesEmployee(String name, int birthday, int moonSales,
double royaltyrate) {
super(name, birthday);
MoonSales = moonSales;
Royaltyrate = royaltyrate;
}
public double getSalary(int month) {
double Salary=0;
Salary=MoonSales*Royaltyrate;
if(birthday==month)
{
Salary=Salary+100;
}
return Salary;
}
}
package girl;
public class Company {
public void ShowSalary(ColaEmployee test,int month) {
System.out.println("员工:"+test.name+"\n月份:"+month+"\n工资状况:"+test.getSalary(month));
}
}
package girl;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee[] t=new ColaEmployee[3];
t[0]=new SalariedEmployee("lll",7,7000);
t[1]=new HourlyEmployee("yyy",7,947,77);
t[2]=new SalesEmployee("ccc",7,77777,0.7);
Company i=new Company();
i.ShowSalary(t[0], 8);
i.ShowSalary(t[1], 2);
i.ShowSalary(t[1], 9);
i.ShowSalary(t[2], 7);
i.ShowSalary(t[2], 12);
}
}