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

Hi, So I have a class that is suppose to draw a random square and a random circl

ID: 3645819 • Letter: H

Question

Hi,

So I have a class that is suppose to draw a random square and a random circle of random size and position. As of right now I can draw the circle and square size and position randomly but I cannot draw a random number of them (at least up to 10). I need to create an array that will allow me to do so, what I have is below.

To restate the above:
(Right now the code produces 1 square (Random size and random Shape) and 1 circle (Random size and random Shape), however it needs to draw a random number of them using an array. This I do not know how to do.)


import java.awt.*;


public class RandomShapes {

public void randomArray(Shape[] shape)
{
for(int i=1; i<shape.length; i++)
{
if( i >= 1)
{
x = (int) (200 * Math.random()) + BUTTON_WIDTH * 2;
y = (int) (200 * Math.random()) + BUTTON_HEIGHT * 2;
xA = (int) (100 * Math.random()) + BUTTON_WIDTH * 2;
yA = (int) (100 * Math.random()) + BUTTON_WIDTH * 2;
sizeS = (int) (100 * Math.random());
sizeC = (int) (200 * Math.random());
squareShape = new Square(x, y, sizeS);
circleShape = new Circle(xA, yA, sizeC);
}
}
}
}

Explanation / Answer

public class RandomShapes {

public void randomArray(Shape[] shape)
{
for(int i=1; i<shape.length; i++)
{
if( i >= 1)
{
x = (int) (200 * Math.random()) + BUTTON_WIDTH * 2;
y = (int) (200 * Math.random()) + BUTTON_HEIGHT * 2;
xA = (int) (100 * Math.random()) + BUTTON_WIDTH * 2;
yA = (int) (100 * Math.random()) + BUTTON_WIDTH * 2;
sizeS = (int) (100 * Math.random());
sizeC = (int) (200 * Math.random());
squareShape = new Square(x, y, sizeS);
circleShape = new Circle(xA, yA, sizeC);
}
}
}

public static void main(String[] args)

{

shape[] shapearray = new shape[10];

randomArray(shapearray);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote