[Solved] Java Square Class [closed]


Change your loop into this:

for (int i=0; i<width; i++)
{
   for (int j=0; j<width; j++)
   { System.out.print("*"); }

   System.out.println();
}

As said, remove semicolons after loops because this makes your loop end immediately without arguments. i should be set to 1 so that it would reiterate in the correct number of times

And edit your getWidth() method into this:

int getWidth() {
    return width;
}

Using width*width would make the method return n^2 of the width. It’s supposed to be width not area.

Edit: I’m not sure if this is what you’re trying to do but try putting this inside Square class:

void draw() {
    for (int i=0; i<width; i++)
{
   for (int j=0; j<width; j++)
   { System.out.print("*"); }

   System.out.println();
}
}

Remove the getWidth() method completely. Then replace your getWidth() invocation with this:

square.draw();

5

solved Java Square Class [closed]