質問するログイン新規登録

質問編集履歴

4

情報の追記

2022/07/31 07:59

投稿

icebreak
icebreak

スコア4

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,12 @@
6
6
  を宣言し、それをメソッドaddPosition()で配列の値を定義しました。
7
7
  その上で同じクラス内の別のメソッドfindPositionでその値を利用したいのですが、どのようにすれば利用することが可能でしょうか。
8
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)
9
13
 
14
+
10
15
  実際に書いたコードはこちらです。
11
16
  また全部のコードも記載致します。
12
17
  ```java

3

誤字の修正

2022/07/31 07:21

投稿

icebreak
icebreak

スコア4

title CHANGED
File without changes
body CHANGED
@@ -36,7 +36,7 @@
36
36
  }
37
37
  }
38
38
  }
39
- /*
39
+
40
40
  public Position findPosition(Issue4 issue){
41
41
  boolean bool2;
42
42
  for(int i = 0;i < count + 1;i++){
@@ -49,7 +49,7 @@
49
49
  }
50
50
  return null;
51
51
  }
52
- */
52
+
53
53
  @Override
54
54
  public int hashCode(){
55
55
  int result = 1;

2

全体のコードの記載

2022/07/31 07:20

投稿

icebreak
icebreak

スコア4

title CHANGED
File without changes
body 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,287 @@
26
27
  amount++;
27
28
  break;
28
29
  }
30
+ else if(bool == false){
31
+ if(i == count -1){
32
+ arrayposition[count] = position;
33
+ count++;
34
+ }
35
+ }
36
+ }
37
+ }
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
+ }
29
47
  else{
30
- arrayposition[count] = position;
31
- count++;
32
48
  }
33
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;
34
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;
35
76
  }
77
+ }
78
+ java
79
+ コード全体
80
+ import java.io.*;
36
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
+ /*
37
311
  public Position findPosition(Issue4 issue){
38
312
  boolean bool2;
39
313
  for(int i = 0;i < count + 1;i++){
@@ -46,6 +320,7 @@
46
320
  }
47
321
  return null;
48
322
  }
323
+ */
49
324
  @Override
50
325
  public int hashCode(){
51
326
  int result = 1;

1

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

2022/07/31 07:13

投稿

icebreak
icebreak

スコア4

title CHANGED
File without changes
body 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];