Question

Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end.

Here is what is missing :

Here is my code:

public class GSL

{

private String arr[];

private int size;

public GSL()

{

    arr = new String[10];

    size = 0;

}

public int size()

{

    return size;

}

public void add(String value)

{

    if (size == arr.length) // Is arr full? Then expand by 20%

    {

      String[] arr2 = new String[(int)(arr.length * 1.2)];

      // Copy elements from arr to arr2

      for (int i = 0; i < arr.length; i++)

        arr2[i] = arr[i];

      // Have arr point to new array

      arr = arr2;

      // Old array will be Garbage Collected

    }

    arr[size] = value;

    size++;

}

public void display()

{

    for (int i = 0; i < size; i++)

      System.out.println(i + ": " + arr[i]);

    if ( arr.length == size)

      System.out.println("List is full\n");

    else

      System.out.println("List has " + (arr.length - size) + " spaces left\n");

}

public String get(int index)

{

    return arr[index];

}

public void clear()

{

    arr = new String[10];

    size = 0;

}

public void insert(int index, String value)

{

    // If the index points to an empty element, add it.

    if ( index >= size )

      add(value);

    else

    {

      if (size == arr.length) // Is arr full? Then expand by 20%

      {

        String[] arr2 = new String[(int)(arr.length * 1.2)];

        // Copy elems from arr to arr2

        for (int i = 0; i < arr.length; i++)

          arr2[i] = arr[i];

        // Have arr point to new array

        arr = arr2;

        // Old array will be Garbage Collected

      }

      // Open a hole to insert the value

      for (int i = size; i > index; i--)

        arr[i] = arr[i - 1];

      arr[index] = value;

      size++;

    }

}

public String toString()

{

    String returnValue = String.valueOf(arr[0]);

    for (int i = 1; i < size; i++)

      returnValue = returnValue + ", " + arr[i];

    return returnValue;

}

public void set(int index, String value)

{

// Code to set an element of your string list at specified index

// to the provided value. You cannot set an item beyond that last

// item in the list.

if (index <= size )    

    {

      arr[index] = value;

    }

else

{

System.out.println ( "index is larger than size");

}

}

public void remove(int index)

{

// Code to remove the element at the specified index. All elements

// after the index are shifted down to fill the hole. You cannot

// remove an item beyond the last item in the list.

if (index <= size )    

    {

      arr[index] = "";

    }

else

{

System.out.println ( "index is larger than size");

}

}

}

Here is the test code:

public class GSLTest

