Question
Please help with this GridItem assignment. Below is the prompt. I am in need of determining how to create the two methods required to be
Please help with this GridItem assignment.
Below is the prompt. I am in need of determining how to create the two methods required to be added.
In addition, how do you add the exception handling logic?
Code for GridWriter is provided at end. Please explain. Do not simply copy and paste from previous examples listed on site.
PROMPT
You will modify the GridWriter class by adding additional collection style functionality. The GridWriter class should get two new methods:
? public int size() should return the number of GridItems stored in the GridWriter ? public GridItem get(int index) should return the stored GridItems by index.
Consider the following code. The first line creates a GridWriter object. Then two items are added to the GridWriter. The index of the items will be 0, and 1. Notice how the for loop uses the size and get methods to print out the areas of the two items
GridWriter gw = new GridWriter(40, 50); gw.add(new MyCircle(10, 10, 9)); gw.add(new MyRectangle(40, 0, 10, 10));
for (int i = 0; i < gw.size(); i++)
{
System.out.println(gw.get(i).getArea());
}
Once you have these two methods working you should add exception logic to the get method. The following code should cause your GridWriter to thow an IndexOutOfBoundsException.
GridWriter gw = new GridWriter(40, 50);
gw.add(new MyCircle(10, 10, 9)); gw.add(new MyRectangle(40, 0, 10, 10));
GridItem i = gw.get(2);
Although the array inside the Gridwriter has a capacity of 4, it only stores two GridItems. 2 is not a valid index. Add a throws statement to your get method that will thow an IndexOutOfBoundsException for any invalid index.
Here is the GridWriter Code.
GridWriter.java
public class GridWriter
{
private GridItem items[];
private int size;
private int rows;
private int columns;
private static final int INITIAL_CAPACITY = 4;
/****
* Create a new GridWriter. It is initially empty. It has the capacity to store four GridItems before it
* will need to double its array size. The row and column arguments are used in the display method, to
* determine the size of the grid that is printed to standard output.
***/
public GridWriter(int r, int c) {
items = new GridItem[INITIAL_CAPACITY];
size = 0;
rows = r;
columns = c;
}
/****
* The GridWriter is a collection style class. It stores GridItems, and prints out a display grid.
* The add method provides a way to put GridItems into the GridWriter.
***/
public void add(GridItem item) {
// If the item array is full, we double its capacity
if (size == items.length) {
doubleItemCapacity();
}
// Store the item GridItem in the items array
items[size] = item;
// Increment size. Size counts the number of items
// currently stored in the GridWriter.
size++;
}
/****
* The display method prints a grid into standard output. The size of the grid is determined by the row and
* column values passed into the constructor
***/
public void display() {
int count;
// Loop through all rows
for (int r = rows; r >= 0; r--) {
// Loop through all columns
for (int c = 0; c < columns; c++) {
// Count the number of GridItems that cover this coordinate
count = 0;
for (int i = 0; i < size; i++) {
if (items[i].containsPoint(c, r)) {
count++;
}
}
// Print the count in the coordinate location. Or a dot if the count is 0
if (count == 0) {
System.out.print(" .");
} else {
System.out.print(" " + count);
}
}
// New line at the end of each row
System.out.println();
}
}
/****
* This is a private helper method that doubles the array capacity of the grid writer
* This allows it to accomodate a dynamic number of grid item objects
**/
private void doubleItemCapacity()
{
// allocate a new array with double capacity
GridItem temp[] = new GridItem[items.length * 2];
// Copy by hand, so to speak
for (int i = 0; i < items.length; i++) {
temp[i] = items[i];
}
// point the items array at the temp array.
// The old array will be garbage collected
items = temp;
}
/**HERE IS MY ADDITION, I FEEL THAT THIS IS SIMPLE ENOUGH BEING THAT IT DOES RETURN THE INTENDED TOPICS. HOWEVER, FOR THE SECOND GO AROUND, WHERE DOES THE EXCEPTION LOGIC GO OR HOW IS IT UTILIZED?
*/
public int size()
{
return size;
}
public GridItem get(int index)
{
return items[index];
}
}
ADDITIONAL CODE IF NECESSARY
GridItem.java
public class GridItem {
protected int x;
protected int y;
public int getX() {return x;}
public void setX(int value) {x = value;}
public int getY() {return y;}
public void setY(int value) {y = value;}
public double getArea() {
return 0;
}
public boolean containsPoint(int xValue, int yValue) {
return x == xValue && y == yValue;
}
}
Circle.java
public class MyCircle extends GridItem {
private int radius;
public MyCircle(int xValue, int yValue, int r) {
x = xValue;
y = yValue;
radius = r;
}
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public boolean containsPoint(int xValue, int yValue) {
double dx = x - xValue;
double dy = y - yValue;
double distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
return distance <= radius;
}
}
Rectangle.java
public class MyRectangle extends GridItem {
private int height;
private int width;
public MyRectangle(int xValue, int yValue, int w, int h) {
x = xValue;
y = yValue;
width = w;
height = h;
}
public double getArea() {
return height * width;
}
public boolean containsPoint(int xValue, int yValue) {
return xValue >= x &&
xValue <= x + width &&
yValue >= y &&
yValue <= y + height;
}
}
Please explain your logic!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started