Question: Consider the following class, Point.java: In a properly encapsulated class, data attributes are usually set to private so that the client will not be able

Consider the following class, Point.java:
In a properly encapsulated class, data attributes are usually set to private so that the client will not be able to directly access the fields. Make the necessary changes and provide the proper getters and setters.
Add an accessor method called quadrant that returns the number of the quadrant (if any) in which the called Point object (this) falls. The method should return:
1 if both coordinates are positive
2 if the x coordinate is negative and the y coordinate is positive
3 if both coordinates are negative
4 if the x coordinate is positive and the y coordinate is negative
0 if the point lies on the x-axis or y-axis
For example:
> Point p1= new Point(3,4);
> p1.quadrant()
1
> Point p3= new Point(-3,-4);
> p3.quadrant()
3
> Point p5= new Point(0,-4);
> p5.quadrant()
0
Although most of the methods in a blueprint class are non-static (meaning that we can think of them as being inside each object of the class), they can also include static methods (which belong to the class as a whole).
A non-static method is preferred if the method needs access to the fields of a particular called object. However, if a method does not need a called object i.e., if it makes more sense to pass in all of the information that it needs as parameters then we typically make it static.
Write a static method closestToOrigin() that takes two Point objects and returns the Point that is closest to the origin.
Hint: Make use of the distanceFromOrigin() method that every Point has!
Because the method is static, we must prepend the class name to call it from outside the class:
> Point p1= new Point(3,4);
> Point p2= new Point(2,-1);
> Point.closestToOrigin(p1, p2)
(2,-1)
 Consider the following class, Point.java: In a properly encapsulated class, data

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!