2016-10-25 9 views
0

私はこれをいくつかの方法で試しました。これらのクラスを継承するには、継承を使用する必要があります。私がプログラムを実行するたびに、音量と面積を0.0にします。半径が正しく表示されます。下部に出力します。シリンダー領域、体積、範囲+継承の場合、プログラム印刷で0.0が印刷されます。java

public class Base_HW04Q1 
{ 
    public double pi = 3.14, l, radius, height, area, volume; 

    public static class RoundShape extends Base_HW04Q1 { 
     public RoundShape(double radius) { 
      this.radius = radius; 
     } 
     public double calcArea() { 
      area = (radius * radius) * pi; 
      return area; 
     } 
     public String toString() { 
      return "A Round Shape of radius: " + radius + ", area " + area + "."; 
     } 
    } 


    public static class Cylinder extends Base_HW04Q1 
    { 
     public Cylinder(double radius, double height) { 
      this.radius = radius; 
      this.height = height; 
     } 
     public double calcArea() { 
      l = Math.sqrt((radius * radius) + (height * height)); 
      area = 2 * pi * radius * height + 2 * pi * l; 
      return area; 
     } 
     public double calcVolume() { 
      volume = pi * (radius * radius) * height; 
      return volume; 
     } 
     public String toString() { 
      return "A Cylinder of radius: " + radius + ", area " + area + " and a volume of " + volume; 
     } 
    } 


    public static class Cone extends Base_HW04Q1 //TODO: This line is almost, but not quite, complete. 
    { 
     public Cone(double radius, double height) { 
      this.radius = radius; 
      this.height = height; 
     } 
     public double calcArea() { 
      l = Math.sqrt((radius * radius) + (height * height)); 
      area = (pi * radius * l) + (pi * radius * radius); 
      return area; 
     } 
     public double calcVolume() { 
      volume = 0.333 * pi * radius * radius * height; 
      return volume; 
     } 
     public String toString() { 
      return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume; 
     } 
    } 

    public static void main(String[] args) 
    { 
     //object creation 
     Cylinder Cylinder1 = new Cylinder(30, 10); 
     Cone Cone1 = new Cone(10, 20); 
     RoundShape RoundShape1 = new RoundShape(50); 

     //print for objects 
     System.out.println(Cylinder1); 
     System.out.println(RoundShape1); 
     System.out.println(Cone1); 
    } 
} 

出力:半径の

Aシリンダー:30.0、面積0.0半径の0.0丸形 の体積:50.0、面積0.0。半径のコーン:10.0、面積0.0および0.0

+0

このコードをどのように呼び出していますか? –

答えて

4

あなたtoString()の 量は決して計算を行うメソッドを呼び出していないし、代わりにデフォルトの0.0フィールド値を出力します。 calcXxxx()メソッドが呼び出される前、つまり計算されたフィールドにまともな値が与えられる前に、toString()が呼び出されると、このリスクが発生します。最良の解決策は、最初にエリアとボリュームなどの計算値のフィールドを完全に取り除くことによって、この問題が最初に起こるのを防ぐことです。 toString()内ではなく、メソッドを呼び出してこれらの値を取得します。

例えば、

public double pi = 3.14, l, radius, height; // , area, volume; 

public static class RoundShape extends Base_HW04Q1 { 
    public RoundShape(double radius) { 
     this.radius = radius; 
    } 
    public double calcArea() { 
     return (radius * radius) * pi; 
     // return area; 
    } 
    public String toString() { 
     return "A Round Shape of radius: " + radius + ", area " + calcArea() + "."; 
    } 
} 
+0

万が一ダブルを3に制限する方法を知っていますか?例えば、0.00?現在の回答ではなく7363.62732812 – TheMuffinMan

+0

@TheMuffinMan:数字を文字列表現と混同しています。ダブルを "制限"しないでください。しかし、それを印刷するときは、*** Format ***をDecimalFormatオブジェクトまたはSystem.out.printfのどちらかとしてください。 –

0

あなただけのオブジェクトをインスタンス化し、他の方法ではコンストラクタではなくするために入力されたためです。

Cylinder Cylinder1 = new Cylinder(30, 10); 
    Cone Cone1 = new Cone(10, 20); 
    RoundShape RoundShape1 = new RoundShape(50); 

これらのメソッド

public double calcArea() { 
     l = Math.sqrt((radius * radius) + (height * height)); 
     area = (pi * radius * l) + (pi * radius * radius); 
     return area; 
    } 
    public double calcVolume() { 
     volume = 0.333 * pi * radius * radius * height; 
     return volume; 
    } 
    public String toString() { 
     return "A Cone of radius: " + radius + ", area " + area + " and a volume of " + volume; 
    } 

、残りの方法で呼び出す誰。

public static void main(String[] args) 
{ 
    //object creation 
    Cylinder Cylinder1 = new Cylinder(30, 10); 
    Cone Cone1 = new Cone(10, 20); 
    RoundShape RoundShape1 = new RoundShape(50); 

    double roundArea = RoundShape1.calcArea();//then use this 
    string roundMessage = RoundShape1.toString();//and this whatever you want. 

    //do it in others too 

    //print for objects 
    System.out.println(Cylinder1); 
    System.out.println(RoundShape1); 
    System.out.println(Cone1); 
} 
関連する問題