Question
Code works with test. But This has 2 errors. One in get method and one in insert method. Was told that the errors are are
Code works with test. But This has 2 errors. One in get method and one in insert method. Was told that the errors are are not correctly validating the index value.
import java.util.Arrays; public class GSL {
// Use an array to create the list private String arr[]; private int size;
/** * Constructor for objects of class GSL */ public GSL() { arr = new String[10]; size = 0; }
/* * size method - returns the size of the list * * return - the size of the list as an integer */ public int size() { return size; }
/* * add method - add one value to the list in the next available position * * param - integer to add to the list//change to string to add */ public void add(String value)// changed to add string { if (size == arr.length) // Is arr full? Then expand by 20% { //changed here to String 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++; }
/* * get method - return the value at the specified location in the list
* * param - index into the list for the value to return * return - integer value///Change to string value for return
*/ public String get(int index) //change to string for GSL(was int for GIL) { return arr[index]; }
/* * cler method - empty the list */
public void clear() { //changed to String arr = new String[10]; size = 0; }
/* * insert method - insert new element at indicated index * param - index to insert new element * param - integer value of new element//change to String vlaue */ public void insert(int index, String value) //changed passed parameter as String { // 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)];//changed to String // 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 "); } }
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) != "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(String.valueOf(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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started