Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OpenStack Trove

Authors: Amrith Kumar, Douglas Shelley

1st Edition

1484212215, 9781484212219

More Books

Students also viewed these Databases questions

Question

What are the purposes of performance appraisals?

Answered: 1 week ago