Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Develop a Generic String List (GSL). Create code for GSL The GSL will have the same methods as described in episodes 302, 303 and 304

Develop a Generic String List (GSL).

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.

Code description given for the two methods

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. }

Code that has already been given:

public class GIL { // Use an array to create the list private int arr[]; private int size;

/* * Constructor for objects of class GIL */ public GIL() { arr = new int[10]; size = 0; }

/* * size - returns the size of the list * * return - the size of the list as an integer */ public int size() { return size; }

/* * add - add one value to the list in the next available position * * param - integer to add to the list */ public void add(int value) { if (size == arr.length) // Is arr full? Then expand by 20% { int[] arr2 = new int[(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++; }

/* * get - return the value at the specified location in the list * * param - index into the list for the value to return * return - integer value */ public int get(int index) { return arr[index]; }

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

/* * insert - insert new element at indicated index * * param - index to insert new element * param - integer value of new element */ public void insert(int index, int 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% { int[] arr2 = new int[(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++; } }

/* * toString - return a string value that represents the list * * return - String */ public String toString() { String returnValue = String.valueOf(arr[0]); for (int i = 1; i < size; i++) returnValue = returnValue + ", " + arr[i]; return returnValue; }

/* * display - display 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 "); else System.out.println("List has " + (arr.length - size) + " spaces left "); } }

Create code to test

Test code already given:

public class GILTest { public static void main(String[] args) { int errors = 0;

GIL list = new GIL(); /******************************************************************/ /* 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) != 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(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) != 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) != 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) != 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++; }

/******************************************************************/ /* 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!!!"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago