Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have multiple classes, they are Circle, Square, ObjectList and ShapePictureDri

ID: 3814019 • Letter: I

Question

I have multiple classes, they are Circle, Square, ObjectList and ShapePictureDriver. I try to create 2 objects from Circle and 2 objects from Square. I create two methods draw() which output the shape to the console ( "[]" represents a square and "O" represents a circle). I want to print out the shape of the objects I added to ObjectList as shown in ShapePictureDriver. How to write the method toString() in ObjectList?

class ObjectList {
private Circle[] myCircles = new Circle[100];
private int numCircles;
private Square[] mySquares = new Square[100];
private int numSquares;
public void addCircle(Circle circle) {
myCircles[numCircles++] = circle;
}
public void addSquare(Square square) {
mySquares[numSquares++] = square;
}
public String toString() {
//hints:

//need to loop over Circles first
//then another loop over Squares
//this approach is nonscalable!
}
}

public class Square
{
private int x;
private int y;
String shape = "[]";
  
public Square(int nx, int ny){
x=nx;
y=ny;

}
public void draw(){
System.out.println(shape);
}
}

public class ShapesPictureDriver {

   public static void main(String[] args) {      
       Square firstSquare = new Square(3,4);
       Square secondSquare = new Square(10,20);

       Circle firstCircle = new Circle(1,5,3);
       Circle secondCircle = new Circle(2,10,6);

ObjectList picture = new ObjectList();
      
       picture.addSquare( firstSquare );
       picture.addSquare( secondSquare );
  
       picture.addCircle( firstCircle );
       picture.addCircle( secondCircle );
  
       System.out.println( "Drawing a Picture with Circles and Squares: " );
      
       System.out.println(picture.toString());
}

}

Explanation / Answer

I assume that similar to Square class, there will be a member String shape = "O"; in circle class(default membwer, not private), which shows the shape of the circle. Then, the toString method can be implemented as below..

class ObjectList {
private Circle[] myCircles = new Circle[100];
private int numCircles;
private Square[] mySquares = new Square[100];
private int numSquares;
public void addCircle(Circle circle) {
myCircles[numCircles++] = circle;
}
public void addSquare(Square square) {
mySquares[numSquares++] = square;
}
public String toString() {
       // as we have to return the string from this method,
       // we cannot use the draw() method in square or circle
       // class. because they don't return anything.
       // we need to loop on all objecs, and prepare the
       // output string ourselves.
  
       String s = "";
       s += "Printing Circles: ";
for(int i=0; i<numCircles; i++) {
           Circle c = myCircles[i];
           s += c.shape + " ";
       }
      
       s += "Printing Squares: ";
for(int i=0; i<numSquares; i++) {
           Square sq = mySquares[i];
           s += sq.shape + " ";
       }
      
       // return the result String
       return s;
}
}