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{/*Stuff*/}

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

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!