Question: Something is wrong. Code works but was told that get and insert methods are not correctly validating the index value. Must be between 0 and
Something is wrong. Code works but was told that get and insert methods are not correctly validating the index value. Must be between 0 and one less than the size Need Help Please
Code
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 "); }
//first new method set // 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 set(int index, String value) { if((index >=0) && (index < size)) { arr[index] = value; } else { System.out.println("Out of bounds/ERROR"); //for(int i =0; i < size;i++) //{ //arr[index]=value; } }
public void remove(int index) //second new method remove // 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. { if((index >=0) && (index < size)) { for (int i = index; i < size() - 1; i++) { arr[i] = arr[i + 1]; } --size; } else { System.out.println("Out of Bounds/ERROR"); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
