Implementing Point Class
The Point class represents the x- and y-coordinates of a single point in the coordinate plane.
Creating Instance Variables
Create two private instance variables of type integer for the x-coordinate and y-coordinate of a point.
Creating a Constructor
Create a public constructor that takes two parameters with the first parameter being the x-coordinate and the second parameter being the y-coordinate. Assign the x- and y-coordinate parameters to the corresponding instance variables.(See Lab3-Step3 on how to create constructors.)
public Point ( int x, int y ) { // Add your code here // Assign the x-coordinate parameter to the instance variable x // Assign the y-coordinate parameter to the instance variable y }
Creating Accessors
Create accessors for the two instance variables. (See Lab3-Step3 on how to create accessors.)
-
getX()
method: returns the value of the x-coordinate of the point -
getY()
method: returns the value of the y-coordinate of the point
Creating Distance Method
To find the length and width of a rectangle from the coordinates of its four corners, you will define a distance method. This method will have one Point object as an argument (since you will be calling it from inside another point), and will return the floating point distance between them.
The Distance method signature is:
public double distance ( Point otherPoint) { // Add your code here }
This method will use the distance formula to find the distance between two points. The distance between two points (x1,y1) and (x2, y2) is defined as:
distance = square root of ( (x1 - x2)2 + (y1 - y2)2 )
-
Math.sqrt(double a)
is used to return the correctly rounded positive square root of a double value. -
Math.pow(double a, double b)
is used to return the value of the first argument raised to the power of the second argument.
To learn more about the use of sqrt( )
, pow( )
,
and other Math
class methods, see the
Java API.
Notice that we have not made any mutator methods. Like Fraction
in Lab 3, Point
is an immutable class, as its
instance variables
will not change their values once the object has been created.