Question
Write a Phone class, using the Comparable interface. Before you start writing the compareTo method, be sure to have the class with: * 3 instance
Write a Phone class, using the Comparable interface. Before you start writing the compareTo method, be sure to have the class with:
* 3 instance variables (at least) * Getters and setters for each * 2 additional methods of your choice
First, implement the Comparable interface like so:
public class Phone implements Comparable
This method should return one of three possible values:
* -1 if this object is "less than" the parameter object
* 0 if this object has the same value as the parameter object
* +1 if this object is "greater than" the parameter object
** You should first confirm that the other is not null, and if not, return -999.
Then, check whether this and the parameter are literally the same objects in memory, in which case we should return 0. Then, go through each of the instance variables in an order that you decide. For each one compare the instance variable values of this and other; if they are different, return -1 or 1 as appropriate. If they are the same, move on to the next instance variable. If all of the instance variables are the same, we should return 0. Note that you'll be relying on String's compareTo method here along with other comparisons (<,>,<=,>=,==,!=)!
In the main class's main method, write some code creating a few Phone objects and comparing them using your compareTo method. Does it behave as expected?
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