为了账号安全,请及时绑定邮箱和手机立即绑定

您好,请问JAVA语言程序设计两道练习题。谢谢!

您好,请问JAVA语言程序设计两道练习题。谢谢!

青春有我 2021-11-12 07:07:27
1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。2、编写程序,求柱体的体积:(1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();(2)、为柱体设计类pillar,要求:a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。b)实现构造方法,对成员变量赋值。c)包含成员方法,计算柱体pillar的体积。(3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积。
查看完整描述

2 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

第一题有问题:1、创建Person接口(即“人”),它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
问题是:你说要创建一个人(接口),然后里面有方法对人的属性进行赋值?这怎么可能呢,接口是没有成员变量(属性)的,怎么能赋值?接口里只能有常量。

第二题可以答一下:
package pillar;
public class Pillar { private Geometry buttom;
private double height;
public Pillar() {
// TODO Auto-generated constructor stub
}
public Pillar(Geometry button,double height){
this.buttom = button;
this.height = height;
}
public double getVolume(){
return this.buttom.getArea()*height;
}
public Geometry getButtom() {
return buttom;
}
public void setButtom(Geometry buttom) {
this.buttom = buttom;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public interface Geometry { double getArea();
}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Circle implements Geometry { private double r;
public Circle() {
// TODO Auto-generated constructor stub
}

public Circle(double r) {
this.r = r;
}

public double getArea() { return Math.PI*r*r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class Rectangle implements Geometry { private double width;
private double height;

public Rectangle() {
// TODO Auto-generated constructor stub
}

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

public double getArea() { return this.width*this.height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}

}
------------------------------------------------类分割线---------------------------------------------------------
package pillar;
public class TestPillar {
/** * @param args
*/
public static void main(String[] args) {
Circle c = new Circle(5);
Rectangle r = new Rectangle(3,4);
Pillar p1 = new Pillar(c,6);
Pillar p2 = new Pillar(r,6);
System.out.println("圆的体积:"+p1.getVolume()+"\t矩形的体积:"+p2.getVolume());
}

}



查看完整回答
反对 回复 2021-11-14
?
芜湖不芜

TA贡献1796条经验 获得超7个赞

第一题

Person接口


import java.util.Date;public interface Person {    public void setData(String sID, String speciality, String name, String sex, Date birthday);    public String getData();                            }

Student实现类


import java.util.Date;public class Student implements Person {    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 + "]";    }        }



查看完整回答
反对 回复 2021-11-14
  • 2 回答
  • 0 关注
  • 259 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信