BNG asked in 電腦與網際網路程式設計 · 1 decade ago

java +實做介面 高手 請幫我修改正確 謝謝

檔案名稱ans1_24.java(內含Shape、Circle及Cylinder類別,以及Color和Weight介面)(50%)

請參考圖2去修改(1)的程式碼,建立Color介面,讓你的形狀可以有顏色,要有getColor及setColor方法,並且建立Weight介面,讓Cylinder可以有重量,要有getWeight及setWeight方法。再將修改後的各形狀屬性印出,印出格式請參考範例1_2

提示:Shape要增加color屬性(型別String),Cylinder要增加weight屬性(型別double)

PS. shape 是抽象 color 要再shape實做

Circle 定義抽象 shape

Cylinder 繼承父親 實做 weight 介面

我的程式碼:

interface Color{

public void setColor(String str);

public String getColor();

}

interface Weight{

public void setWeight(String weight);

public String getWeight();

}

abstract class Shape extends Color {

abstract double getArea();

abstract double getVol();

abstract String getName();

abstract String printAttirb();

}

class Circle extends Shape implements Color {

private double radius=0;

String color ;

public Circle(double radius){

this.radius = radius ;

}

void setColor(String str){

color = str ;

}

void setRadius(double radius){

this.radius = radius ;

}

double getRadius(){

return radius ;

}

double getArea(){

return Math.PI *radius*radius ;

}

double getVol(){

return 0 ;

}

String getName(){

return "圓形";

}

String printAttirb(){

return getName()+"\n顏色="+"\n半徑="+getRadius()+"\n面積"+getArea() ;

}

}

class Cylinder extends Circle {

private double height=0 ;

public Cylinder(double radius,double height){

super(radius);

this.height= height ;

}

void setheight(double height){

this.height= height ;

}

double getheight(){

return height ;

}

double getArea(){

return 2*super.getArea()+2*Math.PI*super.getRadius()*getheight();

}

double getVol(){

return super.getArea()*height;

}

String getName(){

return "圓柱體";

}

String printAttirb(){

return super.printAttirb()+"\n高="+getheight()+"\n體積"+getVol();

}

}

public class ans1_2 {

public static void main(String args[]){

Circle cir=new Circle(4.2);

System.out.println(cir.printAttirb());

System.out.println("");

Cylinder james=new Cylinder(4.2,5.2);

System.out.println(james.printAttirb());

}

}

2 Answers

Rating
  • 薩秋
    Lv 5
    1 decade ago
    Favorite Answer

    abstract class Shape implements Color {//用implements

    .......................

    String color;//提示:Shape要增加color屬性(型別String)

    @Override

    public String getColor() {

    return color;

    }

    @Override

    public void setColor(String str) {

    color = str;

    }

    }

    Circle extends Shape

    Shape 已經有實做Color所以這不用加,裡頭的color 屬性也可以刪,在Shape已經有

    Circle 裡的 printAttirb() return 顏色後加上getColor(),就可印出顏色

    這樣Circle 就改完了

    class Cylinder extends Circle implements Weight{

    //依題目所需,讓Cylinder可以有重量

    double weight;//Cylinder要增加weight屬性(型別double),這是用double所以Weight介面裡的應該是double不是String,要不就在Override自己做轉型Double.parseDouble(weight)

    ....................................

    @Override

    public double getWeight() {

    return weight;

    }

    @Override

    public void setWeight(double weight) {

    this.weight = weight;

    }

    }

    Cylinder 裡的 printAttirb() return 顏色後加上getColor(),就可印出顏色,加上getWeight(),就可印重量

    這樣Cylinder 也改完了

    再來main

    new出cir 物件後,

    cir.setColor("顏色");//就可在這設定顏色

    new出 james物件後,

    james.setColor("顏色");//設顏色

    james.setWeight(10.0);//設重量

    PS:雙引號自行取代成半行

    Source(s): ME
  • 1 decade ago

    請參考圖2,

    能給圖2 嗎?

Still have questions? Get your answers by asking now.