第十二周上机作业
1、设计四个类,分别是:(知识点:抽象类及抽象方法)
(1)Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
(2)2个子类:
1)Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
2)Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
(3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。
package chap1;
public abstract class Shape
{
private double area;
private double per;
private String color;
public Shape() {};
public Shape(String color)
{
this.color = color;
}
public abstract double getArea();
public abstract double getPer();
public abstract void showAll();
public String getColor()
{
return this.color;
}
}
package chap1;
public class Rectangle extends Shape
{
private double width;
private double height;
public Rectangle() {};
public Rectangle(double width, double height, String color)
{
super(color);
this.width = width;
this.height = height;
}
public double getPer()
{
return (width + height) * 2;
}
public double getArea()
{
return width * height;
}
public void showAll()
{
System.out.println(String.format("矩形长度%f,宽度%f,面积%f,周长%f,颜色为%s", width, height, getArea(), getPer(), getColor()));
}
}
package chap1;
public class Circle extends Shape
{
private double radius;
public Circle() {};
public Circle(double radius, String color)
{
super(color);
this.radius = radius;
}
public double getPer()
{
return 2 * 3.14 * radius;
}
public double getArea()
{
return 3.14 * Math.pow(radius, 2);
}
public void showAll()
{
System.out.println(String.format("圆半径%f,面积%f,周长%f,颜色为%s", radius, getArea(), getPer(), getColor()));
}
}
package chap1;
public class PolyDemo
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle(20, 10, "黄色");
rect.showAll();
Circle ccl = new Circle(20, "白色");
ccl.showAll();
}
}

2、Cola公司的雇员分为以下若干类:(知识点:多态)
(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
(2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。
属性:月薪
(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
属性:每小时的工资、每月工作的小时数
(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率
(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。
package week12;
public class ColaEmployee
{
private String name;
private int birthdayMonth;
public ColaEmployee(String name, int birthdayMonth)
{
this.name = name;
this.birthdayMonth = birthdayMonth;
}
public double getSalary(int month)
{
if (month == birthdayMonth)
return 100;
return 0;
}
}
package week12;
public class SalariedEmployee extends ColaEmployee
{
private double monthlyFee;
public SalariedEmployee(String name, int birthdayMonth, double monthlyFee)
{
super(name, birthdayMonth);
this.monthlyFee = monthlyFee;
}
@Override
public double getSalary(int month)
{
return monthlyFee + super.getSalary(month);
}
}
package week12;
public class HourlyEmployee extends ColaEmployee
{
private double feePerHour;
private int hours;
public HourlyEmployee(String name, int birthdayMonth, double feePerHour, int hours)
{
super(name, birthdayMonth);
this.feePerHour = feePerHour;
this.hours = hours;
}
@Override
public double getSalary(int month)
{
double fee = 0;
if (hours > 160)
{
fee += feePerHour * 160 + feePerHour * (hours - 160) * 1.5;
}
else
{
fee += feePerHour * hours;
}
fee += super.getSalary(month);
return fee;
}
}
package week12;
public class SalesEmployee extends ColaEmployee
{
private double monthlySale;
private double royaltyRate;
public SalesEmployee(String name, int birthdayMonth, double monthlySale, double royaltyRate)
{
super(name, birthdayMonth);
this.monthlySale = monthlySale;
this.royaltyRate = royaltyRate;
}
@Override
public double getSalary(int month)
{
double fee = 0;
fee += monthlySale * royaltyRate;
fee += super.getSalary(month);
return fee;
}
}
package week12;
public class Company
{
public static void printCompany(ColaEmployee emp, int month)
{
System.out.println(emp.getName() + "的工资额为" + emp.getSalary(month));
}
}
package week12;
public class TestCompany
{
public static void main(String[] args)
{
ColaEmployee[] emps = new ColaEmployee[]
{
new SalariedEmployee("张三", 4, 18000),
new HourlyEmployee("李四", 5, 200, 180),
new SalesEmployee("王二麻子", 6, 500000, 0.05)
};
for (ColaEmployee emp : emps)
{
Company.printCompany(emp, 5);
}
}
}

3、利用接口实现动态的创建对象:(知识点:接口 )
(1)创建4个类
1苹果
2香蕉
3葡萄
4园丁
(2)在三种水果的构造方法中打印一句话.
以苹果类为例
class apple
{
public apple()
{
System.out.println(“创建了一个苹果类的对象”);
}
}
(3)类图如下:

(4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象。
运行结果如图:

package week12;
public interface Fruit
{
}
package week12;
public class Apple implements Fruit
{
public Apple()
{
System.out.println("创建了一个苹果类的对象");
}
}
package week12;
public class Banana implements Fruit
{
public Banana()
{
System.out.println("创建了一个香蕉类的对象");
}
}
package week12;
public class Grape implements Fruit
{
public Grape()
{
System.out.println("创建了一个葡萄类的对象");
}
}
package week12;
import java.util.Scanner;
public class Gardener
{
public static Fruit create(String type)
{
switch (type)
{
case "苹果":
return new Apple();
case "香蕉":
return new Banana();
case "葡萄":
return new Grape();
default:
return null;
}
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
Fruit fruit = create(s.next());
if (fruit == null)
{
System.out.println("不存在的类");
}
}
}


浙公网安备 33010602011771号