总结,由于在上一篇帖子中详细介绍了接口,对于抽象类和接口都有了较为深入的理解,所以今天的作业可以比较轻松地完成。
作业题一:
1>创建Person接口:public interface Person {
public void setData(String sID, String speciality,String name, String sex, Date birthday);
public String getData();
2>建立Student类:
public class Student implements Person{
/**
* @param args the command line arguments
*/
private String sID;
private String speciality;
private String name;
private String sex;
private Date birthday;
@Override
public void setData(String sID, String speciality, String name, String sex,
Date birthday) {
this.sID = sID;
this.speciality = speciality;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
@Override
public String getData() {
return "Student [sID=" + sID + ", speciality=" + speciality + ", name="
+ name + ", sex=" + sex + ", birthday=" + birthday + "]";
}
作业题二
1> 为柱体的底面设计一个接口Geometry:
public interface Geometry {
public double PI=3.1415;
public double getArea();
}
2>为柱体设计类pillar:
public class pillar {
private Geometry bottom;
private double height;
public pillar(Geometry bottom,double height){
this.bottom=bottom;
this.height=height;
}
public Geometry getBottom(){
return bottom;
}
public double getVolume(){
return this.bottom.getArea()*height;
}
}
3>编写测试类圆形类、矩形类实现Geometry接口:
a)Circle类:
public class Circle implements Geometry{
private double radius;
public Circle(double radius){
this.radius =radius;
}
public double getArea(){
return 2*PI*radius;
}
}
b)Rectangl类:
public class Rectangle implements Geometry{
private double length;
private double width;
public Rectangle(double length,double width){
this.length=length;
this.width=width;
}
public double getArea(){
return length*width;
}
}
4>编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积:
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scan = new Scanner(System.in);
double a,b,c,h;
System.out.println("请输入圆的半径:");
a = scan.nextDouble();
System.out.println("请输入矩形的长:");
b = scan.nextDouble();
System.out.println("请输入矩形的宽:");
c = scan.nextDouble();
System.out.println("请输入柱体的高:");
h = scan.nextDouble();
//Geometry S = new Circle(a);
//Geometry Y = new Rectangle(b,c);
Circle C = new Circle(a);
Rectangle R = new Rectangle(b,c);
pillar p1 = new pillar(C,h);
pillar p2 = new pillar(R,h);
System.out.println("圆柱的体积:"+p1.getVolume());
System.out.println("四棱柱的体积:"+p2.getVolume());
}
}
运行结果:
run:
请输入圆的半径:
2.5
请输入矩形的长:
4
请输入矩形的宽:
6
请输入柱体的高:
5
圆柱的体积:78.53750000000001
四棱柱的体积:120.0
成功构建 (总时间: 11 秒)
作业题三
1>抽象类water:
public abstract class water {
public void water(){};
}
2>流水线:
a)
/*****过滤****/
public interface Filter {
public void Filter();
}
b)
/*****缓冲*****/
public interface Buffer {
public void Buffer();
}
c)
/*****加热*****/
public interface Heat {
public void Heat();
}
d)
/******放糖*****/
public interface Sugar {
public void Sugar();
}
3>三类纯净水:
a)
public class Cwater1 extends water implements Filter,Buffer{
public void water(){System.out.println("提取1线纯净水");};
public void Filter(){System.out.println("对1线纯净水进行过滤");};
public void Buffer(){System.out.println("对1线纯净水进行缓冲");};
}
b)
public class Cwater2 extends water implements Buffer{
public void water(){System.out.println("提取2线纯净水");};
public void Buffer(){System.out.println("对2线纯净水进行缓冲");};
}
c)
public class Cwater3 extends water implements Filter{
public void water(){System.out.println("提取3线纯净水");};
public void Filter(){System.out.println("对3线纯净水进行过滤");};
}
4>生产线Produce:
public class Produce {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Cwater1 c1 = new Cwater1();
Cwater2 c2 = new Cwater2();
Cwater3 c3 = new Cwater3();
c1.water();
c1.Buffer();
c2.water();
c2.Buffer();
c3.water();
c3.Filter();
}
}
运行结果:
run:
提取1线纯净水
对1线纯净水进行缓冲
提取2线纯净水
对2线纯净水进行缓冲
提取3线纯净水
对3线纯净水进行过滤
成功构建 (总时间: 0 秒)