質問
Java初心者です。
Point,Rectangle,Circleの各面積の合計を計算し、出力するプログラムを書いていますが、次のようなエラーが出てしまい、うまくいきません。
どこが間違っているのでしょうか?
エラーメッセージ
TestShape.java:19: エラー: シンボルを見つけられません System.out.println("Point"+ i+1 +"Area is " + obj[i].getArea()); ^ シンボル: メソッド getArea() 場所: クラス Object TestShape.java:20: エラー: シンボルを見つけられません sum += obj[i].getArea(); ^ シンボル: メソッド getArea() 場所: クラス Object TestShape.java:23: エラー: シンボルを見つけられません System.out.println("Circle"+ i+1 +"Area is " + obj[i].getArea()); ^ シンボル: メソッド getArea() 場所: クラス Object TestShape.java:24: エラー: シンボルを見つけられません sum += obj[i].getArea(); ^ シンボル: メソッド getArea() 場所: クラス Object TestShape.java:27: エラー: シンボルを見つけられません System.out.println("Rectangle"+ i+1 +"Area is " + obj[i].getArea()); ^ シンボル: メソッド getArea() 場所: クラス Object TestShape.java:28: エラー: シンボルを見つけられません sum += obj[i].getArea(); ^ シンボル: メソッド getArea() 場所: クラス Object エラー6個
Java
1interface Shape { 2 public double getArea(); 3} 4 5public class Point implements Shape{ 6 int x; 7 int y; 8 Point(){ 9 x = 0; 10 y = 0; 11 } 12 Point(int x, int y){ 13 this.x = x; 14 this.y = y; 15 } 16 public double getArea(){ 17 return 0.0; 18 } 19} 20 21 22public class Circle implements Shape{ 23 int x; 24 int y; 25 int r; 26 Circle(){ 27 x = 0; 28 y = 0; 29 r = 0; 30 } 31 Circle(int x, int y, int r){ 32 this.x = x; 33 this.y = y; 34 this.r = r; 35 } 36 public double getArea(){ 37 return (r * r * Math.PI); 38 } 39} 40 41public class Rectangle implements Shape{ 42 int x; 43 int y; 44 int h; 45 int w; 46 Rectangle(){ 47 x = 0; 48 y = 0; 49 h = 0; 50 w = 0; 51 } 52 Rectangle(int x, int y, int h, int w){ 53 this.x = x; 54 this.y = y; 55 this.h = h; 56 this.w = w; 57 } 58 public double getArea(){ 59 return (h * w); 60 } 61} 62 63 64public class TestShape{ 65 public static void main(String[] args){ 66 Object[] obj = { 67 new Point(1,1), 68 new Point(2,5), 69 new Point(3,7), 70 new Circle(4,2,4), 71 new Circle(2,3,5), 72 new Circle(3,2,6), 73 new Circle(5,5,5), 74 new Rectangle(3,4,5,6), 75 new Rectangle(2,3,4,5), 76 new Rectangle(7,5,3,2), 77 new Rectangle(2,3,4,5), 78 new Rectangle(7,8,9,6), 79 }; 80 81 double sum = 0.0; 82 83 for(int i = 0; i < 3; i++){ 84 System.out.println("Point "+ (i+1) +" Area is " + obj[i].getArea()); 85 sum += obj[i].getArea(); 86 } 87 for(int i = 3; i < 7; i++){ 88 System.out.println("Circle "+ (i+1) +" Area is " + obj[i].getArea()); 89 sum += obj[i].getArea(); 90 } 91 for(int i = 7; i < 12; i++){ 92 System.out.println("Rectangle "+ (i+1) +" Area is " + obj[i].getArea()); 93 sum += obj[i].getArea(); 94 } 95 System.out.println("The sum of area values is " + sum); 96 } 97} 98
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/05/26 15:24
2018/05/26 15:48
退会済みユーザー
2018/05/27 01:35