8A
Complete the class ColoredCircles
. A ColoredCircles
represents a drawing of circles with 5 rows and 4 columns in random colors. It has a constructor that takes (x, y) location of the upper left hand corner of a set of circles and the diameter of the circles
Use the Ellipse class from the Horstmann graphics.
Provide a constructor that takes the (x, y) coordinates of the upper left hand corner and the diameter of the circles as parameters. Do not draw in the constructor
Provide the methods
draw()
which fills 5 rows each containing 4 circles. Use the constants given in ColoredCircles
. Each circle is filled with a random color either red, blue, green or black. You will write a getRandomColor
method to figure out the color to use. Iterate through the rows in the outer loop and through the columns in the inner loop. That means you will draw the first row, then the second, etc.getRandomColor()
gets a random color, either Color.RED, Color.BLUE, Color.GREEN, Color.BLACK. You are provided with a Random
object. Use it and do not change the seed. Generate a random integer 0, 1, 2, or 3 and use the constants defined for you to determine the color. For example if the random number is RED (0) the method will return Color.REDOther instructions
Your output for the final will look like this.
For the draft, complete the constructor. Implement the draw method to draw the first row all in red. This is essentially the inner loop. You must use a loop.
8B
Create an application called DogList
with a main method. In the main method, do the following
ArrayList
of Strings
Note: You must use an ArrayList
and its methods. Do not fake it by just printing the correct strings. You will need to know how to use these methods in a situation where the output can not be faked.
Note1: There is a version of remove method that will remove a specific object. Use that. Here is the code to remove target from the ArrayList called stuff
stuff.remove(target);
Note2: To print an ArrayList in the [xxx, yyy, ...] format, use its toString method like this
System.out.println(stuff.toString());
ArrayList
of Strings
8C
Write an EllipseManager
class which manages Ellipse
objects.
Use the Ellipse
class from the Horstmann graphics library.
The EllipseManager
has an instance variable ArrayList<Ellipse>
. Call the instance variable list. It has a no-argument constructor (That means it takes no parameters) which initializes an ArrayList
of Ellipses
to an empty ArrayList
.
Provide these methods
public void add(Ellipse e)
adds this Ellipse
to the EllipseManager
(adds it to the ArrayList
instance variable)public void exchange(int index1, int index2)
- swaps the element at index1 with the element at index2. If either index is out of bounds, do not changing anything.public Ellipse largest()
gets the Ellipse
with the largest area. If more than one Ellipse
has the same area, return the first. If the EllipseManager is empty, return null. The image below shows the formula for the area of an Ellipse
ArrayList
- provided. (This is why the instance variable must be called list.)From Google.com:
Provide Javadoc
For the draft, define the instance variable and implement the constructor. Also implement the add method.