a point as an object with an x and y as key attributes, another properties is to check if the point is the origin or not. use methods such as getters, setters and constructors, your class should have the following functionality listed
isOrigin() : returns true if the values of x = 0 and y =0
distanceTo() : Takes in another point and finds the distance between the two points.
distanceToX(): Calculates the distance to the x-axis
Provide a toString() : to represent all the point attributes and the value returned by the isOrigin method 1.Provide a simple Class and object diagram for the above scenario 2.Implement class modelled above and test its functionality as specified below
Sample Run1
Enter the x&y values separated by a space: 9 4
Enter the x&y values separated by a space: 3 5
Output1:
Point A(9,4) not the Origin
Point B(3, 5) not the Origin
Distance from Point A(9,4) to Point B(3,5) = 6.08
Distance of Point B (3,5) to the X-axis = 5
Point A represented with toString : x = 9, y = 4
isOrigin : False
import java.util.Scanner;
import static java.lang.Math.*;
class Point{
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
if(isOrigin()){
System.out.printf("Point (%d,%d) not the Origin", x, y);
} else {
System.out.printf("Point (%d,%d) the Origin", x, y);
}
System.out.println("");
}
public boolean isOrigin(){
if(x == 0 & y == 0)
return true;
return false;
}
public void DistanceTo(Point a){
System.out.printf("Distance from Point (%d,%d) to Point (%d,%d) = %.2f", x, y, a.x, a.y, sqrt(pow(x - a.x,2) + pow(y - a.y,2)));
System.out.println("");
}
public void DistanceToX(){
System.out.printf("Distance of Point (%d,%d) to the X-axis = %d", x, y, abs(y));
System.out.println("");
}
@Override
public String toString() {
String answ = "Point represented with toString: x = " + x + ", y = " + y;
return answ;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the numerator and denominator separated by a space: ");
String first = in.nextLine();
String[] firstS = first.split(" ");
System.out.print("Enter the numerator and denominator separated by a space: ");
String second = in.nextLine();
String[] secondS = second.split(" ");
Point point1 = new Point(Integer.parseInt(firstS[0]), Integer.parseInt(firstS[1]));
Point point2 = new Point(Integer.parseInt(secondS[0]), Integer.parseInt(secondS[1]));
point1.DistanceTo(point2);
point2.DistanceToX();
System.out.println(point1.toString());
System.out.println("isOrigin: " + point2.isOrigin());
}
}
Comments
Leave a comment