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

JAVA Below in bold is my code and output, but I am getting the wrong output. The

ID: 3851766 • Letter: J

Question

JAVA

Below in bold is my code and output, but I am getting the wrong output. The output I need is below the bold

package hw2;

public class SortedSet<T extends Comparable<T>> {
   private T[] items;
   private int size;
  
  
   public SortedSet(int capacity) {
      
       items = (T[]) new Comparable[capacity];
       size = 0;
   }
  
  
   public String toString() {
       StringBuilder ans = new StringBuilder("{");
       for(int kk=0; kk<size; kk++){
           ans.append(items[kk]);
           if(kk != size-1)
               ans.append(",");
       }

      
      
      
       ans.append("}");
       return ans.toString();
   }
  
   public boolean isFull() {
       return size == items.length;
   }
  
   public boolean add(T item) {
       if (isFull()) return false;
       if(contains(item)) return false;
       boolean retVal=false;
       int kk=0;
       for(kk=0; kk<size; kk++){
           if(items[kk].compareTo(item)>0)
               break;
           for(int aa=items.length-2; aa >= kk; aa--){
               items[aa+1]=items[aa];
           }
           items[kk]=item;
           size++;
           return true;
       }
      
       return retVal;
      
      
      
      
      
   }
  
   public boolean remove(T item) {
       boolean retVal=false;
       for(int kk=0; kk<size; kk++){
           if(items[kk]==item){
               retVal=true;
               for(int aa=kk; aa<size-1; aa++){
                   items[aa]=items[aa+1];
               }
               size--;
               break;
           }
       }
       return retVal;
   }
  
   public boolean contains(T item) {
       boolean retVal=false;
       for(int kk=0; kk<size; kk++){
           if(items[kk]==item){
               retVal=true;
               break;
           }
       }
       return retVal;
   }
}  

Output:

Add a false
{}
Add c false
{}
Add a false
{}
Add b false
{}
Add z false
{}
Add o false
{}
Does the set contain f? false
Does the set contain c? false
Does the set contain a? false
Does the set contain o? false
Remove a false
{}
Remove c false
{}
Remove a false
{}
Remove b false
{}
Remove z false
{}
Remove o false
{}

  
Code/Questions that need to be answered.

A. An add method. It is passed an object of type T (the type variable). It returns true if the object is added to the set, or false otherwise. An item is not added to a set if an equivalent item is already in the set, or if the set is full. Equivalence is defined by the compareTo method of the T class. Remember that at all times the set must have its items in ascending order.

B. A remove method. It is passed an object of type T. It returns true if an equivalent object exists in the set, in which case it is removed. The method should return false if no equivalent item exists in the set.

C. A toString method. As always, toString should return a string which represents the object. In the case of SortedSet, we would like the string to reflect which items are in the set, and in what order.

D. A contains method. It is passed an object of type T. It should return true if an equivalent item is found in the set, or false otherwise.

Explanation / Answer

Below is the fixed code. There were 2 problems -

1. The loops to shift elements down the array was nested in the the for loop above. That loop and assignment to correct position has to been shifted out. Similar is the change in remove() function. Moved the loop out.

2. You have used == to compare objects in remove () as well as contains() method. This will compare hashcodes only. Ideally you should use .equals() method. I have changed that in your code.

Please check if the modified code works for you. Post a comment if you still have any issues. If the answer helped, request you to rate it. Thank you very much.

package hw2;

public class SortedSet<T extends Comparable<T>> {

   private T[] items;

   private int size;

  

  

   public SortedSet(int capacity) {

  

   items = (T[]) new Comparable[capacity];

   size = 0;

   }

  

  

   public String toString() {

   StringBuilder ans = new StringBuilder("{");

   for(int kk=0; kk<size; kk++){

   ans.append(items[kk]);

   if(kk != size-1)

   ans.append(",");

   }

   ans.append("}");

   return ans.toString();

   }

  

   public boolean isFull() {

   return size == items.length;

   }

  

   public boolean add(T item) {

   if (isFull()) return false;

   if(contains(item)) return false;

   boolean retVal=false;

   int kk=0;

   for(kk=0; kk<size; kk++){

   if(items[kk].compareTo(item)>0)

   break;

   }

   for(int aa=items.length-2; aa >= kk; aa--){

   items[aa+1]=items[aa];

   }

   items[kk]=item;

   size++;

   return true;

  

   }

  

   public boolean remove(T item) {

   boolean retVal=false;

   int kk;

   for( kk=0; kk<size; kk++){

   if(items[kk].equals(item)){

   retVal=true;

   break;

   }

   }

   if(retVal == true){

   for(int aa=kk; aa<size-1; aa++){

   items[aa]=items[aa+1];

   }

   size--;

   }

   return retVal;

   }

  

   public boolean contains(T item) {

   boolean retVal=false;

   for(int kk=0; kk<size; kk++){

   if(items[kk].equals(item)){

   retVal=true;

   break;

   }

   }

   return retVal;

   }

}