Abstract methods could have been used in the GridItem class to make the code cleaner. Specifically, the getArea() method could have been declared as an abstract method since the actual implementation depends on the specific shape class. This would have enforced shape classes like MyCircle and MyRectangle to provide their own implementation of getArea() tailored to each shape's calculation of area.
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
1. 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 5) How could Abstract Methods have been used to make the code cleaner?
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
2. * 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
3. * 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
4. * 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 {
5. 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) {
6. 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
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[];
7. 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;
8. // 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);
}
}
9. // 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));
10. 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;
11. 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;
}
}