Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please assist on Java assignment. The Task: GridWriter Class : You will modify the GridWriter class by adding additional collection style functionality. The GridWriter class

image text in transcribed

Please assist on Java assignment.

The Task:

GridWriter Class: You will modify the GridWriter class by adding additional collection style functionality. The GridWriter class should get two new methods:

1. public int size() should return the number of GridItems stored in the GridWriter

My question is, how does this code look like and how'd you solve this?

2. public GridItem get(int index) should return the stored GridItems by index.

My question is how do you write this code and explain how you got it.

The code of use would then be the following to test the new methods:

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());

}

For the rest of the task, it is the following:

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 code for this class.

Please let me know how to insert the size and get methods as described above.

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

// Count the number of GridItems that cover this coordinate

count = 0;

for (int i = 0; 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

temp[i] = items[i];

}

// point the items array at the temp array.

// The old array will be garbage collected

items = temp;

}

}

Here is the GridItem Class.

public class GridItem protected int x; protected int y; public int getX) freturn x;} public void setX(int value) ix -value;J public int getYO freturn y;} public void setYCint value) ty-value;] public double getAreaO I return 0; public boolean containsPoint(int xValue, int yValue) xValue && y yalue; return x

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago