質問編集履歴

3

MyQueueのクラスを追加しました。ご迷惑をおかけしております。

2021/01/07 08:22

投稿

shimajiro1917
shimajiro1917

スコア4

test CHANGED
File without changes
test CHANGED
@@ -492,7 +492,95 @@
492
492
 
493
493
  ```
494
494
 
495
-
495
+ #MiQueueのクラス
496
+
497
+ ```Java
498
+
499
+ package object;
500
+
501
+
502
+
503
+ public class MyQueue {
504
+
505
+ private Figure[]Buff;
506
+
507
+ private int start;
508
+
509
+ private int end;
510
+
511
+ private int count;
512
+
513
+ Figure x;
514
+
515
+ //constructor
516
+
517
+ public MyQueue(int size) {
518
+
519
+ Buff = new Figure[size];
520
+
521
+ start = 0;
522
+
523
+ count = 0;
524
+
525
+ }
526
+
527
+ //check method
528
+
529
+ public boolean isEmpty() {
530
+
531
+ if (count==0) {
532
+
533
+ return true;
534
+
535
+ }else {
536
+
537
+ return false;
538
+
539
+ }
540
+
541
+ }
542
+
543
+ public boolean isFull() {
544
+
545
+ if(count > Buff.length) {
546
+
547
+ return true;
548
+
549
+ }else {
550
+
551
+ return false;
552
+
553
+ }
554
+
555
+ }
556
+
557
+ //put method
558
+
559
+ public void put(Figure x) {
560
+
561
+ end = start + count;
562
+
563
+ Buff[end] = x;
564
+
565
+ count++;
566
+
567
+ }
568
+
569
+ //get method
570
+
571
+ public Figure get() {
572
+
573
+ x = Buff[start++];
574
+
575
+ count--;
576
+
577
+ return(x);
578
+
579
+ }
580
+
581
+ }
582
+
583
+ ```
496
584
 
497
585
  #自分で試したこと
498
586
 

2

Mystackのクラスを間違えて掲載していました。何度も修正してすみません。

2021/01/07 08:22

投稿

shimajiro1917
shimajiro1917

スコア4

test CHANGED
File without changes
test CHANGED
@@ -286,138 +286,84 @@
286
286
 
287
287
 
288
288
 
289
- public class FiguresQS {
290
-
291
-
292
-
293
- public static void main(String[] args) {
294
-
295
- Figure[] figures1 = new Figure[9];
296
-
297
-
298
-
299
- int i = 0;
300
-
301
- Rectangle r1 = new Rectangle();
302
-
303
- Rectangle r2 = new Rectangle(30, 20);
304
-
305
- figures1[i++] = r1;
306
-
307
- figures1[i++] = r2;
308
-
309
-
310
-
311
- Circle1 c1 = new Circle1();
312
-
313
- Circle1 c2 = new Circle1(30);
314
-
315
- figures1[i++] = c1;
316
-
317
- figures1[i++] = c2;
318
-
319
-
320
-
321
- Triangle t1 = new Triangle();
322
-
323
- Triangle t2 = new Triangle(30, 20);
324
-
325
- Triangle t3 = new Triangle(300, 200);
326
-
327
- figures1[i++] = t1;
328
-
329
- figures1[i++] = t2;
330
-
331
- figures1[i++] = t3;
332
-
333
-
334
-
335
- Square1 s1 = new Square1();
336
-
337
- Square1 s2 = new Square1(20);
338
-
339
- figures1[i++] = s1;
340
-
341
- figures1[i++] = s2;
342
-
343
-
344
-
345
- System.out.println("The contents of figures1 are as follows:");
346
-
347
- for (int j=0; j<figures1.length; j++) {
348
-
349
- figures1[j].identify();
350
-
351
- }
352
-
353
-
354
-
355
- System.out.println();
356
-
357
- MyQueue queue = new MyQueue(7);
358
-
359
- int k = 0;
360
-
361
- try{
362
-
363
- while(queue.isFull() == false) {
364
-
365
- queue.put(figures1[k]);
366
-
367
- k++;
368
-
369
- }
370
-
371
- } catch (ArrayIndexOutOfBoundsException e) {
372
-
373
- }
374
-
375
- System.out.println("The contents of MyQueue are as follows:");
376
-
377
- for(int j = 0;j<7; j++){
378
-
379
- queue.get().identify();
380
-
381
- }
382
-
383
- ///Stackの実装
384
-
385
- System.out.println();
386
-
387
- MyStack stack = new MyStack(8);
388
-
389
- try {
390
-
391
- while(stack.isFull() == false) {
392
-
393
- stack.push(figures1[k]);
394
-
395
- k++;
396
-
397
- }
398
-
399
- } catch (ArrayIndexOutOfBoundsException e) {
400
-
401
- }
402
-
403
- ///printnできるコードの数が限られているように思える
404
-
405
- System.out.println("The contents of MyStack are as follows:");
406
-
407
- for(int j=0; j<8; j++) {
408
-
409
- ///下コードに問題がある///配列の長さに原因があると思われる
410
-
411
- stack.pop().identify();
412
-
413
- }
414
-
415
- }
289
+ public class MyStack {
290
+
291
+ private Figure[] Buff;
292
+
293
+ private int sp;
294
+
295
+ private int count;
296
+
297
+ Figure x;
298
+
299
+ public MyStack(int size) {
300
+
301
+ Buff = new Figure[size];
302
+
303
+ sp = 0;
304
+
305
+ count = 0;
306
+
307
+ }
308
+
309
+ public boolean isEmpty() {
310
+
311
+ if(count==0) {
312
+
313
+ return true;
314
+
315
+ }else {
316
+
317
+ return false;
318
+
319
+ }
320
+
321
+ }
322
+
323
+ public boolean isFull() {
324
+
325
+ if(count >= Buff.length) {
326
+
327
+ return true;
328
+
329
+ }else {
330
+
331
+ return false;
332
+
333
+ }
334
+
335
+ }
336
+
337
+ public void push(Figure x) {
338
+
339
+ Buff[sp] = x;
340
+
341
+ sp++;
342
+
343
+ count++;
344
+
345
+ }
346
+
347
+ public Figure pop() {
348
+
349
+ sp--;
350
+
351
+ ///このコードに問題がある
352
+
353
+ x = Buff[sp];
354
+
355
+ count--;
356
+
357
+ return(x);
358
+
359
+ }
416
360
 
417
361
  }
418
362
 
419
363
 
420
364
 
365
+
366
+
421
367
  ```
