Question: Part 1 : For this assignment, your Item class must also implement the Comparable interface. Example: public class Circle implements Comparable Add the Comparable.compareTo (

Part 1:
For this assignment, your Item class must also implement the Comparable interface. Example: public class Circle implements Comparable
Add the Comparable.compareTo(...) method (below) to your class so you can sort Items.
public int compareTo( Item o )- Compares this Item to the Item passed in the argument. This method returns either a negative integer, zero, or a positive integer based on whether the argument Item is less than, equal to, or larger than the current Item, respectively.
Then
c1.compareTo(c2) returns a negative int because red is equal to Red and 1020.
c3.compareTo(c2) returns a negative int because Blue red in alphabetical order ignoring the case.
Part 2: Implement two subclasses that extend your Item class, well call them ItemA and ItemB here. Implement them as follows:
Instance Variables:
Give your ItemA subclass one instance variable that is a private String.
Give your ItemB subclass one instance variable that is a private Boolean.
Constructors: (1) public ItemA( String s, Integer n1, Integer n2) is a constructor where n1 is the argument for the parent Item class, and n2 is the Integer for the ItemA subclass. (2) public ItemB( String s, Integer n1, Boolean n2) is a constructor where n1 is the argument for the parent Item class, and n2 is the Integer for the ItemB subclass.
Methods: Implement a public getter and setter for each new instance variable. (1) public String toString()- Returns a String representation of the object where all instance variables are on one line and are separated by tabs. This method must call the toString() method of Item. Do not include any text in the String other than the values of the instance variables. (2) public boolean equals(ItemA a)- Calls the equals method from Item first and, if true, it checks the equality of ItemAs Integer instance variable. (3) public boolean equals(ItemB b)- Calls the equals method from Item first and, if true, it checks the equality of ItemBs Boolean instance variable.
The rest of the worksheet are in the pictures.
Part 1 : For this assignment, your Item class

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 Programming Questions!