前提・実現したいこと
エラーの原因が分かりません。
発生している問題・エラーメッセージ
TestPolyLine.java:11: エラー: シンボルを見つけられません System.out.println("numPoints(): " + pts.numPoints()); ^ シンボル: メソッド numPoints() 場所: タイプPoint[]の変数 pts TestPolyLine.java:12: エラー: シンボルを見つけられません System.out.println("totalLength(): " + pts.totalLength()); ^ シンボル: メソッド totalLength() 場所: タイプPoint[]の変数 pts TestPolyLine.java:13: エラー: シンボルを見つけられません System.out.println("leftmostPoint(): " + pts.leftmostPoint()); ^ シンボル: メソッド leftmostPoint() 場所: タイプPoint[]の変数 pts TestPolyLine.java:14: エラー: シンボルを見つけられません System.out.println("horizontalDiameter(): " + pts.horizontalDiameter()); ^ シンボル: メソッド horizontalDiameter() 場所: タイプPoint[]の変数 pts エラー4個
PolyLine.java
java
1public interface PolyLine { 2 3 public int numPoints(); 4 5 // what is the total length of all segments? 6 public double totalLength(); 7 8 // return the point furthest left of all the points 9 // if there are several points sharing the furthest left x-coord., return any one of them 10 public Point leftmostPoint(); 11 12 // compute the horizontal distance between the leftmost and rightmost points 13 public double horizontalDiameter(); 14 15}
PolyLineObj.java
java
1public class PolyLineObj implements PolyLine { 2 3 private Point[] pts; 4 5 public PolyLineObj(Point[] pts) { 6 if (pts == null) { 7 throw new RuntimeException("pts cannot be null"); 8 } 9 if (pts.length < 2) { 10 throw new RuntimeException("pts must contain at least two points"); 11 } 12 for (int i = 0; i < pts.length; i++) { 13 if (pts[i] == null) { 14 throw new RuntimeException("pts must contain non-null points"); 15 } 16 } 17 this.pts = pts; 18 } 19 20 public int numPoints() { 21 return this.pts.length; 22 } 23 24 public double totalLength() { 25 double total = 0.0; 26 for (int i = 0; i < pts.length; i++) { 27 total += this.pts[i].dist(Point[i + 1]); 28 } 29 return total; 30 } 31 32 public Point leftmostPoint() { 33 Point l = this.pts[0]; 34 for (int i = 1; i < pts.length; i++) { 35 if (l.getX() > this.pts[i + 1].getX()) { 36 l = this.pts[i + 1]; 37 } 38 } 39 return l; 40 } 41 42 public double horizontalDiameter() { 43 // get a rightmost point 44 Point r = this.pts[0]; 45 for (int i = 1; i < pts.length; i++) { 46 if (r.getX() < this.pts[i + 1].getX()) { 47 r = this.pts[i + 1]; 48 } 49 } 50 return r.getX() - l.getX(); 51 } 52 53 public String toString() { 54 for (int i = 0; i < pts.length; i++) { 55 if (i != (pts.length - 1)) { 56 System.out.print(this.pts[i] + " --> "); 57 } else { 58 System.out.print(this.pts[i]); 59 } 60 } 61 } 62 63} 64
TestPolyLine.java
java
1public class TestPolyLine { 2 public static void main(String[] args) { 3 Point[] pts = { 4 new PointObj(0.0, 0.0), 5 new PointObj(-2, 0.0), 6 new PointObj(-2, 1.0), 7 new PointObj(3.0, -1) 8 }; 9 10 System.out.println("pts: " + pts); 11 System.out.println("numPoints(): " + pts.numPoints()); 12 System.out.println("totalLength(): " + pts.totalLength()); 13 System.out.println("leftmostPoint(): " + pts.leftmostPoint()); 14 System.out.println("horizontalDiameter(): " + pts.horizontalDiameter()); 15 } 16} 17
Point.java
java
1public interface Point { 2 public double getX(); 3 public double getY(); 4 public double dist(Point q); 5} 6
PointObj.java
java
1public class PointObj implements Point { 2 3 double x; 4 double y; 5 6 PointObj(double x, double y) { 7 this.x = x; 8 this.y = y; 9 } 10 11 public double getX() { 12 return this.x; 13 } 14 15 public double getY() { 16 return this.y; 17 } 18 19 public double dist(Point q) { 20 double dx = this.x - q.getX(); 21 double dy = this.y - q.getY(); 22 return Math.sqrt(dx*dx + dy*dy); 23 } 24 25 public String toString() { 26 return "(" + this.x + ", " + this.y + ")"; 27 } 28 29} 30
試したこと
メソッド名等、誤字脱字がないか何度も確認しました。
PointやPointObjの定義はどのようなものでしょうか。
何のためにPoint[] と配列で宣言したのでしょうか
配列を使う練習をしております。
でもエラーが出てるところは配列として呼び出そうとしてませんよね?
この書き方をすれば、配列として呼び出せると勘違いしておりました。どの様にすれば、配列に対して、メソッドを使うことが出るのでしょうか?
mts10806さんのおっしゃる通り、配列を呼び出せていませんでした。
該当箇所ですが、自分がやりたかったことが、以下の様だったと気付きました。
```
Point[] pts = {
new PointObj(0.0, 0.0),
new PointObj(-1, 0.0),
new PointObj(-1, 1.0),
new PointObj(3.0, 1.0),
};
PolyLine a = new PolyLineObj(pts);
```
ありがとうございました。
回答1件
あなたの回答
tips
プレビュー