// Hw9ListOfStrings.java // // Do not modify the Hw9ListOfStrings main class. Make this code work // by adding the appropriate classes with the appropriate methods as // described by the test methods called by the main method. // Note that you do not have to add any code to the test cases, though // you do have to solve them with general-purpose solutions (and not just // hard-code the example test cases!). // Hint: to solve this incrementally, you may wish to comment out // parts of the test code so the parts you have implemented will compile // and can be tested as you go. import java.util.Arrays; public class Hw9ListOfStrings { public static void main(String[] args) throws Exception { testListOfStringsClass(); } private static void testListOfStringsClass() { System.out.print("Running ListOfStrings class tests... "); // default constructor: ListOfStrings list = new ListOfStrings(); assert(list.getCapacity() == 1); // default initial capacity is 1 assert(list.getSize() == 0); // no elements in the list to start assert(list.toString().equals("<>")); // on a whim, we'll use angle-brackets // as delimiters for our list values // For a "real" arraylist implementation, you would never actually // expose the underlying (or "backing") array. But here we will // so that we can test if it is working properly. To signify that // this method is really not for external use, we'll preface its // name with an underscore (_getArray): assert(Arrays.toString(list._getArray()).equals("[null]")); // add an element with room to spare list.add("Larry"); assert(list.getCapacity() == 1); // there was room for it! assert(list.getSize() == 1); // one element in the list assert(list.toString().equals("")); // there it is! assert(Arrays.toString(list._getArray()).equals("[Larry]")); // get the nth element assert(list.get(0).equals("Larry")); assert(list.get(1) == null); // return null if the index is >= size assert(list.get(-1) == null); // return null if the index is negative // add an element requiring doubling capacity from 1 to 2 list.add("Moe"); assert(list.getCapacity() == 2); // we doubled capacity assert(list.getSize() == 2); // but we're still full now! assert(list.toString().equals("")); assert(Arrays.toString(list._getArray()).equals("[Larry, Moe]")); assert(list.get(0).equals("Larry")); assert(list.get(1).equals("Moe")); // add an element requiring doubling capacity from 2 to 4 list.add("Curly"); assert(list.getCapacity() == 4); assert(list.getSize() == 3); assert(list.toString().equals("")); assert(Arrays.toString(list._getArray()).equals("[Larry, Moe, Curly, null]")); // add an element once again with room list.add("Shemp"); assert(list.getCapacity() == 4); assert(list.getSize() == 4); assert(list.toString().equals("")); assert(Arrays.toString(list._getArray()).equals("[Larry, Moe, Curly, Shemp]")); // add an element requiring doubling capacity from 4 to 8 list.add("Joe"); assert(list.getCapacity() == 8); assert(list.getSize() == 5); assert(list.toString().equals("")); assert(Arrays.toString(list._getArray()).equals( "[Larry, Moe, Curly, Shemp, Joe, null, null, null]")); // remove the String at the given index -- do not shrink the capacity, // but you must shift the other elements to the left so all the unused/null // elements remain on the right. Also, remove should return a boolean, // which is true if the remove succeeded, and false otherwise (because // the index was out of bounds). assert(list.remove(2) == true); // we'll remove "Curly" (at element 2) assert(list.getCapacity() == 8); assert(list.getSize() == 4); assert(list.toString().equals("")); assert(Arrays.toString(list._getArray()).equals( "[Larry, Moe, Shemp, Joe, null, null, null, null]")); assert(list.remove(-1) == false); // out of bounds assert(list.remove(4) == false); // ditto // A couple more removes and adds and we're done! assert(list.remove(2) == true); assert(list.toString().equals("")); assert(list.remove(2) == true); assert(list.toString().equals("")); assert(list.remove(2) == false); list.add("Curly"); assert(list.getCapacity() == 8); assert(list.getSize() == 3); assert(list.toString().equals("")); assert(Arrays.toString(list._getArray()).equals( "[Larry, Moe, Curly, null, null, null, null, null]")); System.out.println("Passed all tests!!!"); } } ////////////////////////////////// // ListOfStrings class ////////////////////////////////// class ListOfStrings { // ADD CODE HERE!!! }