jane doe - 1 month ago 15

Java Question

I have a project where I have to fill a 600x400 window (JavaFX) with 30 random sized circles with no filling. The largest circle must be filled with a translucent red (and if there are multiple large circles with the same radius only one can be filled). I'm able to get all the circles on the screen fine. My problem is getting the largest circle to be red. I haven't been taught arrays which were used in almost all of my many google searches. I cant figure out how exactly to track the largest circle. His hint to us is : "When it comes to keeping track of the largest circle, remember that two reference variables can point to the same Circle object. Maintain a separate Circle reference variable that always points to the largest circle (so far created). You may want to initialize this variable to a circle that has a radius of 0. You can get the radius of a circle using the getRadius method." I created a circle object and a largestCircle object but don't understand how to make the largestCircle object have the highest radius.

This is the code I have so far:

`{`

Random gen = new Random();

int x = 0;

int y = 0;

int radius = 0;

double largestRadius = Math.max(radius);

Circle largestCircle = null;

Group root = new Group();

//prints out 30 circles

for (int i = 0; i <= 30; i++)

{

Circle circle = new Circle(x, y, radius);

{

radius = gen.nextInt(66) + 10; //generates random radius from 10 to 75

x = gen.nextInt(600 - 2 * radius) + radius;

y = gen.nextInt(400 - 2 * radius) + radius;

}

if (circle.getRadius() == largestRadius)

{

largestCircle = circle;

largestCircle.setFill(Color.rgb(255, 0, 0, 0.3));

}

circle.setFill(null);

circle.setStroke(Color.rgb(gen.nextInt(256), + gen.nextInt(256), gen.nextInt(256)));

circle.setStrokeWidth(3);

root.getChildren().add(circle);

}

after I generate the random circles how to I find the max radius that was generated and set it to largestCircle? the highest radius a circle can be is 75, but sometimes none of the circles have a radius of 75. How do I set the max to be the highest number the program randomly generates?

Any help would be greatly appreciated! Thank you for your time

Answer

How about the following.

It has a two fixes.

-1, use `>`

and not `==`

when figuring if current circle is the largest.

-2, change the color of the largest circle at the end, after all the circles have been made... else you might make multiple circles red.

```
{
Random gen = new Random();
int x = 0;
int y = 0;
int radius = 0;
double largestRadius = Math.max(radius);
Circle largestCircle = null;
Group root = new Group();
//prints out 30 circles
for (int i = 0; i <= 30; i++)
{
Circle circle = new Circle(x, y, radius);
if (circle.getRadius() > largestRadius)
{
largestCircle = circle;
}
{
radius = gen.nextInt(66) + 10; //generates random radius from 10 to 75
x = gen.nextInt(600 - 2 * radius) + radius;
y = gen.nextInt(400 - 2 * radius) + radius;
}
circle.setFill(null);
circle.setStroke(Color.rgb(gen.nextInt(256), + gen.nextInt(256), gen.nextInt(256)));
circle.setStrokeWidth(3);
root.getChildren().add(circle);
}
largestCircle.setFill(Color.rgb(255, 0, 0, 0.3));
```