回答編集履歴

2

コンストラクタ改善

2016/07/03 17:54

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -38,13 +38,9 @@
38
38
 
39
39
 
40
40
 
41
- // 終点はデフォルトで0.0になる
42
-
43
41
  public LineSegment(double x, double y){
44
42
 
45
- this.x = x;
43
+ this(x, y, 0.0, 0.0);
46
-
47
- this.y = y;
48
44
 
49
45
  }
50
46
 
@@ -52,7 +48,9 @@
52
48
 
53
49
  public LineSegment(double x, double y, double x1, double y1){
54
50
 
55
- this(x, y);
51
+ this.x = x;
52
+
53
+ this.y = y;
56
54
 
57
55
  this.x1 = x1;
58
56
 
@@ -78,17 +76,13 @@
78
76
 
79
77
  ThickLineSegment(double x1, double y1, double x2, double y2){
80
78
 
81
- super(x1, y1, x2, y2);
79
+ this(x1, y1, x2, y2, 1);
82
-
83
- setWidth(1);
84
80
 
85
81
  }
86
82
 
87
83
  ThickLineSegment(double x, double y, int w){
88
84
 
89
- super(x, y);
85
+ this(x, y, 0.0, 0.0, w);
90
-
91
- setWidth(w);
92
86
 
93
87
  }
94
88
 

1

コメントを受けて

2016/07/03 17:54

投稿

swordone
swordone

スコア20651

test CHANGED
@@ -17,3 +17,103 @@
17
17
  要件の一つにあるwidthも見当たりません。makeForm,toStringの実装もめちゃくちゃです。
18
18
 
19
19
  しかし腑に落ちないのが、これではThickLineSegmentクラスからどうやってもLineSegmentクラスの「もう一方の端点」の情報と思われるx1,y1に値を設定することができないということ。LineSegmentクラスもいじれるんですかね?
20
+
21
+
22
+
23
+ ---
24
+
25
+
26
+
27
+ LineSegmentもいじれるとのことなので改良策を。
28
+
29
+ そもそも「線分」というからには始点・終点を設定するのが本来の姿のはずなので、始点・終点を設定するコンストラクタをLineSegmentに実装させる。
30
+
31
+ ```java
32
+
33
+ public class LineSegment{
34
+
35
+ private double x, y; //始点
36
+
37
+ private double x1, y1; //終点
38
+
39
+
40
+
41
+ // 終点はデフォルトで0.0になる
42
+
43
+ public LineSegment(double x, double y){
44
+
45
+ this.x = x;
46
+
47
+ this.y = y;
48
+
49
+ }
50
+
51
+
52
+
53
+ public LineSegment(double x, double y, double x1, double y1){
54
+
55
+ this(x, y);
56
+
57
+ this.x1 = x1;
58
+
59
+ this.y1 = y1;
60
+
61
+ }
62
+
63
+ }
64
+
65
+ ```
66
+
67
+ で、これを継承したThickLineSegmentでは幅を設定すればいいだけにする。
68
+
69
+ ```java
70
+
71
+ class ThickLineSegment extends LineSegment{
72
+
73
+
74
+
75
+ int width;
76
+
77
+
78
+
79
+ ThickLineSegment(double x1, double y1, double x2, double y2){
80
+
81
+ super(x1, y1, x2, y2);
82
+
83
+ setWidth(1);
84
+
85
+ }
86
+
87
+ ThickLineSegment(double x, double y, int w){
88
+
89
+ super(x, y);
90
+
91
+ setWidth(w);
92
+
93
+ }
94
+
95
+
96
+
97
+ ThickLineSegment(double x1, double y1, double x2, double y2,int w){
98
+
99
+ super(x1, y1, x2, y2);
100
+
101
+ setWidth(w);
102
+
103
+ }
104
+
105
+
106
+
107
+ public void setWidth(int w){
108
+
109
+ width = w;
110
+
111
+ }
112
+
113
+ }
114
+
115
+ ```
116
+
117
+ makeFormの要件は、ご自身のコードの条件の部分をよく見てください。
118
+
119
+ toStringの要件がよくわからないのですが…