1 Write a complete class definition for a class named Point
1) Write a complete class definition for a class named Point. It will represent a standard Cartesian point consisting of an x and y coordinate. These two coordinates should be created as private or protected integer variables in the class. The class should also have the following public member functions defined.
Point( int a, int b) - a constructor function that initializes the variable x to the value passed to the function inside parameter a, and initializes the data member y to the value passed to the function inside parameter b.
int getx( ) - simple public function that returns the value stored in the data member x.
int gety( ) - simple public function that returns the value stored in the data member y.
void setx( int a ) -stores the value passed inside of parameter a into the data member x.
void sety( int b ) -stores the value passed inside of parameter b into the data member y.
void setxy(int a, int b) – stores the given value a into the x coordinate and the given argument b into the y coordinate BUT only if they are both greater than 0.
boolean isEqual( Point otherPoint) – returns true if this point equals the otherPoint passed in as an argument. We will consider two points equal if they have the same x and y value.?
Answer:
< >
public class Point {
protected int x, y; // coordinates of Point
// constructor
public Point( int xCoordinate, int yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
}
// set x and y coordinates of Point
public void setXY( int xCoordinate, int yCoordinate )
{
if(xCoordinate>0 && yCoordinate > 0){
x = xCoordinate;
y = yCoordinate;
}
}
// get x coordinate
public int getX()
{
return x;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
// get y coordinate
public int getY()
{
return y;
}
boolean isEqual( Point otherPoint) {
if(otherPoint.x == x && otherPoint.y == y){
return true;
}
return false;
}
}
// get x coordinate
public int getX()
{
return x;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
// get y coordinate
public int getY()
{
return y;
}
boolean isEqual( Point otherPoint) {
if(otherPoint.x == x && otherPoint.y == y){
return true;
}
return false;
}
}
2)
We will consider a Line to consist of two endpoints P1 and P2. This means that the relationship between the Line class and the Point class (of question #1) is the “Has-A” relationship. Write a simple Line class that has the two points P1 and P2. Include a constructor which takes 4 int arguments. The first two arguments will be the x and y coordinates of P1 and the 3rd and 4th arguments will be the x and y coordinates for P2. The points themselves should be created/instantiated in the Line constructor. ?
Solution
public class Line
{
private Point p1;
private Point p2;
Line(int x1,int y1,int x2,int y2)
{
p1=new Point(x1,y1);
p2=new Point(x2,y2);
}
public void setP1(Point p)
{
p1.x=p.x;
p1.y=p.y;
}
public void setP2(Point p)
{
p2.x=p.x;
p2.y=p.y;
}
public Point getP1()
{
return p1;
}
public Point getP2()
{
return p2;
}
}


