亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您好,請問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 關注
  • 314 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號