質問編集履歴

4

情報の追記

2022/07/31 07:59

投稿

icebreak
icebreak

スコア4

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,11 @@
5
5
  Position[] arrayposition = new Position[count +1];
6
6
  を宣言し、それをメソッドaddPosition()で配列の値を定義しました。
7
7
  その上で同じクラス内の別のメソッドfindPositionでその値を利用したいのですが、どのようにすれば利用することが可能でしょうか。
8
+
9
+ <エラーメッセージ>
10
+ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
11
+ at Portfolio.addPosition(Issue4.java:223)
12
+ at Issue100.main(Issue4.java:32)
8
13
 
9
14
 
10
15
  実際に書いたコードはこちらです。

3

誤字の修正

2022/07/31 07:21

投稿

icebreak
icebreak

スコア4

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,277 @@
36
36
  }
37
37
  }
38
38
  }
39
+
40
+ public Position findPosition(Issue4 issue){
41
+ boolean bool2;
42
+ for(int i = 0;i < count + 1;i++){
43
+ bool2 = issue.equals(arrayposition[i]);
44
+ if(bool2 == true){
45
+ return arrayposition[i];
46
+ }
47
+ else{
48
+ }
49
+ }
50
+ return null;
51
+ }
52
+
53
+ @Override
54
+ public int hashCode(){
55
+ int result = 1;
56
+ result = 31*result + ((issue == null)? 0:issue.hashCode());
57
+ return result;
58
+ }
59
+ @Override
60
+ public boolean equals(Object obj){
61
+ if(this == obj){
62
+ return true;
63
+ }
64
+ if(obj == null){
65
+ return false;
66
+ }
67
+ if(getClass() != obj.getClass()){
68
+ return false;
69
+ }
70
+ Portfolio port = (Portfolio)obj;
71
+
72
+ if(!issue.equals(port.issue)){
73
+ return false;
74
+ }
75
+ return true;
76
+ }
77
+ }
78
+ java
79
+ コード全体
80
+ import java.io.*;
81
+
82
+ class Issue100{
83
+ public static void main(String[] args){
84
+
85
+ Issue4 issue1 = new Issue4("1111","AAAA");
86
+ Issue4 issue2 = new Issue4("2222","BBBB");
87
+ Issue4 issue3 = new Issue4("3333","CCCC");
88
+ Issue4 issue4 = new Issue4("4444","DDDD");
89
+ Issue4 issue5 = new Issue4("5555","EEEE");
90
+ Issue4 issue6 = new Issue4("6666","FFFF");
91
+ Issue4 issue7 = new Issue4("1111","AAAA");
92
+
93
+ Stock stock1 = new Stock("1111","AAAA",Market.TSE);
94
+ Stock stock2 = new Stock("2222","BBBB",Market.OSE);
95
+ Stock stock3 = new Stock("3333","CCCC",Market.NSE);
96
+ Bond bon1 = new Bond("1111","AAAA",20001111,1.0);
97
+ Bond bon2 = new Bond("2222","BBBB",20002222,1.5);
98
+ Bond bon3 = new Bond("3333","CCCC",20003333,2.0);
99
+
100
+
101
+ Position pos1 = new Position(issue1,0);
102
+ Position pos2 = new Position(issue2,0);
103
+ Position pos3 = new Position(issue3,0);
104
+ Position pos4 = new Position(issue4,0);
105
+ Position pos5 = new Position(issue5,0);
106
+ Position pos6 = new Position(issue6,0);
107
+
108
+ Portfolio por = new Portfolio();
109
+
110
+ por.addPosition(pos1);
111
+ por.addPosition(pos2);
112
+ /*
113
+ Position a = por.findPosition(issue7);
114
+ System.out.println(a);
115
+ */
116
+ }
117
+ }
118
+ class Issue4{
119
+
120
+ protected String code;
121
+ protected String name;
122
+
123
+ public Issue4(String code,String name)throws IllegalArgumentException{
124
+ if(code == null || name == null){
125
+ IllegalArgumentException e = new IllegalArgumentException();
126
+ throw e;
127
+ }
128
+ else{
129
+ this.code = code;
130
+ this.name = name;
131
+ }
132
+ }
133
+ public String getCode(){
134
+ return this.code;
135
+ }
136
+ public String getName(){
137
+ return this.name;
138
+ }
139
+ }
140
+
141
+ class Bond extends Issue4{
142
+
143
+ private int maturity;
144
+ private double coupon;
145
+
146
+ public Bond(String code,String name,int maturity,double coupon)throws IllegalArgumentException{
147
+ super(code,name);
148
+ if(maturity <= 20000101 || maturity >= 29991231){
149
+ IllegalArgumentException e = new IllegalArgumentException();
150
+ throw e;
151
+ }
152
+ else{
153
+ if(coupon < 0){
154
+ IllegalArgumentException e = new IllegalArgumentException();
155
+ throw e;
156
+ }
157
+ else{
158
+ this.maturity = maturity;
159
+ this.coupon = coupon;
160
+ }
161
+ }
162
+ }
163
+ public int getMaturity(){
164
+ return this.maturity;
165
+ }
166
+ public double getCoupon(){
167
+ return this.coupon;
168
+ }
169
+ public BondType getBondType(){
170
+ if(coupon == 0){
171
+ BondType bondtype = BondType.ZERO_COUPON_BOND;
172
+ return bondtype;
173
+ }
174
+ else{
175
+ BondType bondtype = BondType.COUPON_BOND;
176
+ return bondtype;
177
+ }
178
+ }
179
+ @Override
180
+ public String toString(){
181
+ return "コード :" + code + "\n" + "名称 :" + name + "\n" + "償還月日 :"+ this.maturity +
182
+ "\n" + "クーポンレート :" + this.coupon ;
183
+ }
184
+ @Override
185
+ public int hashCode(){
186
+ double result = 1;
187
+ result = 31*result + ((code == null)? 0:code.hashCode());
188
+ result = 31*result + this.maturity;
189
+ result = 31*result + this.coupon;
190
+ int result2 = (int)result;
191
+ return result2;
192
+ }
193
+ @Override
194
+ public boolean equals(Object obj){
195
+ if(this == obj){
196
+ return true;
197
+ }
198
+ if(obj == null){
199
+ return false;
200
+ }
201
+ if(getClass() != obj.getClass()){
202
+ return false;
203
+ }
204
+ Bond bond = (Bond)obj;
205
+
206
+ if(!code.equals(bond.code)){
207
+ return false;
208
+ }
209
+ if(maturity != bond.maturity){
210
+ return false;
211
+ }
212
+ if(coupon != bond.coupon){
213
+ return false;
214
+ }
215
+ return true;
216
+ }
217
+ }
218
+ class Stock extends Issue4{
219
+ private Market market;
220
+
221
+ public Stock(String code,String name,Market market){
222
+ super(code,name);
223
+ this.market = market;
224
+ }
225
+ public Market getMarket(){
226
+ return this.market;
227
+ }
228
+ @Override
229
+ public String toString(){
230
+ return "コード :" + code + "\n" + "名称 :" + name + "\n" + "上場市場 :"+ this.market;
231
+ }
232
+ @Override
233
+ public int hashCode(){
234
+ int result = 1;
235
+ result = 31*result + ((code == null)? 0:code.hashCode());
236
+ result = 31*result + ((this.market == null)? 0:market.hashCode());
237
+ return result;
238
+ }
239
+ @Override
240
+ public boolean equals(Object obj){
241
+ if(this == obj){
242
+ return true;
243
+ }
244
+ if(obj == null){
245
+ return false;
246
+ }
247
+ if(getClass() != obj.getClass()){
248
+ return false;
249
+ }
250
+ Stock stock = (Stock)obj;
251
+
252
+ if(!code.equals(stock.code)){
253
+ return false;
254
+ }
255
+ if(market != stock.market){
256
+ return false;
257
+ }
258
+ return true;
259
+ }
260
+ }
261
+ class Position{
262
+
263
+ protected Issue4 issue;
264
+ protected double amount;
265
+
266
+ public Position(){
267
+ }
268
+ public Position(Issue4 issue,double amount){
269
+ this.issue = issue;
270
+ this.amount = amount;
271
+ }
272
+ public Issue4 getIssue4(){
273
+ return this.issue;
274
+ }
275
+ public double getAmount(){
276
+ return this.amount;
277
+ }
278
+ public void setIssue4(Issue4 issue){
279
+ this.issue = issue;
280
+ }
281
+ public void setAmount(double amount){
282
+ this.amount = amount;
283
+ }
284
+ }
285
+ class Portfolio extends Position{
286
+ static int count = 0;
287
+ Position[] arrayposition = new Position[count +1];
288
+
289
+ public void addPosition(Position position){
290
+ if(count == 0){
291
+ arrayposition[count] = position;
292
+ count++;
293
+ }
294
+ else if(count > 0){
295
+ for(int i = 0; count > i;i++){
296
+ boolean bool = position.equals(arrayposition[i]);
297
+ if(bool == true){
298
+ amount++;
299
+ break;
300
+ }
301
+ else if(bool == false){
302
+ if(i == count -1){
303
+ arrayposition[count] = position;
304
+ count++;
305
+ }
306
+ }
307
+ }
308
+ }
309
+ }
39
310
  /*
40
311
  public Position findPosition(Issue4 issue){
41
312
  boolean bool2;
@@ -75,275 +346,4 @@
75
346
  return true;
76
347
  }
77
348
  }
78
- java
349
+
79
- コード全体
80
- import java.io.*;
81
-
82
- class Issue100{
83
- public static void main(String[] args){
84
-
85
- Issue4 issue1 = new Issue4("1111","AAAA");
86
- Issue4 issue2 = new Issue4("2222","BBBB");
87
- Issue4 issue3 = new Issue4("3333","CCCC");
88
- Issue4 issue4 = new Issue4("4444","DDDD");
89
- Issue4 issue5 = new Issue4("5555","EEEE");
90
- Issue4 issue6 = new Issue4("6666","FFFF");
91
- Issue4 issue7 = new Issue4("1111","AAAA");
92
-
93
- Stock stock1 = new Stock("1111","AAAA",Market.TSE);
94
- Stock stock2 = new Stock("2222","BBBB",Market.OSE);
95
- Stock stock3 = new Stock("3333","CCCC",Market.NSE);
96
- Bond bon1 = new Bond("1111","AAAA",20001111,1.0);
97
- Bond bon2 = new Bond("2222","BBBB",20002222,1.5);
98
- Bond bon3 = new Bond("3333","CCCC",20003333,2.0);
99
-
100
-
101
- Position pos1 = new Position(issue1,0);
102
- Position pos2 = new Position(issue2,0);
103
- Position pos3 = new Position(issue3,0);
104
- Position pos4 = new Position(issue4,0);
105
- Position pos5 = new Position(issue5,0);
106
- Position pos6 = new Position(issue6,0);
107
-
108
- Portfolio por = new Portfolio();
109
-
110
- por.addPosition(pos1);
111
- por.addPosition(pos2);
112
- /*
113
- Position a = por.findPosition(issue7);
114
- System.out.println(a);
115
- */
116
- }
117
- }
118
- class Issue4{
119
-
120
- protected String code;
121
- protected String name;
122
-
123
- public Issue4(String code,String name)throws IllegalArgumentException{
124
- if(code == null || name == null){
125
- IllegalArgumentException e = new IllegalArgumentException();
126
- throw e;
127
- }
128
- else{
129
- this.code = code;
130
- this.name = name;
131
- }
132
- }
133
- public String getCode(){
134
- return this.code;
135
- }
136
- public String getName(){
137
- return this.name;
138
- }
139
- }
140
-
141
- class Bond extends Issue4{
142
-
143
- private int maturity;
144
- private double coupon;
145
-
146
- public Bond(String code,String name,int maturity,double coupon)throws IllegalArgumentException{
147
- super(code,name);
148
- if(maturity <= 20000101 || maturity >= 29991231){
149
- IllegalArgumentException e = new IllegalArgumentException();
150
- throw e;
151
- }
152
- else{
153
- if(coupon < 0){
154
- IllegalArgumentException e = new IllegalArgumentException();
155
- throw e;
156
- }
157
- else{
158
- this.maturity = maturity;
159
- this.coupon = coupon;
160
- }
161
- }
162
- }
163
- public int getMaturity(){
164
- return this.maturity;
165
- }
166
- public double getCoupon(){
167
- return this.coupon;
168
- }
169
- public BondType getBondType(){
170
- if(coupon == 0){
171
- BondType bondtype = BondType.ZERO_COUPON_BOND;
172
- return bondtype;
173
- }
174
- else{
175
- BondType bondtype = BondType.COUPON_BOND;
176
- return bondtype;
177
- }
178
- }
179
- @Override
180
- public String toString(){
181
- return "コード :" + code + "\n" + "名称 :" + name + "\n" + "償還月日 :"+ this.maturity +
182
- "\n" + "クーポンレート :" + this.coupon ;
183
- }
184
- @Override
185
- public int hashCode(){
186
- double result = 1;
187
- result = 31*result + ((code == null)? 0:code.hashCode());
188
- result = 31*result + this.maturity;
189
- result = 31*result + this.coupon;
190
- int result2 = (int)result;
191
- return result2;
192
- }
193
- @Override
194
- public boolean equals(Object obj){
195
- if(this == obj){
196
- return true;
197
- }
198
- if(obj == null){
199
- return false;
200
- }
201
- if(getClass() != obj.getClass()){
202
- return false;
203
- }
204
- Bond bond = (Bond)obj;
205
-
206
- if(!code.equals(bond.code)){
207
- return false;
208
- }
209
- if(maturity != bond.maturity){
210
- return false;
211
- }
212
- if(coupon != bond.coupon){
213
- return false;
214
- }
215
- return true;
216
- }
217
- }
218
- class Stock extends Issue4{
219
- private Market market;
220
-
221
- public Stock(String code,String name,Market market){
222
- super(code,name);
223
- this.market = market;
224
- }
225
- public Market getMarket(){
226
- return this.market;
227
- }
228
- @Override
229
- public String toString(){
230
- return "コード :" + code + "\n" + "名称 :" + name + "\n" + "上場市場 :"+ this.market;
231
- }
232
- @Override
233
- public int hashCode(){
234
- int result = 1;
235
- result = 31*result + ((code == null)? 0:code.hashCode());
236
- result = 31*result + ((this.market == null)? 0:market.hashCode());
237
- return result;
238
- }
239
- @Override
240
- public boolean equals(Object obj){
241
- if(this == obj){
242
- return true;
243
- }
244
- if(obj == null){
245
- return false;
246
- }
247
- if(getClass() != obj.getClass()){
248
- return false;
249
- }
250
- Stock stock = (Stock)obj;
251
-
252
- if(!code.equals(stock.code)){
253
- return false;
254
- }
255
- if(market != stock.market){
256
- return false;
257
- }
258
- return true;
259
- }
260
- }
261
- class Position{
262
-
263
- protected Issue4 issue;
264
- protected double amount;
265
-
266
- public Position(){
267
- }
268
- public Position(Issue4 issue,double amount){
269
- this.issue = issue;
270
- this.amount = amount;
271
- }
272
- public Issue4 getIssue4(){
273
- return this.issue;
274
- }
275
- public double getAmount(){
276
- return this.amount;
277
- }
278
- public void setIssue4(Issue4 issue){
279
- this.issue = issue;
280
- }
281
- public void setAmount(double amount){
282
- this.amount = amount;
283
- }
284
- }
285
- class Portfolio extends Position{
286
- static int count = 0;
287
- Position[] arrayposition = new Position[count +1];
288
-
289
- public void addPosition(Position position){
290
- if(count == 0){
291
- arrayposition[count] = position;
292
- count++;
293
- }
294
- else if(count > 0){
295
- for(int i = 0; count > i;i++){
296
- boolean bool = position.equals(arrayposition[i]);
297
- if(bool == true){
298
- amount++;
299
- break;
300
- }
301
- else if(bool == false){
302
- if(i == count -1){
303
- arrayposition[count] = position;
304
- count++;
305
- }
306
- }
307
- }
308
- }
309
- }
310
- /*
311
- public Position findPosition(Issue4 issue){
312
- boolean bool2;
313
- for(int i = 0;i < count + 1;i++){
314
- bool2 = issue.equals(arrayposition[i]);
315
- if(bool2 == true){
316
- return arrayposition[i];
317
- }
318
- else{
319
- }
320
- }
321
- return null;
322
- }
323
- */
324
- @Override
325
- public int hashCode(){
326
- int result = 1;
327
- result = 31*result + ((issue == null)? 0:issue.hashCode());
328
- return result;
329
- }
330
- @Override
331
- public boolean equals(Object obj){
332
- if(this == obj){
333
- return true;
334
- }
335
- if(obj == null){
336
- return false;
337
- }
338
- if(getClass() != obj.getClass()){
339
- return false;
340
- }
341
- Portfolio port = (Portfolio)obj;
342
-
343
- if(!issue.equals(port.issue)){
344
- return false;
345
- }
346
- return true;
347
- }
348
- }
349
-

