Part 1 Written Answers Download the GridWriterzip file and
Part 1 - Written Answers
Download the GridWriter.zip file and examine the classes. Carefully read through the code and note any statements that you do not understand. When you are comfortable with the code, answer the following questions. Submit your answers in a text file or document file.
Question 3) Put a System.out.println statement into each containsPoint method (GridItem, MyCircle, and MyRectangle). Run the program and examine the output. You will notice that GridItem’s containsPoint method is never called. Why is this?
Files from GridWriter.zip:
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;
}
}
-------------------------------------------------------------------------------------------------------------
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;
}
}
---------------------------------------------------------------------------------------------------------
public class GridWriterProgram {
public static void main(String[] args) {
GridWriter gw = new GridWriter(40, 50);
gw.add(new MyCircle(10, 10, 9));
gw.add(new MyCircle(25, 20, 12));
gw.add(new MyCircle(25, 20, 5));
gw.add(new MyRectangle(25, 25, 20, 15));
gw.add(new MyRectangle(5, 5, 3, 4));
gw.add(new MyRectangle(40, 0, 10, 10));
gw.display();
}
}
---------------------------------------------------------------------------------------------------------
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;
}
}
-----------------------------------------------------------------------------------------------------------------
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;
}
}
The GridWriter class is a collection type class similar to an ArrayList. It accepts shape objects that inherit from the superclass Gridltenm that inherit from the superclass Gridltem. Consider the main method below. It creates a GridWriter, adds six shapes, and then displays a public statie void-ain(String[] args) Gridwriter gw-new Gridwite: (40, 50) gw.ada (new MyCircle(10, 10, 9)) gw.add (new XyCircle (25, 20, 12) gw.ada (new MyCircle (25, 20, 5)) gw.add (new MyReetengle(25, 25, 2015)); gw.add (new MyRectangle(S, 5, 3, 4)); gw.add (new MyRectangle(40, 0, 10, 10)) gw.display ): The console output will look like this:Solution
Please follow the code and comments for descritpion :
CODE :
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;
}
}
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;
}
}
public class GridWriterProgram {
public static void main(String[] args) {
GridWriter gw = new GridWriter(40, 50);
gw.add(new MyCircle(10, 10, 9));
gw.add(new MyCircle(25, 20, 12));
gw.add(new MyCircle(25, 20, 5));
gw.add(new MyRectangle(25, 25, 20, 15));
gw.add(new MyRectangle(5, 5, 3, 4));
gw.add(new MyRectangle(40, 0, 10, 10));
gw.display();
}
}
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;
}
}
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;
}
}
OUTPUT :
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . . . 1 1 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 . . . .
. . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . . . . . . . . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . .
. . . . . . . . . . 1 . . . 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . . . 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . . 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . . 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 . . . . . . . . . . . . .
. . . 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . .
. . 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . .
. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . .
. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . .
. . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . .
. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 . . . . . . 1 . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . 1 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . . 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . . . . 1 1 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . . . . . 1 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 1 1 1 1 1 1
Description :
Please note that the System.out.println(super) not permitted. But the line System.out.println(this); is executable as the this.toString() is called and printed automatically.
this refers to your current object. super refers to the super class, the class your current object directly inherits from So System.out.println(this) prints your object\'s toString() method, but System.out.println(super) fails because super is NOT an object (and thus has no toString() method).
So the call for the super class in the gridItem is called but could not be printed over the course of the code execution.
Hope this is helpful.









