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!