Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with this Java programming problem. Consider the following class decla

ID: 3791110 • Letter: N

Question

Need help with this Java programming problem.

Consider the following class declaration for an inventory item, which has a name and a unique ID number: class Inventoryltem {private String name; private int uniqueltemlD;} Finish the class by writing appropriate get methods, set methods and constructors. Next modify the class so that it implements the Comparable interface. The compareTo() method should compare each item's unique ID number (for instance the item with ID 5 is less than the item with ID 10.) Test your class by creating an array of inventory items and then sort them using the Arrays.sort() method as demonstrated in class.

Explanation / Answer

Hi, Please find below the classes. I have named the test class as Driver.java. You may test the program for different inventory items by updating the values in Driver.java

InventoryItem.java:

public class InventoryItem implements Comparable {

   private String name;
   private int uniqueItemId;

   public InventoryItem(String name, int uniqueItemId) {
       super();
       this.name = name;
       this.uniqueItemId = uniqueItemId;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getUniqueItemId() {
       return uniqueItemId;
   }

   public void setUniqueItemId(int uniqueItemId) {
       this.uniqueItemId = uniqueItemId;
   }

   @Override
   public int compareTo(Object arg0) {
       // TODO Auto-generated method stub
       InventoryItem item = (InventoryItem) arg0;
       if (this.uniqueItemId < item.getUniqueItemId())
           return -1;
       else if (this.uniqueItemId > item.getUniqueItemId())
           return 1;
       else
           return 0;
   }

}

Driver.java:

import java.util.Arrays;

public class Driver {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       InventoryItem arr[]=new InventoryItem[5];
       arr[0]=new InventoryItem("item1",1001);
       arr[1]=new InventoryItem("item2",1002);
       arr[2]=new InventoryItem("item3",1008);
       arr[3]=new InventoryItem("item4",1004);
       arr[4]=new InventoryItem("item5",1003);

       Arrays.sort(arr);
       System.out.println("Sorted array:");
       for(int i=0;i<arr.length;i++){
           System.out.println("name: " + arr[i].getName() +" unique item ID :" + arr[i].getUniqueItemId());
       }
   }

}

Output from the driver class:
Sorted array:
name: item1   unique item ID :1001
name: item2   unique item ID :1002
name: item5   unique item ID :1003
name: item4   unique item ID :1004
name: item3   unique item ID :1008

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote