0
class Use {
public static void main(String[] args) throws IOException {
    Use u = new Use();
    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    ArrayList<Square> sq = new ArrayList <Square>(); 
    int shapeChoice = 0;
    int customChoice = 0;
    int count = 0;
    String info1;
    boolean boolCase;
    System.out.println("Welcome, this program allows you to create and access    shapes, displaying the area and perimeter of each.");
    while (true){
        shapeChoice = Integer.parseInt(br.readLine());
        if (shapeChoice == 0) break;
        count++;
        sq.add(new Square(count, shapeChoice));
        String info = (sq.get(count-1).toString());
        System.out.println(info);
        if (count > 1) {
            info1 = (sq.get(count - 1).toString());
        System.out.println(info1);
        }
    }
    /*System.out.println("Enter Square Num");
    customChoice = Integer.parseInt(br.readLine());
    String info1 = (sq.get(customChoice - 1).toString());
    System.out.println(info1);*/
    for(int x = sq.size(); x > 0; x--){
        System.out.println(x);
        String info = (sq.get(x-1).toString());
        System.out.println(info);
    }       
}
}

abstract public class Test {
private static int s; //Base top
public Test(){
    s = 1;
}
Test(int s){
    this.s = s;
}
abstract public int findPerimeter();
abstract public int findArea();
public int getS() {
    return s;
}
public void setS(int s){
    this.s = s;
}

}

class Square extends Test{
private static int numSquares = 0;
private int iD;
public Square(int iD){
    super();
    numSquares ++;
}
public Square(int s, int iD){
    super(s);
    numSquares ++;
}
public int getID(){
    return iD;
}
public void setID(int iD){
    this.iD = iD;
}
public int getNumSquares(){
    return numSquares;
}
@Override
public int findArea(){
    return (super.getS()*super.getS());
}
@Override
public int findPerimeter() {
    return (super.getS())*4; 
}
public String toString(){
    return "Area is "+findArea()+" and Perimeter is "+findPerimeter();
}
public void decreaseNum(){
    numSquares --;
}
public int getS(){
    return super.getS();
}

}

My Test class creates a square, and I was using the main to create new squares, but when I output the info on the squares using the toString() method, it displays only the most recently created object's values.

How would I be able to fix that?

thanks a lot for reading!

4

2 回答 2

0

This is probably because you are using a static variable in the Test class to store what I am guessing is a non-static variable "s" which is meant to be the the side. The best way to check that you are actually getting back different objects is to append the Object.hashcode to the toString or to not override the toString method at all.

于 2013-04-24T04:13:44.307 回答
0
 if (count > 1) {
            info1 = (sq.get(count - 1).toString());
 System.out.println(info1);

This line prints only the last created Square. Maybe you wanted to loop through the array?

Edit :

You loop through it here

for(int x = sq.size(); x > 0; x--){
        System.out.println(x);
        String info = (sq.get(x-1).toString());
        System.out.println(info);
    }  

but this piece of code is never executed because you are in an infinite loop with while (true)

于 2013-04-24T04:15:36.120 回答