2

全体のコードの記載

2022/07/31 07:20

投稿

icebreak
icebreak

スコア4

test CHANGED
File without changes
test CHANGED
@@ -7,10 +7,11 @@
7
7
  その上で同じクラス内の別のメソッドfindPositionでその値を利用したいのですが、どのようにすれば利用することが可能でしょうか。
8
8
 
9
9
 
10
- 実際に書いたコードはこちらです
10
+ 実際に書いたコードはこちらです
11
+ また全部のコードも記載致します。
11
12
  ```java
12
13
  コード
13
- class Portfolio extend Position{
14
+ class Portfolio extends Position{
14
15
  static int count = 0;
15
16
  Position[] arrayposition = new Position[count +1];
16
17
 
@@ -26,14 +27,16 @@
26
27
  amount++;
27
28
  break;
28
29
  }
29
- else{
30
+ else if(bool == false){
31
+ if(i == count -1){
30
- arrayposition[count] = position;
32
+ arrayposition[count] = position;
31
- count++;
33
+ count++;
32
- }
34
+ }
33
- }
35
+ }
34
- }
36
+ }
35
- }
37
+ }
36
-
38
+ }
39
+ /*
37
40
  public Position findPosition(Issue4 issue){
38
41
  boolean bool2;
39
42
  for(int i = 0;i < count + 1;i++){
@@ -46,6 +49,7 @@
46
49
  }
47
50
  return null;
48
51
  }
52
+ */
49
53
  @Override
50
54
  public int hashCode(){
51
55
  int result = 1;
@@ -71,4 +75,275 @@
71
75
  return true;
72
76
  }
73
77
  }
74
-
78
+ java
79
+ コード全体
80
+ import java.io.*;
81
+
82
+ class Issue100{
83
+ public static void main(String[] args){
84
+
85
+ Issue4 issue1 = new Issue4("1111","AAAA");
86
+ Issue4 issue2 = new Issue4("2222","BBBB");
87
+ Issue4 issue3 = new Issue4("3333","CCCC");
88
+ Issue4 issue4 = new Issue4("4444","DDDD");
89
+ Issue4 issue5 = new Issue4("5555","EEEE");
90
+ Issue4 issue6 = new Issue4("6666","FFFF");
91
+ Issue4 issue7 = new Issue4("1111","AAAA");
92
+
93
+ Stock stock1 = new Stock("1111","AAAA",Market.TSE);
94
+ Stock stock2 = new Stock("2222","BBBB",Market.OSE);
95
+ Stock stock3 = new Stock("3333","CCCC",Market.NSE);
96
+ Bond bon1 = new Bond("1111","AAAA",20001111,1.0);
97
+ Bond bon2 = new Bond("2222","BBBB",20002222,1.5);
98
+ Bond bon3 = new Bond("3333","CCCC",20003333,2.0);
99
+
100
+
101
+ Position pos1 = new Position(issue1,0);
102
+ Position pos2 = new Position(issue2,0);
103
+ Position pos3 = new Position(issue3,0);
104
+ Position pos4 = new Position(issue4,0);
105
+ Position pos5 = new Position(issue5,0);
106
+ Position pos6 = new Position(issue6,0);
107
+
108
+ Portfolio por = new Portfolio();
109
+
110
+ por.addPosition(pos1);
111
+ por.addPosition(pos2);
112
+ /*
113
+ Position a = por.findPosition(issue7);
114
+ System.out.println(a);
115
+ */
116
+ }
117
+ }
118
+ class Issue4{
119
+
120
+ protected String code;
121
+ protected String name;
122
+
123
+ public Issue4(String code,String name)throws IllegalArgumentException{
124
+ if(code == null || name == null){
125
+ IllegalArgumentException e = new IllegalArgumentException();
126
+ throw e;
127
+ }
128
+ else{
129
+ this.code = code;
130
+ this.name = name;
131
+ }
132
+ }
133
+ public String getCode(){
134
+ return this.code;
135
+ }
136
+ public String getName(){
137
+ return this.name;
138
+ }
139
+ }
140
+
141
+ class Bond extends Issue4{
142
+
143
+ private int maturity;
144
+ private double coupon;
145
+
146
+ public Bond(String code,String name,int maturity,double coupon)throws IllegalArgumentException{
147
+ super(code,name);
148
+ if(maturity <= 20000101 || maturity >= 29991231){
149
+ IllegalArgumentException e = new IllegalArgumentException();
150
+ throw e;
151
+ }
152
+ else{
153
+ if(coupon < 0){
154
+ IllegalArgumentException e = new IllegalArgumentException();
155
+ throw e;
156
+ }
157
+ else{
158
+ this.maturity = maturity;
159
+ this.coupon = coupon;
160
+ }
161
+ }
162
+ }
163
+ public int getMaturity(){
164
+ return this.maturity;
165
+ }
166
+ public double getCoupon(){
167
+ return this.coupon;
168
+ }
169
+ public BondType getBondType(){
170
+ if(coupon == 0){
171
+ BondType bondtype = BondType.ZERO_COUPON_BOND;
172
+ return bondtype;
173
+ }
174
+ else{
175
+ BondType bondtype = BondType.COUPON_BOND;
176
+ return bondtype;
177
+ }
178
+ }
179
+ @Override
180
+ public String toString(){
181
+ return "コード :" + code + "\n" + "名称 :" + name + "\n" + "償還月日 :"+ this.maturity +
182
+ "\n" + "クーポンレート :" + this.coupon ;
183
+ }
184
+ @Override
185
+ public int hashCode(){
186
+ double result = 1;
187
+ result = 31*result + ((code == null)? 0:code.hashCode());
188
+ result = 31*result + this.maturity;
189
+ result = 31*result + this.coupon;
190
+ int result2 = (int)result;
191
+ return result2;
192
+ }
193
+ @Override
194
+ public boolean equals(Object obj){
195
+ if(this == obj){
196
+ return true;
197
+ }
198
+ if(obj == null){
199
+ return false;
200
+ }
201
+ if(getClass() != obj.getClass()){
202
+ return false;
203
+ }
204
+ Bond bond = (Bond)obj;
205
+
206
+ if(!code.equals(bond.code)){
207
+ return false;
208
+ }
209
+ if(maturity != bond.maturity){
210
+ return false;
211
+ }
212
+ if(coupon != bond.coupon){
213
+ return false;
214
+ }
215
+ return true;
216
+ }
217
+ }
218
+ class Stock extends Issue4{
219
+ private Market market;
220
+
221
+ public Stock(String code,String name,Market market){
222
+ super(code,name);
223
+ this.market = market;
224
+ }
225
+ public Market getMarket(){
226
+ return this.market;
227
+ }
228
+ @Override
229
+ public String toString(){
230
+ return "コード :" + code + "\n" + "名称 :" + name + "\n" + "上場市場 :"+ this.market;
231
+ }
232
+ @Override
233
+ public int hashCode(){
234
+ int result = 1;
235
+ result = 31*result + ((code == null)? 0:code.hashCode());
236
+ result = 31*result + ((this.market == null)? 0:market.hashCode());
237
+ return result;
238
+ }
239
+ @Override
240
+ public boolean equals(Object obj){
241
+ if(this == obj){
242
+ return true;
243
+ }
244
+ if(obj == null){
245
+ return false;
246
+ }
247
+ if(getClass() != obj.getClass()){
248
+ return false;
249
+ }
250
+ Stock stock = (Stock)obj;
251
+
252
+ if(!code.equals(stock.code)){
253
+ return false;
254
+ }
255
+ if(market != stock.market){
256
+ return false;
257
+ }
258
+ return true;
259
+ }
260
+ }
261
+ class Position{
262
+
263
+ protected Issue4 issue;
264
+ protected double amount;
265
+
266
+ public Position(){
267
+ }
268
+ public Position(Issue4 issue,double amount){
269
+ this.issue = issue;
270
+ this.amount = amount;
271
+ }
272
+ public Issue4 getIssue4(){
273
+ return this.issue;
274
+ }
275
+ public double getAmount(){
276
+ return this.amount;
277
+ }
278
+ public void setIssue4(Issue4 issue){
279
+ this.issue = issue;
280
+ }
281
+ public void setAmount(double amount){
282
+ this.amount = amount;
283
+ }
284
+ }
285
+ class Portfolio extends Position{
286
+ static int count = 0;
287
+ Position[] arrayposition = new Position[count +1];
288
+
289
+ public void addPosition(Position position){
290
+ if(count == 0){
291
+ arrayposition[count] = position;
292
+ count++;
293
+ }
294
+ else if(count > 0){
295
+ for(int i = 0; count > i;i++){
296
+ boolean bool = position.equals(arrayposition[i]);
297
+ if(bool == true){
298
+ amount++;
299
+ break;
300
+ }
301
+ else if(bool == false){
302
+ if(i == count -1){
303
+ arrayposition[count] = position;
304
+ count++;
305
+ }
306
+ }
307
+ }
308
+ }
309
+ }
310
+ /*
311
+ public Position findPosition(Issue4 issue){
312
+ boolean bool2;
313
+ for(int i = 0;i < count + 1;i++){
314
+ bool2 = issue.equals(arrayposition[i]);
315
+ if(bool2 == true){
316
+ return arrayposition[i];
317
+ }
318
+ else{
319
+ }
320
+ }
321
+ return null;
322
+ }
323
+ */
324
+ @Override
325
+ public int hashCode(){
326
+ int result = 1;
327
+ result = 31*result + ((issue == null)? 0:issue.hashCode());
328
+ return result;
329
+ }
330
+ @Override
331
+ public boolean equals(Object obj){
332
+ if(this == obj){
333
+ return true;
334
+ }
335
+ if(obj == null){
336
+ return false;
337
+ }
338
+ if(getClass() != obj.getClass()){
339
+ return false;
340
+ }
341
+ Portfolio port = (Portfolio)obj;
342
+
343
+ if(!issue.equals(port.issue)){
344
+ return false;
345
+ }
346
+ return true;
347
+ }
348
+ }
349
+

1

マークダウン法で記載致しました。

2022/07/31 07:13

投稿

icebreak
icebreak

スコア4

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提
2
2
 
3
- クラスの先頭で配列あるメソッドの中で配列
3
+ クラスの先頭で配列
4
4
  static int count = 0;
5
5
  Position[] arrayposition = new Position[count +1];
6
6
  を宣言し、それをメソッドaddPosition()で配列の値を定義しました。
@@ -8,6 +8,8 @@
8
8
 
9
9
 
10
10
  実際に書いたコードはこちらです
11
+ ```java
12
+ コード
11
13
  class Portfolio extend Position{
12
14
  static int count = 0;
13
15
  Position[] arrayposition = new Position[count +1];