Question
Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to
Java Help
2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below:
---After declaration, constructors invoked--- Using toString():
First point is (0, 0)
Second point is (7, 13)
Third point is (7, 15)
Second point (7, 13) lines up vertically with third point (7, 15)
Second point (7, 13) doesn't line up horizontally with third point (7, 15)
Enter the x-coordinate for first point: retgre
Not an integer! Try again! Enter the x-coordinate for first point: 89.67
Not an integer! Try again! Enter the x-coordinate for first point: -13
ERROR! Should be positive. Enter the x-coordinate for first point: 15
Enter the y-coordinate for first point: fwgfe
Not an integer! Try again! Enter the y-coordinate for first point: 90.6
Not an integer! Try again! Enter the y-coordinate for first point: -32
ERROR! Should be positive. Enter the y-coordinate for first point: b
Not an integer! Try again! Enter the y-coordinate for first point: 23
First point (after call to set) is (15, 23)
Distance from origin for first point = 27.46
Distance from origin for second point = 14.76
Distance between first point and second point = 12.81
First point (after call to translate (5, 10)) is (20, 33)
Second point (after call to translate (15, 5)) is (22, 18)
---Call to equals: The 2 points are NOT equal.
---Calls to copy and print---
First point (after call to copy) is (20, 33)
Second point (after call to copy) is (20, 33)
---Call to equals after call to copy: The 2 points are equal.
Here is my class point
public class Point { int x, y; //Default Constructor public Point() { } //Parameter Constructor public Point(int x, int y) { this.x = x; this.y = y; } //getter function for x public int getX() { return x; }
//getter function for y public int getY() { return y; } //A method named set to set the coordinates to the parameters passed; invalid values set to 0 public void set(int x, int y) { this.x = x; this.y = y; } //A method named print to print each Point object as (x, y) public void print(Point p) { System.out.println( toString(p) ); } // method toString() public String toString(Point p) { return "( " + p.getX() + " , " + p.getY() + " )"; }
//method named equals to compare 2 Point objects for equality public boolean equals(Point p1, Point p2) { if(p1.x == p2.x && p1.y == p2.y) { return true; } else { return false; } }
//2 methods named copy and getCopy to make a copy of a Point object into another Point object public Point copy(Point p) { Point copyPoint = new Point(); copyPoint.x = p.getX(); copyPoint.y = p.getY(); return copyPoint; } public Point getCopy(Point p) { return copy(p); } //method named distanceFromOrigin to calculate the distance between a point and the origin at(0, 0) public double distanceFromOrigin(Point p) { //Using Euclidian Distance Formula d = sqrt( x^2 + y^2 ) double d = Math.sqrt( (p.getX() * p.getX()) + (p.getY() * p.getY()) ); return d; } // method named distance to calculate the distance from a point to a given point. public double distance(Point p1, Point p2) { //Using Euclidian Distance Formula d = sqrt( (x1^2 - x2^2) + (y1^2 - y2^2) ) double d = Math.sqrt( Math.abs((Math.pow(p1.getX(),2) - Math.pow(p2.getX(),2))) + Math.abs((Math.pow(p1.getY(),2) - Math.pow(p2.getY(),2)))); return d; } //method named translate to shift the location of a point by a given amount. public Point translate(Point p, int amount) { p.set( p.getX() + amount, p.getY()+amount); return p; } //method named isHorizontal that returns true if any given point lines up horizontally with a given point. public boolean isHorizontal(Point p1, Point p2) { //if y coordinate is same then they are horizontal return (p1.getY() == p2.getY()); } //method named isVertical that returns true if any given Point object lines up vertically with a given Point object. public boolean isVertical(Point p1, Point p2) { //if x coordinate is same then they are vertical return (p1.getX() == p2.getX()); } //method named slope that returns the slope of the line between this Point object and a given Point object. public double slope(Point p1, Point p2) { double sl = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX()); return sl; } public static void main(String[] args) { }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started