{

public static void main(String[] args)

{

    int errors = 0;

    GSL list = new GSL();

    /******************************************************************/

    /* A new list should return a size of zero                        */

    /******************************************************************/

    if ( list.size() != 0)

    {

      System.out.println("ERROR: A new list does not return size of 0");

      errors++;

    }

    /******************************************************************/

    /* Adding an element should change the size to 1                  */

    /******************************************************************/

    list.add("3");

    if ( list.size() != 1)

    {

      System.out.println("ERROR: Add one element to list does not return size of 1");

      errors++;

    }

   

    /******************************************************************/

    /* Is value at the first position the value we expect?            */

    /******************************************************************/

    if ( ! list.get(0).equals( "3" ) )

    {

      System.out.println("ERROR: The value of the first element is not what we set it to be");

      errors++;

    }

    /******************************************************************/

    /* Display the list                                               */

    /******************************************************************/

    System.out.println("Display list with one element");

    list.display();

    /******************************************************************/

    /* Add 9 more values to list. The size should be 10.              */

    /******************************************************************/

    for (int i = 0; i < 9; i++)

      list.add(Integer.toString(i+1));

    if ( list.size() != 10 )

    {

      System.out.println("ERROR: The size is not equal to 10");

      errors++;

    }

    // Display the list

    System.out.println("Display list with ten elements");

    list.display();

/******************************************************************/

    /* Add one more value. The list should expand                     */

    /******************************************************************/

    list.add("11");

    if ( list.size() != 11)

    {

      System.out.println("ERROR: Add one more element to list does not return size of 11");

      errors++;

    }

   if ( !list.get(10).equals("11") )

    {

      System.out.println("ERROR: The value of the 11th element is not what we set it to be");

      errors++;

    }

    System.out.println("Display list with eleven elements. List expanded");

    list.display();

    /******************************************************************/

    /* Insert a new element at the beginning of the list              */

    /******************************************************************/

    list.insert(0, "111");

    if ( list.size() != 12)

    {

      System.out.println("ERROR: Add one more element to list does not return size of 12");

      errors++;

    }

    if ( ! list.get(0).equals( "111" ) )

    {

      System.out.println("ERROR: The value of the first element is not what we set it to be");

      errors++;

    }

    // Display the list

    System.out.println("Display list with insert at index 5");

    list.display();

    /******************************************************************/

    /*Insert a new element in the middle of the list                  */

    /******************************************************************/

    list.insert(5, "2222");

    if ( list.size() != 13)

    {

      System.out.println("ERROR: Add one more element to list does not return size of 13");

      errors++;

    }

    if ( ! list.get(5).equals( "2222" ) )

    {

      System.out.println("ERROR: The value of the sixth element is not what we set it to be");

     errors++;

    }

    // Display the list

    System.out.println("Display list with insert at index 5");

    list.display();

    /******************************************************************/

    /* Display list as a string                                       */

    /******************************************************************/

    String stringList = list.toString();

    if (!("111, 3, 1, 2, 3, 2222, 4, 5, 6, 7, 8, 9, 11".equals(stringList)))

    {

      System.out.println("ERROR: toString does not return a properly formatted string");

      System.out.println("ERROR: >" + stringList + "<");

      errors++;

    }

   

    list.remove( 5 );

      if (! list.get(5).equals("") )

          System.out.println( "Did not remove correctly" );

    

     list.remove( 19 ); // if this gives an error, success!        

         

      

    list.set( 5, "Something");

      if (! list.get(5).equals("Something") )

          System.out.println( "Did not set correctly" );

    

     list.set( 19, "that lol" ); // if this gives an error, success!

   

    /******************************************************************/

    /* Clear the list                                                 */

    /******************************************************************/

    list.clear();

    // An empty list should return a size of zero

    if ( list.size() != 0)

    {

      System.out.println("ERROR: An empty list does not return size of 0");

      errors++;

    }

    // Display the list

    System.out.println("Display list that has been cleared");

    list.display();

    // What is the final result?

    if ( errors == 0 )

      System.out.println("No errors found!!!");

}

}

Create code for GSL

The GSL will have the same methods as described in episodes 302, 303 and 304 for the Generic Integer List. Use the same names for your methods and the same number of parameters as in the GIL code.

I want you to create code that will work with String objects.

You will be adding two additional additional methods that were not described in the Generic Integer List.

public void set(int index, String value)
{
  // Code to set an element of your string list at specified index
  // to the provided value. You cannot set an item beyond that last
  // item in the list. Set will only work on elements greater than
  // or equal to zero and less than size. Do not add elements to the
  // list with set. Display an error if an index is out of bounds.
}

public void remove(int index)
{
  // Code to remove the element at the specified index. All elements
  // after the index are shifted down to fill the hole. You cannot
  // remove an item beyond the last item in the list. Remove will only
  // work on elements greater than or equal to zero and less than size.
  // Display an error if an index is out of bounds.
}

Create code to test your GSL

You will also create a set of tests that exercise your code. Your tests should follow the example of GILTest as shown in episodes 302, 303 and 304. You will need to create new tests for the set and remove methods.

Again, your tests should test with String objects.

Your GSL should pass all your tests with no errors. If your code does not pass all your tests you either have an error in your GSL code or GSLTest code. Do not turn in your work unless your code cleanly passes your tests.

Create a jar file with GSL.java and GSLTest.java

Submit your jar file

You will get full credit for this assignment if you do the following:

The GSL implements all methods and functionality

The GSL methods are named as in the GIL code

The GSL methods have the same number of parameters as in the GIL code

The GSLTest implements all tests as described in GILTest plus tests for the set and remove methods

If any of these are not correct, no credit.

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

public class GSL {

   // Instance variables
   private String arr[];
   private int size;

   /**
   * Default constructor
   */
   public GSL() {
       arr = new String[10];
       size = 0;
   }

   /**
   * Returns size of the list
   * @return
   */
   public int size() {
       return size;
   }

   /**
   *
   * @param value
   */
   public void add(String value) {
       if (size == arr.length) { // Is arr full? Then expand by 20%
           String[] arr2 = new String[(int) (arr.length * 1.2)];
           // Copy elements from arr to arr2
           for (int i = 0; i < arr.length; i++)
               arr2[i] = arr[i];
          
           // Have arr point to new array
           arr = arr2;
           // Old array will be Garbage Collected
       }
       arr[size] = value;
       size++;
   }

   /**
   * Displays the list
   */
   public void display() {
       for (int i = 0; i < size; i++)
           System.out.println(i + ": " + arr[i]);
       if (arr.length == size)
           System.out.println("List is full\n");
       else
           System.out.println("List has " + (arr.length - size) + " spaces left\n");
   }

   /**
   * Gets the String at index
   * @param index
   * @return
   */
   public String get(int index) {
       return arr[index];
   }

   /**
   * Clears the list
   */
   public void clear() {
       arr = new String[10];
       size = 0;
   }

   /**
   * Inserts String value at index
   * @param index
   * @param value
   */
   public void insert(int index, String value) {
       // If the index points to an empty element, add it.
       if (index >= size)
           add(value);
       else {
           if (size == arr.length) // Is arr full? Then expand by 20%
           {
               String[] arr2 = new String[(int) (arr.length * 1.2)];
               // Copy elems from arr to arr2
               for (int i = 0; i < arr.length; i++)
                   arr2[i] = arr[i];
               // Have arr point to new array
               arr = arr2;
               // Old array will be Garbage Collected
           }
           // Open a hole to insert the value
           for (int i = size; i > index; i--)
               arr[i] = arr[i - 1];
           arr[index] = value;
           size++;
       }
   }

   /**
   * Displays the list in String format
   */
   public String toString() {
       String returnValue = String.valueOf(arr[0]);
       for (int i = 1; i < size; i++)
           returnValue = returnValue + ", " + arr[i];
       return returnValue;
   }

   /**
   * Sets the String at index with the String value
   * @param index
   * @param value
   */
   public void set(int index, String value) {
       // Code to set an element of your string list at specified index
       // to the provided value. You cannot set an item beyond that last
       // item in the list.
       if ((index >= 0) && (index < size)) {
           arr[index] = value;
       } else if (index < 0) {
           System.out.println("index " + index + " is less than 0");
       } else {
           System.out.println("index " + index + " is larger than size");
       }
   }

   /**
   * Removes the String at index
   * @param index
   */
   public void remove(int index) {
       // Code to remove the element at the specified index. All elements
       // after the index are shifted down to fill the hole. You cannot
       // remove an item beyond the last item in the list.
       if ((index >= 0) && (index < size)) {
           // To remove the String at index
           // move all elements after index one position towards index
           for (int i = index; i < size() - 1; i++) {
               arr[i] = arr[i + 1];
           }
           this.size -= 1;
       } else if (index < 0) {
           System.out.println("index " + index + " is less than 0");
       } else {
           System.out.println("index " + index + " is larger than size");
       }
   }
}

