8A
Complete the class XRectangle
. An XRectangle
has a x,y location of the upper left hand corner. It represents a rectangle of X's with 4 rows and 5 columns in random colors
You are given a class X
which represents a graphical X and that can draw itself at a specified location in a specified color. You can look at its documentation to see the details. Open the X class and select "Documentation" in the upper right drop down menu.
Provide a constructor that takes the x, y coordinates of the upper left hand corner as parameters. Do not draw in the constructor
Provide the method
draw()
which draws 4 rows each containing 5 X objects. Each X is drawn in a random color either red, blue, green or black. You will write and call 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 to 4 and use the constants defined in the XRectangle
class to determine the color. For example if the random number is RED (0) the method will return Color.REDOther instructions
X
class. The numbers 20 and 15 should not appear in your code.Your output for the final will look like this.
For the draft, complete the constructor. Implement the draw method to draw the first column all in red. You must use a loop.
8B
Write an application CountryList
. It will have a main method. In the main method, do the following:
Note: There is a version of remove method that will remove a specific object. Use that. Do not use the index. Here is the code to remove target from the ArrayList called stuff
stuff.remove(target);
Note: To print an ArrayList in the [xxx, yyy, ...] format, use its toString method like this
System.out.println(stuff.toString());
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.
8C
Write a class Bookstore
which manages Book
objects.
The Book
class is provided for you. It has a constructor that takes the title of book and its price as parameters. It has methods to getPrice
and getTitle
. Copy the Book
class into your project. Do not change it in any way.
A Bookstore
has an instance variable ArrayList<Book>
. Call the instance variable books. It has a no-argument constructor (That means it takes no parameters) which initializes the instance variable (the ArrayList of Book objects) to an empty ArrayList.
Provide these methods
public void add(Book b)
which adds the Book to the Bookstore
public void swap(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 String cheapest()
gets the title for the Book
with the lowest price. If more than one Book
has the same price, return the title of the first. If the Bookstore
object is empty, return the empty stringpublic String toString()
gets a string representation of the Books
in the Bookstore
- provided. (This is why the instance variable must be called books.)Provide Javadoc.
For the draft, define the instance variable and implement the constructor. Also implement the add method.