Question
Write a class that represents two dimensional points. public class Point { //fields, constructors, methods... } The class should have the following fields: x (double)
Write a class that represents two dimensional points.
public class Point { //fields, constructors, methods... }
The class should have the following fields:
x (double)
y (double)
The class should have the following basic features:
A default constructor that creates a point at the origin.
A parameterized constructor to assign the fields of the class
Getters & setters for each field
A toString() method that prints out a String representation of a Point object. The format of the string should look like this: Point - x: 20, y: 100
The class should also have the following methods:
public boolean isLocatedAtOrigin() - returns true if the point is on the origin, otherwise the method returns false
public double distance(double xOther, double yOther) - returns the distance between this point and the (x, y) coordinate given as method parameters
Test case - Make sure you can create Point objects:
Point atOrigin = new Point(); System.out.println(atOrigin.toString()); //Point - x: 0, y: 0 Point point = new Point(1, 5); System.out.println(point.toString()); //Point - x: 1, y: 5
Test case - Your isLocatedAtOrigin() and distance() methods should both work correctly:
System.out.println(atOrigin.isLocatedAtOrigin()); //true System.out.println(point.isLocatedAtOrigin()); //false System.out.println(point.distance(-2, 1)); //5 System.out.println(atOrigin.distance(0, 0)); //0
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