Answer the following for the XYCoord class given below. (20 pts.) (a) Give a sta
ID: 3800341 • Letter: A
Question
Answer the following for the XYCoord class given below. (20 pts.)
(a) Give a statement to create a new XYCoord object, coord1, initialized to its default value. (1pt)
(b) Give a statement to create a new XYCoord object, coord2, to the value (20,40). (1 pt.)
(c) Add a copy constructor to the XYCoord class below where indicated. (1 pts.)
(d) Create a new XYCoord object named coord4 that has the same value as an XYCoord
object named coord3. (2 pts)
(e) Add getter and setter methods to the XYCoord class below where indicated. (4 pts.)
(f) Complete the toString and equals methods in the XYCoord class where indicated. (4 pts.)
(g) Complete a method named scale to the XYCoord class where indicated that allows a scaling
factor to be given, and returns a new XYCoord object with both the x and y values multiplied by
the provided scaling factor. (3 pts.)
(h) Give a section of code that, for objects xcoord1 and xcoord2, displays “are equal” if they
are equal, and “not equal” otherwise. (2 pts.)
(i) Give the code to display the following, (2 pts.)
The value of coord1 is (x, y) - for the x and y values of object coord1
public class XYCoord
{
private int x;
private int y;
public XYCoord()
{
x = 0;
y = 0;
}
public XYCoord(int a, int b)
{
x = a;
y = b;
}
// add a copy constructor (set to value of another XYCoord object)
// add getters/setters (accessors/mutators)
// complete to return a string of the form “(x, y)”
public String toString()
{
}
//complete
public boolean equals( ):
{
}
//complete scale method
Explanation / Answer
Hi, please find my implementation and answer of all questions.
Please let me know in case of any issue.
Please read all comments.
public class XYCoord
{
private int x;
private int y;
public XYCoord()
{
x = 0;
y = 0;
}
public XYCoord(int a, int b)
{
x = a;
y = b;
}
// add a copy constructor (set to value of another XYCoord object)
public XYCoord(XYCoord other){
x = other.x;
y = other.y;
}
// add getters/setters (accessors/mutators)
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
// complete to return a string of the form “(x, y)”
public String toString()
{
return "("+x+","+y+")";
}
//complete
public boolean equals(Object obj)
{
if(obj instanceof XYCoord){
XYCoord t = (XYCoord)obj;
if(t.x == x && t.y == y)
return true;
}
return false;
}
//complete scale method
public XYCoord scale(int f){
XYCoord newObj = new XYCoord(x*f, y*f);
return newObj;
}
public static void main(String[] args) {
//a
XYCoord coord1 = new XYCoord();
//b
XYCoord coord2 = new XYCoord(20, 40);
//c
//XYCoord coord4 = new XYCoord(coord3)
//h
/*
if(xcoord1.equals(xcoord2))
System.out.println("are equal");
else
System.out.println("not equal");
*/
//i
System.out.println(coord1);
}
}