422
368
 
423
369
 

1

不足していたコードがあったのでついかしました。未熟で理解不足の目立つコードで恐縮ですがアドバイスなどいただけたら幸いです。

2021/01/07 08:00

投稿

shimajiro1917
shimajiro1917

スコア4

test CHANGED
File without changes
test CHANGED
@@ -420,6 +420,134 @@
420
420
 
421
421
  ```
422
422
 
423
+
424
+
425
+ #Rectangleのコード
426
+
427
+ ```Java
428
+
429
+ package object;
430
+
431
+
432
+
433
+ import java.util.concurrent.atomic.AtomicInteger;
434
+
435
+
436
+
437
+ public class Rectangle extends Figure {
438
+
439
+
440
+
441
+ private double height = 0;
442
+
443
+ private double width = 0;
444
+
445
+
446
+
447
+ ///当初インスタンスに名前と番号を付けようと思ったのですがうまく行っていません
448
+
449
+ static final String NAME = Rectangle.class.getSimpleName();
450
+
451
+ static final java.util.concurrent.atomic.AtomicInteger counter = new AtomicInteger(0);
452
+
453
+
454
+
455
+
456
+
457
+ Rectangle(){
458
+
459
+ this.width = 20;
460
+
461
+ this.height = 10;
462
+
463
+ }
464
+
465
+ Rectangle(double w, double h){
466
+
467
+ this.width = w;
468
+
469
+ this.height = h;
470
+
471
+ }
472
+
473
+ void setWidth(double w) {
474
+
475
+ this.width = w;
476
+
477
+ }
478
+
479
+ void setHeight(double h) {
480
+
481
+ this.height = h;
482
+
483
+ }
484
+
485
+ void setSize(double w, double h) {
486
+
487
+ setWidth(w);
488
+
489
+ setHeight(h);
490
+
491
+ }
492
+
493
+ double getWidth() {
494
+
495
+ return this.width;
496
+
497
+ }
498
+
499
+ double getHeight() {
500
+
501
+ return this.height;
502
+
503
+ }
504
+
505
+
506
+
507
+ Rectangle(int width, int height){
508
+
509
+ super(NAME + counter.addAndGet(1));
510
+
511
+ this.width = width;
512
+
513
+ this.height = height;
514
+
515
+ }
516
+
517
+
518
+
519
+ @Override
520
+
521
+ double getArea() {
522
+
523
+ return width * height;
524
+
525
+ }
526
+
527
+ void identify() {
528
+
529
+ System.out.println("I am a rectangle with area" + getArea() + ".");
530
+
531
+ }
532
+
533
+ @Override
534
+
535
+ protected void getName() {
536
+
537
+ // TODO 自動生成されたメソッド・スタブ
538
+
539
+
540
+
541
+ }
542
+
543
+ }
544
+
545
+
546
+
547
+ ```
548
+
549
+
550
+
423
551
  #自分で試したこと
424
552
 
425
553
  配列の長さに問題があるのかと思い、