1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。
2、编写程序,求柱体的体积:
(1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();
(2)、为柱体设计类pillar,要求:
a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。
b)实现构造方法,对成员变量赋值。
c)包含成员方法,计算柱体pillar的体积。
(3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积。
3设计一个系统:
x纯净水生产线
目前流程是:从某个地方把水取出来,然后经过缓冲,过滤,加热和放糖的步骤
abstract 水{
public void 水();
}
interface 过滤{}
interface 缓冲{}
interface 加热{}
interface 放糖{}
class 纯净水1 extends 水 imps 过滤,缓冲{}
class 纯净水2 extends 水 imps 缓冲{}
class 纯净水2 extends 水 imps 过滤{}
代码如下:
第一题---------------------------------------------
package Person接口;
interface Person{
public void setData(String name,String sex,String birthday);
public void getData();
}
class Student implements Person{
private String name;
private String sex;
private String birthday;
public void setData(String name,String sex,String birthday){
this.name=name;
this.sex=sex;
this.birthday=birthday;
}
public void getData(){
System.out.println("姓名为:"+this.name+" "+"性别为:"+this.sex+" "+"生日为:"+this.birthday);
}
}
public class Day0701 {
public static void main(String[] args) {
Student s1=new Student();
s1.setData("张三","男","19940424");
s1.getData();
}
}
第二题.--------------------------------------------------------------------
package 求圆柱体体积;
interface Geometry{
public double getArea();
}
class Pillar {
private int height;
private double DiMian;
public Pillar(double DiMian,int height){
this.DiMian=DiMian;
this.height=height;
}
public void TiGi(){
System.out.println("圆柱体体积为:"+this.DiMian*this.height);
}
}
class Circle implements Geometry{
private int radius;
public Circle(int r){
radius=r;
}
public double getArea(){
return Math.PI*this.radius*this.radius;
}
}
class Rect implements Geometry{
private int length;
private int wide;
public Rect(int l,int w){
length=l;
wide=w;
}
public double getArea(){
return this.length*this.wide;
}
}
public class day0702 {
public static void main(String[] args) {
Rect rect=new Rect(10,5);
Circle circle=new Circle(5);
Pillar pillar1=new Pillar(circle.getArea(),5);
Pillar pillar2=new Pillar(rect.getArea(),5);
pillar1.TiGi();
pillar2.TiGi();
}
}
第三题---------------------------------------------------------
package 流水线;
abstract class Water {
public abstract String water();
}
interface GuoLv{
public abstract String guoLv();
}
interface HuanChong{
public abstract String huanChong();
}
interface JiaRe{
public String jiaRe();
}
interface FangTang{
public String fangTang();
}
class Water1 extends Water implements GuoLv,HuanChong{
public String water(){
return "取水";
}
public String guoLv(){
return "过滤";
}
public String huanChong(){
return "缓冲";
}
public void print(){
System.out.println(water()+" "+guoLv()+" "+huanChong());
}
}
class Water2 extends Water implements FangTang{
public String water(){
return "取水";
}
public String fangTang(){
return "放糖";
}
public void print(){
System.out.println(water()+" "+fangTang());
}
}
public class day0703 {
public static void main(String[] args) {
Water1 a=new Water1();
Water2 b=new Water2();
System.out.println("纯净水1:");
a.print();
System.out.println("纯净水2:");
b.print();
}
}