Note that there are some explanatory texts on larger screens.

plurals
  1. POIdentical Output of Two Objects from arraylist
    primarykey
    data
    text
    <pre><code>class Use { public static void main(String[] args) throws IOException { Use u = new Use(); BufferedReader br = new BufferedReader (new InputStreamReader(System.in)); ArrayList&lt;Square&gt; sq = new ArrayList &lt;Square&gt;(); 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 &gt; 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 &gt; 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; } </code></pre> <p>}</p> <pre><code>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(); } </code></pre> <p>}</p> <p>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.</p> <p>How would I be able to fix that?</p> <p>thanks a lot for reading!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload