Question

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

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] + "\n";
      
       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("\nEnter 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("\nEnter a licence plate number: ");
       plate = input.next();

       adtsl.sortedAdd(plate);

       display(adtsl);
      
   }

   public static void display(ADTSortedList adtsl){
      
       System.out.println("\nLicense 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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] + "\n";
  
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("\nEnter 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("\nEnter a licence plate number: ");
plate = input.next();
adtsl.sortedAdd(plate);
display(adtsl);
  
}
public static void display(ADTSortedList adtsl){
  
System.out.println("\nLicense 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

Add a comment
Know the answer?
Add Answer to:
using Data Structures using Java. Modify it to make the ADT resizable. For this ADT, the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make...

    Now, your objective is to rewrite the same Stack class with a Generic ArrayList and make the entire class support using Generic types. You should be able to create a Stack of any primitive wrapper class, and show us that your Generic Stack implementation (push, pop, search, display, etc.) works with: • Character (Stack) • Integer (Stack) • Float (Stack • Double (Stack) • String (Stack) You can create these Stack objects in main() and perform operations on them to...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same...

    Create a program called GeneralizedBubbleSort that will make use of the Comparable Interface in the same way it is used in the GeneralizedSelectionSort. You should use the attached ComparableDemo to test your program. public class ComparableDemo { public static void main(String[] args) { Double[] d = new Double[10]; for (int i = 0; i < d.length; i++) d[i] = new Double(d.length - i); System.out.println("Before sorting:"); int i; for (i = 0; i < d.length; i++) System.out.print(d[i].doubleValue( ) + ", ");...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • 2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority...

    2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...

  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT