java接口编程题

来源:互联网 发布:mysql update 阻塞 编辑:程序博客网 时间:2024/05/29 18:36

1、创建Person接口(”),它有setData()getData()方法对属性namesexbirthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接口,并对自己的学生属性的成员变量sIDspeciality设置值和获得它们值所组成的字符串信息。

package xianweifu;interface People{public  void setData(String name, String sex, String birthday);public String getData();}class Student implements People{private String name;private String sex;private String birthday;private String sID;private String speciality;public void setData(String name, String sex, String birthday) {this.name = name;this.sex = sex;this.birthday = birthday;}public String getData() {return "名字" + name + "性別" + sex + "生日" + birthday + "ID" + sID + "专长" +speciality;}}public class demo7 {public static void main(String[] args) {}}

2、编写程序,求柱体的体积:

1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();

2)、为柱体设计类pillar,要求:

a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。

b)实现构造方法,对成员变量赋值。

c)包含成员方法,计算柱体pillar的体积。

3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积。

package xianweifu;interface Geometry{public double getArea();}class Pillar{Geometry bottom;double height;public Pillar(Geometry bottom, double height){this.bottom=bottom;this.height=height;}public double Volume(){return bottom.getArea()*this.height; }}class Circle implements Geometry{double radius;public Circle(double radius){this.radius = radius;}public double getArea() {return Math.PI*this.radius*this.radius; }}class Rect implements Geometry{double wide,length;public Rect(double wide, double length){this.wide = wide;this.length = length;}public double getArea() {return wide*length;}}public class Demo {public static void main(String[] args) { Pillar pillar;       Geometry bottom;                 bottom = new Rect(10, 5);       pillar = new Pillar(bottom, 5);       System.out.println("矩形底的柱体的体积:" + pillar.Volume());                 bottom = new Circle(5);       pillar = new Pillar(bottom, 5);       System.out.println("圆形底的柱体的体积:" + pillar.Volume());  }}<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>

3设计一个系统:

 

xxx纯净水生产线

 

目前流程是:从某个地方把水取出来,然后经过缓冲,过滤,加热和放糖的步骤

 

abstract {

 

public void ();

 

}

 

interface 过滤{}

 

interface 缓冲{}

 

interface 加热{}

 

interface 放糖{}

 

class 纯净水1 extends  imps 过滤,缓冲{}

 

class 纯净水2 extends  imps 缓冲{}

 

class 纯净水2 extends  imps 过滤{}

package xianweifu;abstract class Water{public void water(){}}interface Filter{public void filter();}interface Buffer{public void buffer();}interface Warm{public void warm();}interface Suger{public void suger();}class Water1 extends Water implements Filter,Buffer{@Overridepublic void buffer() {// TODO 自动生成的方法存根}@Overridepublic void filter() {// TODO 自动生成的方法存根}}class Water2 extends Water implements Buffer{@Overridepublic void buffer() {// TODO 自动生成的方法存根}}class Water3 extends Water implements Filter{@Overridepublic void filter() {// TODO 自动生成的方法存根}}public class demo0 {public static void main(String[] args) {}}


0 0
原创粉丝点击