Answer the following for the XYCoord class given below 20 pt
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
ALREADY HAVE QUESTION NUMBER ONE COMPLETED
2. Create a subclass of the XYCoord class from question 1, called ColorXYCoord, that stores a color value
(as a string, e.g., “red”). INCLUDE ONLY (a) the needed instances variables, (b) a constructor that takes as
parameters x and y values and a color (as a String), and (c) an appropriate toString method that returns a
the value of an XYCoord object in the form \"(10, 20) red\". (10 pts.)
Solution
import java.util.*;
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 coord)
{
x = coord.x;
y = coord.y;
}
// add getters/setters (accessors/mutators)
public void setX(int x)
{
this.x = x;
}
public int getX()
{
return x;
}
public void setY(int y)
{
this.y = y;
}
public int getY()
{
return y;
}
public String toString()
{
return \"(\"+getX() +\",\"+getY()+\")\";
}
//complete
public boolean equals(XYCoord obj)
{
if(x == obj.x && y == obj.y)
return true;
else
return false;
}
//complete scale method
public void scale(int scaleFactor)
{
x = x*scaleFactor;
y = y*scaleFactor;
}
}
class ColorXYCoord extends XYCoord
{
private String color;
public ColorXYCoord(int x,int y,String color)
{
super(x,y);
this.color = color;
}
public String toString()
{
return \"(\"+getX() +\",\"+getY()+\")\"+color;
}
}
class CoordTest
{
public static void main (String[] args)
{
XYCoord coord1 = new XYCoord();
XYCoord coord2 = new XYCoord(20,40);
if(coord1.equals(coord2))
System.out.println(\"\ are equal\");
else
System.out.println(\"\ not equal\");
System.out.println(\"\ The value of coord1 is \"+coord1.toString());
XYCoord coord3 = new XYCoord(10,50);
XYCoord coord4 = new XYCoord(coord3);
ColorXYCoord colorXY = new ColorXYCoord(10,20,\"red\");
System.out.println(\"\ The value of colorXY coordinate is \"+colorXY.toString());
}
}
output:
not equal
The value of coord1 is (0,0)
The value of colorXY is coordinate is (10,20)red



