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

using Data Structures using Java. Modify it to make the ADT resizable. For this

ID: 3759719 • Letter: U

Question

using Data Structures using Java.

Modify it to make the ADT resizable. For this ADT, the MAX_SIZE will not be final, but rather will be increased whenever an new item needs to be added and the array is full (ie. size==MAX_SIZE). Whenever the array needs to grow, add the lesser of: (i) half the current size, or; (ii) 50 additional.

Have your ADT report its old and new maximum size whenever the resize operation is called by printing “ADT resized – from xxx to yyy” where xxx is the old size and yyy is the new size. Modify the test driver to increase the number of names to more than 310 (duplicate names are fine).

import java.util.Scanner;
// ADTSortedList class implementation
public class ADTSortedList{
   private String[] list;
   private int count;
   // crate SortedList
   public ADTSortedList(){
       list = new String[20];
       count = 0;
}
   // crate SortedAdd for the list of plateNumber
   public void sortedAdd(String plateNumber) throws ADTSortedListException{
       if(count == list.length)
           throw new ADTSortedListException("List is full");
  
       int i = 0;
       while(i < count){
           if(list[i].compareTo(plateNumber) > 0)
               break;
           i++;
           }
      
       for(int j = count; j > i; j--)
           list[j] = list[j - 1];
       list[i] = plateNumber;
       count++;
       }
   // crate sortedRemove String plate Number
   public void sortedRemove(String plateNumber){
      
       if(count == 0)
           return;
      
       for(int i = 0; i < count; i++){
          
           if(list[i].compareTo(plateNumber) == 0){
               for(int j = i; j < count - 1; j++){
                   list[j] = list[j + 1];
                   }
               list[count - 1] = "";
              
               count--;
               }
           }
       }
   // crate sortedSize
   public int sortedSize(){
      
       return count;
       }
   //crate calocateDeplicates
   public void locateDeplicates(){
      
       for(int i = 0; i < count - 1; i++){
           if(list[i].compareTo(list[i + 1]) == 0){
              
               System.out.println((i + 1) + ". " + list[i + 1]);
               }
           }
       }
   // crate sortedGet
   public String sortedGet(){
       String result = "";
      
       for(int i = 0; i < count; i++)
           result += (i + 1) + ". " + list[i] + " ";
      
       return result;
       }
   }// end of ADTSortedList class.

import java.util.Scanner;

//ADTSortedListDriver class implementation
public class ADTSortedListDriver{
   public static void main(String[] args) throws ADTSortedListException{
      
       Scanner input = new Scanner(System.in);
       String plate = "";
       ADTSortedList adtsl = new ADTSortedList();

       // allow the user to enter plate number we are using the sortedAdd and sortedRemove and
       System.out.print("Enter a licence plate numbers (ZZZZ for exit): ");
       plate = input.next();
       while(!plate.equals("ZZZZ"))
       {   
           adtsl.sortedAdd(plate);
           System.out.print("Enter a licence plate numbers (ZZZZ for exit): ");
           plate = input.next();
           }
       display(adtsl);
      
       System.out.println("Size of the list: " + adtsl.sortedSize());
       System.out.print(" Enter a licence plate number to be removed: ");
      
       plate = input.next();
       adtsl.sortedRemove(plate);
      
       display(adtsl);

       System.out.println("Duplicate plates:");
       adtsl.locateDeplicates();
  
       System.out.print(" Enter a licence plate number: ");
       plate = input.next();

       adtsl.sortedAdd(plate);

       display(adtsl);
      
   }

   public static void display(ADTSortedList adtsl){
      
       System.out.println(" License plate numbers:");
       System.out.println(adtsl.sortedGet());
       }
   }// end of ADTSortedListDriver class
   // message if the list is full
public class ADTSortedListException extends Exception{
   public ADTSortedListException(){
       super("List if full");
       }
   public ADTSortedListException(String message){
       super(message);
       }
   } // end of ADTSortedListException class

Explanation / Answer

public class ADTSortedList{
private String[] list;
private int count;
// crate SortedList
public ADTSortedList(){
list = new String[20];
count = 0;
}
// crate SortedAdd for the list of plateNumber
public void sortedAdd(String plateNumber) throws ADTSortedListException{
if(count == list.length)
throw new ADTSortedListException("List is full");
  
int i = 0;
while(i < count){
if(list[i].compareTo(plateNumber) > 0)
break;
i++;
}
  
for(int j = count; j > i; j--)
list[j] = list[j - 1];
list[i] = plateNumber;
count++;
}
// crate sortedRemove String plate Number
public void sortedRemove(String plateNumber){
  
if(count == 0)
return;
  
for(int i = 0; i < count; i++){
  
if(list[i].compareTo(plateNumber) == 0){
for(int j = i; j < count - 1; j++){
list[j] = list[j + 1];
}
list[count - 1] = "";
  
count--;
}
}
}
// crate sortedSize
public int sortedSize(){
  
return count;
}
//crate calocateDeplicates
public void locateDeplicates(){
  
for(int i = 0; i < count - 1; i++){
if(list[i].compareTo(list[i + 1]) == 0){
  
System.out.println((i + 1) + ". " + list[i + 1]);
}
}
}
// crate sortedGet
public String sortedGet(){
String result = "";
  
for(int i = 0; i < count; i++)
result += (i + 1) + ". " + list[i] + " ";
  
return result;
}
}// end of ADTSortedList class.
import java.util.Scanner;
//ADTSortedListDriver class implementation
public class ADTSortedListDriver{
public static void main(String[] args) throws ADTSortedListException{
  
Scanner input = new Scanner(System.in);
String plate = "";
ADTSortedList adtsl = new ADTSortedList();
// allow the user to enter plate number we are using the sortedAdd and sortedRemove and
System.out.print("Enter a licence plate numbers (ZZZZ for exit): ");
plate = input.next();
while(!plate.equals("ZZZZ"))
{   
adtsl.sortedAdd(plate);
System.out.print("Enter a licence plate numbers (ZZZZ for exit): ");
plate = input.next();
}
display(adtsl);
  
System.out.println("Size of the list: " + adtsl.sortedSize());
System.out.print(" Enter a licence plate number to be removed: ");
  
plate = input.next();
adtsl.sortedRemove(plate);
  
display(adtsl);
System.out.println("Duplicate plates:");
adtsl.locateDeplicates();
  
System.out.print(" Enter a licence plate number: ");
plate = input.next();
adtsl.sortedAdd(plate);
display(adtsl);
  
}
public static void display(ADTSortedList adtsl){
  
System.out.println(" License plate numbers:");
System.out.println(adtsl.sortedGet());
}
}// end of ADTSortedListDriver class
// message if the list is full
public class ADTSortedListException extends Exception{
public ADTSortedListException(){
super("List if full");
}
public ADTSortedListException(String message){
super(message);
}
} // end of ADTSortedListException class