public class GSLTest {
   public static void main(String[] args) {
       int errors = 0;
       GSL list = new GSL();
      
       /******************************************************************/
       /* A new list should return a size of zero */
       /******************************************************************/
       if (list.size() != 0) {
           System.out.println("ERROR: A new list does not return size of 0");
           errors++;
       }
      
       /******************************************************************/
       /* Adding an element should change the size to 1 */
       /******************************************************************/
       list.add("3");
       if (list.size() != 1) {
           System.out.println("ERROR: Add one element to list does not return size of 1");
           errors++;
       }
      
       /******************************************************************/
       /* Is value at the first position the value we expect? */
       /******************************************************************/
       if (!list.get(0).equals("3")) {
           System.out.println("ERROR: The value of the first element is not what we set it to be");
           errors++;
       }
      
       /******************************************************************/
       /* Display the list */
       /******************************************************************/
       System.out.println("Display list with one element");
       list.display();
      
       /******************************************************************/
       /* Add 9 more values to list. The size should be 10. */
       /******************************************************************/
       for (int i = 0; i < 9; i++)
           list.add(Integer.toString(i + 1));
       if (list.size() != 10) {
           System.out.println("ERROR: The size is not equal to 10");
           errors++;
       }
       // Display the list
       System.out.println("Display list with ten elements");
       list.display();
      
       /******************************************************************/
       /* Add one more value. The list should expand */
       /******************************************************************/
       list.add("11");
       if (list.size() != 11) {
           System.out.println("ERROR: Add one more element to list does not return size of 11");
           errors++;
       }
       if (!list.get(10).equals("11")) {
           System.out.println("ERROR: The value of the 11th element is not what we set it to be");
           errors++;
       }
       System.out.println("Display list with eleven elements. List expanded");
       list.display();
      
       /******************************************************************/
       /* Insert a new element at the beginning of the list */
       /******************************************************************/
       list.insert(5, "111");
       if (list.size() != 12) {
           System.out.println("ERROR: Add one more element to list does not return size of 12");
           errors++;
       }
       if (!list.get(5).equals("111")) {
           System.out.println("ERROR: The value of the first element is not what we set it to be");
           errors++;
       }
       // Display the list
       System.out.println("Display list with insert at index 5");
       list.display();
      
       /******************************************************************/
       /* Insert a new element in the middle of the list */
       /******************************************************************/
       list.insert(5, "2222");
       if (list.size() != 13) {
           System.out.println("ERROR: Add one more element to list does not return size of 13");
           errors++;
       }
       if (!list.get(5).equals("2222")) {
           System.out.println("ERROR: The value of the sixth element is not what we set it to be");
           errors++;
       }
       // Display the list
       System.out.println("Display list with insert at index 5");
       list.display();
      
       /******************************************************************/
       /* Display list as a string */
       /******************************************************************/
       String stringList = list.toString();
       if (!("3, 1, 2, 3, 4, 2222, 111, 5, 6, 7, 8, 9, 11".equals(stringList))) {
           System.out.println("ERROR: toString does not return a properly formatted string");
           System.out.println("ERROR: >" + stringList + "<");
           errors++;
       }
       list.remove(5);
       if (!list.get(5).equals("111"))
           System.out.println("Did not remove correctly");
       list.remove(19); // if this gives an error, success!
       list.set(5, "Something");
       if (!list.get(5).equals("Something"))
           System.out.println("Did not set correctly");
       list.set(19, "that lol"); // if this gives an error, success!
      
       /******************************************************************/
       /* Clear the list */
       /******************************************************************/
       System.out.println("Display list before clear");
       list.display();
       list.clear();
       // An empty list should return a size of zero
       if (list.size() != 0) {
           System.out.println("ERROR: An empty list does not return size of 0");
           errors++;
       }
       // Display the list
       System.out.println("Display list that has been cleared");
       list.display();
       // What is the final result?
       if (errors == 0)
           System.out.println("No errors found!!!");
   }
}


SAMPLE OUTPUT:

Display list with one element List has 9 spaces left Display list with ten elements 0: 3 2: 2 3: 3 4: 4 5: 5 «i. «i 7: 7 8: 8Display list with insert at index 5 0: 3 2: 2 3: 3 A:: 셋 5: 111 6: 5 7: 6 8: 7 9: 8 10: 9 1 List is full 2 Display list withindex 19 is larger than size index 19 is larger than size Display list before clear 0: 3 2: 2 3: 3 4: 4 5: Something 6: 5 7:

Add a comment
Know the answer?
Add Answer to:
Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...
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
  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • 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)...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY....

    PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY. THE CODE IS IN JAVA PROGRAMMING LANGUAGE. import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; import java.lang.RuntimeException; import java.util.Arrays; public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess {    protected Object[] data; protected int size; public int size() {     return size; }    private void rangeCheck(int index) {     if (index < 0 || index >= size) throw new IndexOutOfBoundsException(""); }    @SuppressWarnings("unchecked") private E...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Create an ArrayListReview class with one generic type to do the following • Creates an array...

    Create an ArrayListReview class with one generic type to do the following • Creates an array list filled with the generic type of the ArrayListReview class, and inserts new elements into the specified location index-i in the list. (5 points) • Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is...

  • Write the output produced when the following method is passed each of the following lists and...

    Write the output produced when the following method is passed each of the following lists and give the formal code for the problem: public static void mystery1(ArrayList<Integer> list) { for (int i = list.size() - 1; i > 0; i--) { if (list.get(i) < list.get(i - 1)) { int element = list.get(i); list.remove(i); list.add(0, element); } } System.out.println(list); } a. [2, 6, 1, 8] b. [30, 20, 10, 60, 50, 40]

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

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