Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your first job is to write a Phone class, where we will be using the Comparable interface. Before you start writing the compareTo method, be

Your first job is to write a Phone class, where we will be 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

Comparable

This leads to another question, how do I compare objects in such a way that I will know which one should go first and which should go second? In comes the Comparable interface.

First, implement the comparable interface like so:

public class Phone implements Comparable{/*Stuff*/}

Weve used interfaces before so everything up to Comparable should look a little familiar. What is this deal? This tells the JVM that when you implement the compareTo method (Comparables sole required method), it will take in an Object of the Phone type and not just of the general Object type.

The interface Comparable authorizes an int-returning method compareTo, which takes a Phone as its parameter. Define and implement the compareTo method.

This method should return one of three possible values:

  • -1 (or any negative int) if this object is "less than" or "comes before" the parameter object
  • 0 if this object has the same value as the parameter object
  • +1 (or any positive int) if this object is "greater than" or "comes after" the parameter object

Part 3 (5 points)

- You should first confirm that other is not null, and if not, throw an IllegalArgumentException.

(If we had not used the type parameter with Comparable, compareTo would have taken an Object rather than a Phone as a parameter, and we would have needed to check that compareTo's parameter was the right type via instanceof)

- Then, check whether this and the parameter are literally the same object 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 somewhat on String's compareTo method here!

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_2

Step: 3

blur-text-image_3

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

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

Students also viewed these Databases questions

Question

BGP relies on policies to make routing decisions a . False b . True

Answered: 1 week ago