#いくつかの図形の面積を表すサブクラスで別のクラスにインスタンスを使ってstackの順とqueueの順で実行したいです
#理想のコンソールの例
The contents of figures1 are as follows:
I am a rectangle with area200.0.
I am a rectangle with area600.0.
I am a Circle1 with area1256.6370614359173.
I am a Circle1 with area2827.4333882308138.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Square with area0.0.
I am a Square with area900.0.
The contents of MyQueue are as follows:
I am a rectangle with area200.0.
I am a rectangle with area600.0.
I am a Circle1 with area1256.6370614359173.
I am a Circle1 with area2827.4333882308138.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
The contents of MyStack are as follows:
I am a Square with area900.0.
I am a Square with area0.0.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Circle1 with area2827.4333882308138.
I am a Circle1 with area1256.6370614359173.
I am a rectangle with area600.0.
I am a rectangle with area200.0.
#実際に実行すると
The contents of figures1 are as follows:
I am a rectangle with area200.0.
I am a rectangle with area600.0.
I am a Circle1 with area1256.6370614359173.
I am a Circle1 with area2827.4333882308138.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Square with area0.0.
I am a Square with area900.0.
The contents of MyQueue are as follows:
I am a rectangle with area200.0.
I am a rectangle with area600.0.
I am a Circle1 with area1256.6370614359173.
I am a Circle1 with area2827.4333882308138.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
I am a Triangle with area0.0.
The contents of MyStack are as follows:
I am a Square with area900.0.
I am a Square with area0.0.
**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 8
at object.MyStack.pop(MyStack.java:35)
at object.FiguresQS.main(FiguresQS.java:64)
**
のようになり、stackのコードが実行されません。
#考えられる問題
コンソールに表示してあるようにクラスMyStackの35行目とFiguresQSの64行目に何かしらの問題が生じているようです。
#該当するソースコード
Java
1package object; 2 3public class FiguresQS { 4 5 public static void main(String[] args) { 6 Figure[] figures1 = new Figure[9]; 7 8 int i = 0; 9 Rectangle r1 = new Rectangle(); 10 Rectangle r2 = new Rectangle(30, 20); 11 figures1[i++] = r1; 12 figures1[i++] = r2; 13 14 Circle1 c1 = new Circle1(); 15 Circle1 c2 = new Circle1(30); 16 figures1[i++] = c1; 17 figures1[i++] = c2; 18 19 Triangle t1 = new Triangle(); 20 Triangle t2 = new Triangle(30, 20); 21 Triangle t3 = new Triangle(300, 200); 22 figures1[i++] = t1; 23 figures1[i++] = t2; 24 figures1[i++] = t3; 25 26 Square1 s1 = new Square1(); 27 Square1 s2 = new Square1(20); 28 figures1[i++] = s1; 29 figures1[i++] = s2; 30 31 System.out.println("The contents of figures1 are as follows:"); 32 for (int j=0; j<figures1.length; j++) { 33 figures1[j].identify(); 34 } 35 36 System.out.println(); 37 MyQueue queue = new MyQueue(7); 38 int k = 0; 39 try{ 40 while(queue.isFull() == false) { 41 queue.put(figures1[k]); 42 k++; 43 } 44 } catch (ArrayIndexOutOfBoundsException e) { 45 } 46 System.out.println("The contents of MyQueue are as follows:"); 47 for(int j = 0;j<7; j++){ 48 queue.get().identify(); 49 } 50 ///Stackの実装 51 System.out.println(); 52 MyStack stack = new MyStack(8); 53 try { 54 while(stack.isFull() == false) { 55 stack.push(figures1[k]); 56 k++; 57 } 58 } catch (ArrayIndexOutOfBoundsException e) { 59 } 60 ///printnできるコードの数が限られているように思える 61 System.out.println("The contents of MyStack are as follows:"); 62 for(int j=0; j<8; j++) { 63 ///下コードに問題がある///配列の長さに原因があると思われる 64 stack.pop().identify(); 65 } 66 } 67} 68
#問題があると思われるMyStackクラス
Java
1package object; 2 3public class MyStack { 4 private Figure[] Buff; 5 private int sp; 6 private int count; 7 Figure x; 8 public MyStack(int size) { 9 Buff = new Figure[size]; 10 sp = 0; 11 count = 0; 12 } 13 public boolean isEmpty() { 14 if(count==0) { 15 return true; 16 }else { 17 return false; 18 } 19 } 20 public boolean isFull() { 21 if(count >= Buff.length) { 22 return true; 23 }else { 24 return false; 25 } 26 } 27 public void push(Figure x) { 28 Buff[sp] = x; 29 sp++; 30 count++; 31 } 32 public Figure pop() { 33 sp--; 34 ///このコードに問題がある 35 x = Buff[sp]; 36 count--; 37 return(x); 38 } 39} 40 41
#Rectangleのコード
Java
1 package object; 2 3import java.util.concurrent.atomic.AtomicInteger; 4 5public class Rectangle extends Figure { 6 7 private double height = 0; 8 private double width = 0; 9 10///当初インスタンスに名前と番号を付けようと思ったのですがうまく行っていません 11 static final String NAME = Rectangle.class.getSimpleName(); 12 static final java.util.concurrent.atomic.AtomicInteger counter = new AtomicInteger(0); 13 14 15 Rectangle(){ 16 this.width = 20; 17 this.height = 10; 18 } 19 Rectangle(double w, double h){ 20 this.width = w; 21 this.height = h; 22 } 23 void setWidth(double w) { 24 this.width = w; 25 } 26 void setHeight(double h) { 27 this.height = h; 28 } 29 void setSize(double w, double h) { 30 setWidth(w); 31 setHeight(h); 32 } 33 double getWidth() { 34 return this.width; 35 } 36 double getHeight() { 37 return this.height; 38 } 39 40 Rectangle(int width, int height){ 41 super(NAME + counter.addAndGet(1)); 42 this.width = width; 43 this.height = height; 44 } 45 46 @Override 47 double getArea() { 48 return width * height; 49 } 50 void identify() { 51 System.out.println("I am a rectangle with area" + getArea() + "."); 52 } 53 @Override 54 protected void getName() { 55 // TODO 自動生成されたメソッド・スタブ 56 57 } 58} 59
#MiQueueのクラス
Java
1package object; 2 3public class MyQueue { 4 private Figure[]Buff; 5 private int start; 6 private int end; 7 private int count; 8 Figure x; 9 //constructor 10 public MyQueue(int size) { 11 Buff = new Figure[size]; 12 start = 0; 13 count = 0; 14 } 15 //check method 16 public boolean isEmpty() { 17 if (count==0) { 18 return true; 19 }else { 20 return false; 21 } 22 } 23 public boolean isFull() { 24 if(count > Buff.length) { 25 return true; 26 }else { 27 return false; 28 } 29 } 30 //put method 31 public void put(Figure x) { 32 end = start + count; 33 Buff[end] = x; 34 count++; 35 } 36 //get method 37 public Figure get() { 38 x = Buff[start++]; 39 count--; 40 return(x); 41 } 42}
#自分で試したこと
配列の長さに問題があるのかと思い、
MyQueue queue = new MyQueue(7);
の()の中の数値を変えてみたり、同様に
MyStack stack = new MyStack(8); や
Figure[] figures1 = new Figure[9];
も同様に数値を変えてみました。
コンソールMyStackの表示される個数がこれ以上増やすことができず、解決方法がおもいつかなくなりました。
どうすればstackが実行されるのかご存知の方、教えていただけたら幸いです。
また、figures1はFigures1[i++]にいれた図形の情報が全て事項できているのに対し、queueではSquare以降が表示されない理由についてわかる方がいらっしゃいましたら、教えていただけると幸いです。
回答1件
あなたの回答
tips
プレビュー