/*********************************************** * CS2510 Fall 2011 * Lecture #16 * ArrayList, BST, Parameterized Methods cont. ***********************************************/ // Get the ArrayList from the java libraries import java.util.ArrayList; import tester.*; // Information about a Transformer class Transformer { String name; Appliance appliance; Transformer(String name, Appliance appliance) { this.name = name; this.appliance = appliance; } } // A simple representation of an appliance class Appliance { String name; Appliance(String name) { this.name = name; } } /* Will continue next lecture class BST { ArrayList list; BST() { } T get(int i) { } }*/ class LectureExamples { // Some Transformers Transformer optimus = new Transformer("Optimus",new Appliance("Wheeled Toaster")); Transformer megatron = new Transformer("Megatron",new Appliance("Blender")); Transformer bumblebee = new Transformer("Bumblebee",new Appliance("Food processor")); Transformer none = new Transformer("None",new Appliance("No Spoon")); // Our List of Transformers ArrayList alist = new ArrayList(); LectureExamples() { reset(); } // Make a clean state for our tests void reset() { this.alist.clear(); this.alist.add(optimus); this.alist.add(megatron); this.alist.add(bumblebee); } // Tests the different methods of an ArrayList void testArrayLists(Tester t) { this.reset(); t.checkExpect(this.alist.get(0).name,"Optimus"); t.checkExpect(this.alist.get(2).name,"Bumblebee"); t.checkException(new IndexOutOfBoundsException("Index: 3, Size: 3"),alist,"get",3); this.alist.remove(2); t.checkExpect(this.alist.size(),2); this.alist.add(optimus); t.checkExpect(this.alist.get(2).name, "Optimus"); this.alist.remove(optimus); this.alist.remove(this.alist.indexOf(optimus)); t.checkExpect(this.alist.contains(none),false); this.reset(); this.alist.set(0, megatron); t.checkExpect(this.alist.indexOf(megatron),0); } // Testing our Search method void testSearch(Tester t) { this.alist.clear(); this.alist.add(bumblebee); this.alist.add(megatron); this.alist.add(optimus); t.checkExpect(this.search("Bumblebee",alist,0,alist.size()),bumblebee); t.checkExpect(this.search("Optimus",alist,0,alist.size()),optimus); } // Testing our swap method void testSwap(Tester t) { this.reset(); this.swap(alist,0,2); t.checkExpect(this.alist.get(0).name,"Bumblebee"); t.checkExpect(this.alist.get(2).name,"Optimus"); } // swap: Swaps 2 elements in the list by index void swap(ArrayList list, int i, int j) { /* Traditional method X tmp = list.get(i); list.set(i, list.get(j)); list.set(j,tmp); */ list.set(i, list.set(j, list.get(i))); } // search : Uses binary search to find a name in a list of Transformers // Assumes list is a binary search tree Transformer search(String name, ArrayList list, int low, int high) { int middle = (low + high) /2; if (low>high) { throw new RuntimeException("No luck, so sad."); } if(list.get(middle).name == name) { return list.get(middle); } else { if (name.compareTo(list.get(middle).name) < 0) { return this.search(name,list,low,middle-1); } else { return this.search(name,list,middle+1,high); } } } }