SlideShare a Scribd company logo
1 of 11
Download to read offline
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
* 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
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;
}
}

More Related Content

Similar to Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf

Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6sotlsoc
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.pptDeepVala5
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 
Network vs. Code Metrics to Predict Defects: A Replication Study
Network vs. Code Metrics  to Predict Defects: A Replication StudyNetwork vs. Code Metrics  to Predict Defects: A Replication Study
Network vs. Code Metrics to Predict Defects: A Replication StudyKim Herzig
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsFALLEE31188
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptxNelyJay
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)Arun Umrao
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdffirstchoiceajmer
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
RIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSRIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSDominik Jungowski
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdfaravlitraders2012
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++vidyamittal
 

Similar to Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf (20)

Chapter 6.6
Chapter 6.6Chapter 6.6
Chapter 6.6
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Unit 1 Part - 3 constructor Overloading Static.ppt
Unit 1 Part - 3  constructor Overloading Static.pptUnit 1 Part - 3  constructor Overloading Static.ppt
Unit 1 Part - 3 constructor Overloading Static.ppt
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
 
Network vs. Code Metrics to Predict Defects: A Replication Study
Network vs. Code Metrics  to Predict Defects: A Replication StudyNetwork vs. Code Metrics  to Predict Defects: A Replication Study
Network vs. Code Metrics to Predict Defects: A Replication Study
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Parameters
ParametersParameters
Parameters
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Introduction to cpp (c++)
Introduction to cpp (c++)Introduction to cpp (c++)
Introduction to cpp (c++)
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
RIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JSRIA - Entwicklung mit Ext JS
RIA - Entwicklung mit Ext JS
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
 
Advance features of C++
Advance features of C++Advance features of C++
Advance features of C++
 

More from kamdinrossihoungma74

WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdfWHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdfkamdinrossihoungma74
 
What advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdfWhat advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdfkamdinrossihoungma74
 
What are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdfWhat are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdfkamdinrossihoungma74
 
Using Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdfUsing Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdfkamdinrossihoungma74
 
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdfUnlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdfkamdinrossihoungma74
 
True or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdfTrue or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdfkamdinrossihoungma74
 
To understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdfTo understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdfkamdinrossihoungma74
 
Think about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdfThink about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdfkamdinrossihoungma74
 
The probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdfThe probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdfkamdinrossihoungma74
 
Sally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdfSally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdfkamdinrossihoungma74
 
Short answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdfShort answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdfkamdinrossihoungma74
 
Rectangle class inherits members of Shape class Has one method calle.pdf
Rectangle class inherits members of Shape class  Has one method calle.pdfRectangle class inherits members of Shape class  Has one method calle.pdf
Rectangle class inherits members of Shape class Has one method calle.pdfkamdinrossihoungma74
 
Operating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdfOperating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdfkamdinrossihoungma74
 
Of the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdfOf the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdfkamdinrossihoungma74
 
Multiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdfMultiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdfkamdinrossihoungma74
 
Lets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdfLets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdfkamdinrossihoungma74
 
Let X be a random variable with cumulative distribution function F(a.pdf
Let X be a random variable with cumulative distribution function  F(a.pdfLet X be a random variable with cumulative distribution function  F(a.pdf
Let X be a random variable with cumulative distribution function F(a.pdfkamdinrossihoungma74
 
John and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdfJohn and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdfkamdinrossihoungma74
 
Java Question help needed In the program Fill the Add statements.pdf
Java Question  help needed In the program Fill the Add statements.pdfJava Question  help needed In the program Fill the Add statements.pdf
Java Question help needed In the program Fill the Add statements.pdfkamdinrossihoungma74
 
How can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdfHow can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdfkamdinrossihoungma74
 

More from kamdinrossihoungma74 (20)

WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdfWHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
 
What advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdfWhat advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdf
 
What are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdfWhat are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdf
 
Using Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdfUsing Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdf
 
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdfUnlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
 
True or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdfTrue or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdf
 
To understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdfTo understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdf
 
Think about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdfThink about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdf
 
The probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdfThe probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdf
 
Sally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdfSally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdf
 
Short answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdfShort answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdf
 
Rectangle class inherits members of Shape class Has one method calle.pdf
Rectangle class inherits members of Shape class  Has one method calle.pdfRectangle class inherits members of Shape class  Has one method calle.pdf
Rectangle class inherits members of Shape class Has one method calle.pdf
 
Operating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdfOperating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdf
 
Of the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdfOf the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdf
 
Multiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdfMultiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdf
 
Lets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdfLets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdf
 
Let X be a random variable with cumulative distribution function F(a.pdf
Let X be a random variable with cumulative distribution function  F(a.pdfLet X be a random variable with cumulative distribution function  F(a.pdf
Let X be a random variable with cumulative distribution function F(a.pdf
 
John and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdfJohn and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdf
 
Java Question help needed In the program Fill the Add statements.pdf
Java Question  help needed In the program Fill the Add statements.pdfJava Question  help needed In the program Fill the Add statements.pdf
Java Question help needed In the program Fill the Add statements.pdf
 
How can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdfHow can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdf
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
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; } }