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)
}
PointInterface.java (Interface class)
public interface PointInterface {
@Override
public String toString();
public double distanceTo(Point otherPoint);
@Override
public boolean equals(Object obj);
public boolean inQuadrant(int quadrant);
public void translate(int xMove, int yMove);
public boolean onXAxis();
public boolean onYAxis();
}
Point.java
ublic class Point implements PointInterface{
private int x, y;
public Point()
{
this.x = 2;
this.y = -7;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public Point(Point p)
{
this.x = p.getX();
this.y = p.getY();
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString()
{
return("(" + this.x + "," + this.y + ")");
}
@Override
public double distanceTo(Point otherPoint) {
return Math.hypot((otherPoint.getX() - x), (otherPoint.getY() -
y));
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public boolean inQuadrant(int quadrant) {
if(quadrant < 1 || quadrant > 4)
throw new IllegalArgumentException("Quadrant out range 1-4");
else
{
boolean inQuadrant = false;
switch(quadrant)
{
case 1:
{
inQuadrant = (x >= 0 && y >= 0);
break;
}
case 2:
{
inQuadrant = (x < 0 && y >= 0);
break;
}
case 3:
{
inQuadrant = (x < 0 && y < 0);
break;
}
case 4:
{
inQuadrant = (x >= 0 && y < 0);
break;
}
}
return inQuadrant;
}
}
@Override
public void translate(int xMove, int yMove) {
this.x = xMove;
this.y = yMove;
}
@Override
public boolean onXAxis() {
return(this.y == 0);
}
@Override
public boolean onYAxis() {
return(this.x == 0);
}
}
PointTest.java (Driver class)
public class PointTest {
public static void main(String[] args) {
// creating 3 Point objects
System.out.println("Creating an object of Point using default
constructor...");
Point p1 = new Point();
System.out.println("Creating an object of Point using overloaded
constructor...");
Point p2 = new Point(0, 9);
System.out.println("Creating an object of Point using copy
constructor...");
Point p3 = new Point(new Point(4, 0));
System.out.println("\nDisplaying all the 3 Points...");
System.out.println("Point 1: " + p1.toString());
System.out.println("Point 2: " + p2.toString());
System.out.println("Point 3: " + p3.toString());
System.out.println("\nCalculating distance between the 3
Points...");
System.out.println("Distance between Point 1 and Point 2: " +
String.format("%.2f", p1.distanceTo(p2)));
System.out.println("Distance between Point 2 and Point 3: " +
String.format("%.2f", p2.distanceTo(p3)));
System.out.println("Distance between Point 3 and Point 1: " +
String.format("%.2f", p3.distanceTo(p1)));
System.out.println("\nDetermining onXAxis and onYAxis for all
the 3 Points...");
System.out.println("Is Point 1, " + p1.toString() + " on x-axis: "
+ p1.onXAxis());
System.out.println("Is Point 1, " + p1.toString() + " on y-axis: "
+ p1.onYAxis());
System.out.println("Is Point 2, " + p2.toString() + " on x-axis: "
+ p2.onXAxis());
System.out.println("Is Point 2, " + p2.toString() + " on y-axis: "
+ p2.onYAxis());
System.out.println("Is Point 3, " + p3.toString() + " on x-axis: "
+ p3.onXAxis());
System.out.println("Is Point 3, " + p3.toString() + " on y-axis: "
+ p3.onYAxis());
System.out.println("\nDetermining in which quadrant all the 3
Points fall...");
try {
System.out.println("Is Point 1 " + p1.toString() + " in Quadrant 1:
" + p1.inQuadrant(1));
System.out.println("Is Point 1 " + p1.toString() + " in Quadrant 2:
" + p1.inQuadrant(2));
System.out.println("Is Point 1 " + p1.toString() + " in Quadrant 3:
" + p1.inQuadrant(3));
System.out.println("Is Point 1 " + p1.toString() + " in Quadrant 4:
" + p1.inQuadrant(4));
System.out.println("\nIs Point 2 " + p2.toString() + " in
Quadrant 1: " + p2.inQuadrant(1));
System.out.println("Is Point 2 " + p2.toString() + " in Quadrant 2:
" + p2.inQuadrant(2));
System.out.println("Is Point 2 " + p2.toString() + " in Quadrant 3:
" + p2.inQuadrant(3));
System.out.println("Is Point 2 " + p2.toString() + " in Quadrant 4:
" + p2.inQuadrant(4));
System.out.println("\nIs Point 3 " + p3.toString() + " in
Quadrant 1: " + p3.inQuadrant(1));
System.out.println("Is Point 3 " + p3.toString() + " in Quadrant 2:
" + p3.inQuadrant(2));
System.out.println("Is Point 3 " + p3.toString() + " in Quadrant 3:
" + p3.inQuadrant(3));
System.out.println("Is Point 3 " + p3.toString() + " in Quadrant 4:
" + p3.inQuadrant(4));
} catch (IllegalArgumentException iae) {
System.out.println(iae.getMessage());
}
System.out.println("\nTranslating Point 1 to (3,-1), Point 2 to
(0, 2) and Point 3 to (8, 0)...");
p1.translate(3, -1);
p2.translate(0, 2);
p3.translate(8, 0);
System.out.println("Point 1: " + p1.toString());
System.out.println("Point 2: " + p2.toString());
System.out.println("Point 3: " + p3.toString());
}
}
***************************************************************** SCREENSHOT **********************************************************