Review:
CSE148 Object Oriented Programming Homework Set 03
1. Define class, Create instance/object from a class.
2. Access data fields and methods in a class.
3. Instance member vs class member (static member)
4. Key techniques:
a. Understand “call-by-value”, “call-by-reference”.
b. Pass object to method.
c. Define array of reference type.
d. “this” reference and its usage in a class
Write a Java program to:
1. Define a Point2D class which is a point in 2D coordinate system. The class contains:
a. Data fields:
• x (double), private
• y (double), private
The default values for these 2 variables are both 0.0.
b. Methods:
(1) A no-arg constructor that creates a default Point2D object.
(2) A constructor that creates a Point2D object with specified x and y coordinates.
(3) Getter method and Setter method to get and change x-coordinate.
(4) Getter method and Setter method to get and change y-coordinate.
(5) A method named “getDistance()” to calculate the distance from this point to the original point (0.0, 0.0).
(6) A method name “getDistance(Point2D pt)” to calculate the distance from this point to another point specified parameter “pt”
(7) A method toString() which returns the following text message “Point(x-coord, y-coord)”. x-coord, and y-coord are replaced by values stored in member x and y.
Test:
Create a test class, in this class:
(1) Create an array of Point2D type.
(2) Create Point2D objects and store these objects in above array. Display these points (call toString())
(3) For each point in the array, display distance of this point to the original point.
SCCC CSE148, Spring 2021
(4) Define a method in Testnamed “findClosestPointToOrignal()”. Pass above array to this method as input parameter. Return the point which is closest to the original point.
Call this method in main() and display result returned from the method.
(5) Define a method named “findClosestPoints()”. Pass above array to the method as input parameter. Return 2 points in the array which are closest to each other. Return these 2 points from the method.
Call this method in main() and display result returned from the method.
SCCC CSE148, Spring 2021