In: Computer Science
my question hasnt been answered yet. So, posting it again.
Follow program instructions carefully. spacing is important.
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 <the missing 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…
Piece of the code:
//This is the interface for a Point class (which will represent a 2-dimensional Point)
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(<your descriptive String> 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(<your descriptive String> 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(<your descriptive String> 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)
}
Thanks for the question, here is the implemented class Point. As per the question the following 3 methods are commented so I didnt implement them - these are - halfwayTo() slopeTo() remaining all method declared in PointInterface are implemented correctly. Let me know for any changes or modification, do comment please. ============================================================================
public class Point implements PointInterface { int x; int y; //A default constructor that will set the values to (2,-7) public Point() { x = 2; y = -7; } //A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. public Point(int x, int y) { this.x = x; this.y = y; } //A copy constructor that will receive a Point. If it receives null, then it should public Point(Point point) { if (point == null) { throw new IllegalArgumentException("Point cannot be null"); } this.x = point.x; this.y = point.y; } @Override public double distanceTo(Point otherPoint) { if (otherPoint == null) { throw new IllegalArgumentException("Point cannot be null"); } return Math.sqrt((x - otherPoint.x) * (x - otherPoint.x) + (y - otherPoint.y) * (y - otherPoint.y)); } @Override public boolean inQuadrant(int quadrant) { if (quadrant == 1 && x > 0 && y > 0) return true; if (quadrant == 2 && x < 0 && y > 0) return true; if (quadrant == 3 && x < 0 && y < 0) return true; if (quadrant == 4 && x > 0 && y < 0) return true; return false; } @Override public void translate(int xMove, int yMove) { x = xMove; y = yMove; } @Override public boolean onXAxis() { return y == 0; } @Override public boolean onYAxis() { return x == 0; } @Override public String toString() { return "(" + x + "," + y + ")"; } @Override public boolean equals(Object obj) { if (obj == null) return false; if (obj instanceof Point) { Point aPoint = (Point) obj; return x == aPoint.x && y == aPoint.y; } return false; } }
===================================================================
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(<your descriptive String> 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(<your descriptive String> 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(<your descriptive String> 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) }