In: Computer Science
Requirements: You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints). Point should have the following:
Data:
Constructors:
throw new IllegalArgumentException(<”your meaningful String here”>);
If it is OK, the it should initialize the data (of the new instance being created) to be the same as the Point that was received.
Methods:
public class Point implements PointInterface
When your Point.java compiles, Java will expect all methods in the interface to be implemented and will give a compiler error if they are missing. The compiler error will read “class Point is not abstract and does not implement method ”.
You can actually copy the PointInterface methods definitions into your Point class so it will have the comments and the method headers. If you do this, be sure to take out the ; in the method headers or you will get a “missing method body” syntax error when compiling…
public interface PointInterface
{
// toString
// returns a String representing this instance in the form (x,y)
(WITHOUT a space after the ,)
public String toString();
// distanceTo
// throws a new IllegalArgumentException(
if null is received
// returns the distance from this Point to the Point that was
received
// NOTE: there is a static method in the Math class called hypot
can be useful for this method
public double distanceTo(Point otherPoint);
//equals - returns true if it is equal to what is received (as an Object)
public boolean equals(Object obj);
// inQuadrant
// returns true if this Point is in the quadrant specified
// throws a new IllegalArgumentException if the quadrant is
out of range (not 1-4)
public boolean inQuadrant(int quadrant);
// translate
// changes this Point's x and y value by the what is received (thus
"translating" it)
// returns nothing
public void translate(int xMove, int yMove);
// onXAxis
// returns true if this Point is on the x-axis
public boolean onXAxis();
// onYAxis
// returns true if this Point is to the on the y-axis
public boolean onYAxis();
//=============================================
// The method definitions below are commented out and
// do NOT have to be implemented
// //===========================================
// halfwayTo
// throws a new IllegalArgumentException(
if null is received
// returns a new Point which is halfway to the Point that is
received
//public Point halfwayTo(Point another);
// slopeTo
// throws a new IllegalArgumentException(
if null is received
// returns the slope between this Point and the one that is
received.
// since the slope is (changeInY/changeInX), then first check to see if
changeInX is 0
// if so, then return Double.POSITIVE_INFINITY; (since the
denominator is 0)
//public double slopeTo(Point anotherPoint)
}
/****************************Point.java*****************************/
public class Point implements PointInterface {
private int x;
private int y;
public Point() {
this.x = 2;
this.y = -7;
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public Point(Point p) throws InvalidArgumentException {
if (p == null) {
throw new
InvalidArgumentException("Null pointer exception!");
} else {
this.x =
p.x;
this.y =
p.y;
}
}
@Override
public double distanceTo(Point otherPoint) {
if (this == null || otherPoint == null) {
throw new
IllegalArgumentException();
}
return
Math.sqrt(Math.pow((otherPoint.x - this.x), 2) +
Math.pow((otherPoint.y - this.y), 2));
}
@Override
public boolean inQuadrant(int quadrant) {
if (quadrant==0||quadrant>4)
{
throw new
IllegalArgumentException();
} else if (quadrant(this.x, this.y)
== quadrant) {
return
true;
}
return false;
}
private static int quadrant(int x, int y) {
if (x > 0 && y > 0)
{
return 1;
} else if (x < 0 && y
> 0) {
return 2;
} else if (x < 0 && y
< 0) {
return 3;
} else if (x > 0 && y
< 0) {
return 4;
} else {
return
0;
}
}
@Override
public void translate(int xMove, int yMove) {
this.x = x + xMove;
this.y = y + yMove;
}
@Override
public boolean onXAxis() {
if (this.y == 0) {
return
true;
}
return false;
}
@Override
public boolean onYAxis() {
if (this.x == 0) {
return
true;
}
return false;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y
+ "]";
}
@Override
public boolean equals(Object arg0) {
if (this == null || (Point) arg0
== null) {
throw new
IllegalArgumentException("");
}
if (this.x == ((Point) arg0).x
&& this.y == ((Point) arg0).y) {
return
true;
} else {
return
false;
}
}
}
/*********************output*******************/
testing all
-----------------------------
==>Testing parameterized constructor/toString(): passing in
-1 and -3
Point [x=-1, y=-3]
==>Testing parameterized constructor/toString(): passing in
-1 and 4
Point [x=-1, y=4]
==>Testing parameterized constructor/toString(): passing in 0
and -3
Point [x=0, y=-3]
==>Testing parameterized constructor/toString(): passing in 0
and 4
Point [x=0, y=4]
==>Testing parameterized constructor/toString(): passing in 6
and -3
Point [x=6, y=-3]
==>Testing parameterized constructor/toString(): passing in 6
and 4
Point [x=6, y=4]
-----------------------------
==>Testing default constructor/toString()
Point [x=2, y=-7]
-----------------------------
==>Testing copy constructor (translate must work), passing in
null
InvalidArgumentException
==>Testing copy constructor (translate must work), passing in
Point [x=5, y=6]
Point that was created is: Point [x=5, y=6]
-----------------------------
==>Testing Point [x=-3, y=4] 's .translate(0, 0)
Point [x=-3, y=4]
==>Testing Point [x=-3, y=4] 's .translate(0, 7)
Point [x=-3, y=11]
==>Testing Point [x=-3, y=4] 's .translate(5, 0)
Point [x=2, y=4]
==>Testing Point [x=-3, y=4] 's .translate(5, 7)
Point [x=2, y=11]
-----------------------------
==>Testing Point [x=0, y=0] 's .inQuadrant(0)
true
==>Testing Point [x=0, y=6] 's .inQuadrant(0)
true
==>Testing Point [x=0, y=-2] 's .inQuadrant(0)
true
==>Testing Point [x=-3, y=0] 's .inQuadrant(0)
true
==>Testing Point [x=-3, y=6] 's .inQuadrant(0)
false
==>Testing Point [x=-3, y=-2] 's .inQuadrant(0)
false
==>Testing Point [x=5, y=0] 's .inQuadrant(0)
true
==>Testing Point [x=5, y=6] 's .inQuadrant(0)
false
==>Testing Point [x=5, y=-2] 's .inQuadrant(0)
false
==>Testing Point [x=0, y=0] 's .inQuadrant(1)
false
==>Testing Point [x=0, y=6] 's .inQuadrant(1)
false
==>Testing Point [x=0, y=-2] 's .inQuadrant(1)
false
==>Testing Point [x=-3, y=0] 's .inQuadrant(1)
false
==>Testing Point [x=-3, y=6] 's .inQuadrant(1)
false
==>Testing Point [x=-3, y=-2] 's .inQuadrant(1)
false
==>Testing Point [x=5, y=0] 's .inQuadrant(1)
false
==>Testing Point [x=5, y=6] 's .inQuadrant(1)
true
==>Testing Point [x=5, y=-2] 's .inQuadrant(1)
false
==>Testing Point [x=0, y=0] 's .inQuadrant(2)
false
==>Testing Point [x=0, y=6] 's .inQuadrant(2)
false
==>Testing Point [x=0, y=-2] 's .inQuadrant(2)
false
==>Testing Point [x=-3, y=0] 's .inQuadrant(2)
false
==>Testing Point [x=-3, y=6] 's .inQuadrant(2)
true
==>Testing Point [x=-3, y=-2] 's .inQuadrant(2)
false
==>Testing Point [x=5, y=0] 's .inQuadrant(2)
false
==>Testing Point [x=5, y=6] 's .inQuadrant(2)
false
==>Testing Point [x=5, y=-2] 's .inQuadrant(2)
false
==>Testing Point [x=0, y=0] 's .inQuadrant(3)
false
==>Testing Point [x=0, y=6] 's .inQuadrant(3)
false
==>Testing Point [x=0, y=-2] 's .inQuadrant(3)
false
==>Testing Point [x=-3, y=0] 's .inQuadrant(3)
false
==>Testing Point [x=-3, y=6] 's .inQuadrant(3)
false
==>Testing Point [x=-3, y=-2] 's .inQuadrant(3)
true
==>Testing Point [x=5, y=0] 's .inQuadrant(3)
false
==>Testing Point [x=5, y=6] 's .inQuadrant(3)
false
==>Testing Point [x=5, y=-2] 's .inQuadrant(3)
false
==>Testing Point [x=0, y=0] 's .inQuadrant(4)
false
==>Testing Point [x=0, y=6] 's .inQuadrant(4)
false
==>Testing Point [x=0, y=-2] 's .inQuadrant(4)
false
==>Testing Point [x=-3, y=0] 's .inQuadrant(4)
false
==>Testing Point [x=-3, y=6] 's .inQuadrant(4)
false
==>Testing Point [x=-3, y=-2] 's .inQuadrant(4)
false
==>Testing Point [x=5, y=0] 's .inQuadrant(4)
false
==>Testing Point [x=5, y=6] 's .inQuadrant(4)
false
==>Testing Point [x=5, y=-2] 's .inQuadrant(4)
true
==>Testing Point [x=0, y=0] 's .inQuadrant(8)
false
==>Testing Point [x=0, y=6] 's .inQuadrant(8)
false
==>Testing Point [x=0, y=-2] 's .inQuadrant(8)
false
==>Testing Point [x=-3, y=0] 's .inQuadrant(8)
false
==>Testing Point [x=-3, y=6] 's .inQuadrant(8)
false
==>Testing Point [x=-3, y=-2] 's .inQuadrant(8)
false
==>Testing Point [x=5, y=0] 's .inQuadrant(8)
false
==>Testing Point [x=5, y=6] 's .inQuadrant(8)
false
==>Testing Point [x=5, y=-2] 's .inQuadrant(8)
false
-----------------------------
==>Testing whether Point [x=4, y=-2] .equals null
java.lang.IllegalArgumentException
==>Testing whether Point [x=4, y=-2] .equals Point [x=4,
y=-2] (as a STRING)
java.lang.ClassCastException
==>Testing whether Point [x=4, y=-2] .equals Point [x=4,
y=1]
false
==>Testing whether Point [x=4, y=-2] .equals Point [x=9,
y=-2]
false
==>Testing whether Point [x=4, y=-2] .equals Point [x=11,
y=0]
false
==>Testing whether Point [x=4, y=-2] .equals Point [x=4,
y=-2]
true
==>Testing whether Point [x=4, y=-2] .equals Point [x=-2,
y=4]
false
-----------------------------
==>Testing Point [x=6, y=9] 's .distanceTo null
java.lang.IllegalArgumentException
==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=-1,
y=5]
0
==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=-1,
y=2]
3
==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=5,
y=5]
6
==>Testing Point [x=-1, y=5] 's .distanceTo Point [x=5,
y=2]
6.708
==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=-1,
y=5]
3
==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=-1,
y=2]
0
==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=5,
y=5]
6.708
==>Testing Point [x=-1, y=2] 's .distanceTo Point [x=5,
y=2]
6
==>Testing Point [x=5, y=5] 's .distanceTo Point [x=-1,
y=5]
6
==>Testing Point [x=5, y=5] 's .distanceTo Point [x=-1,
y=2]
6.708
==>Testing Point [x=5, y=5] 's .distanceTo Point [x=5,
y=5]
0
==>Testing Point [x=5, y=5] 's .distanceTo Point [x=5,
y=2]
3
==>Testing Point [x=5, y=2] 's .distanceTo Point [x=-1,
y=5]
6.708
==>Testing Point [x=5, y=2] 's .distanceTo Point [x=-1,
y=2]
6
==>Testing Point [x=5, y=2] 's .distanceTo Point [x=5,
y=5]
3
==>Testing Point [x=5, y=2] 's .distanceTo Point [x=5,
y=2]
0
Please let me know if you have any doubt or modify the answer, Thanks :)
2 Console X <terminated Point TesterHG [Java Application) C:\Program Files Java jre1.8.0_211\bin\javaw.exe (Sep 24, 2019, 7:11:18 AM) testing all and 3 ==>Testing parameterized constructor/toString(): passing in -1 Point [x=-1, y=-3] ==>Testing parameterized constructor/toString(): passing in -1 Point (x=-1, y=4] and 4 ==>Testing parameterized constructor/toString(): passing in Point (x=0, y=-3] and -3 and 4 ==> Testing parameterized constructor/toString(): passing in Point (x=0, y=4] ==> Testing parameterized constructor/toString(): passing in 6 and -3, Point (x=6, y=-3] ==>Testing parameterized constructor/toString(): passing in 6 and 4 Point [x=6, y=4] --------- ==>Testing default constructor/toString() Point (x=2, y=-7] ==>Testing copy constructor (translate must work), passing in null InvalidArgumentException ==> Testing copy constructor (translate must work), passing in Point [x=5, y=6] Point that was created is: Point [x=5, y=6] ==> Testing Point [x=-3, y=4] 's .translate(0, 0) Point [x=-3, y=4] ==>Testing Point [x=-3, y=4] 's translate(0, 7) Point (x=-3, y=11] ==>Testing Point (=-3, y=4] 's .translate(5, 0) Point [x=2, y=4]