Okay...SO I have the code written. It compiles and it runs...but it doesn't do what it's supposed to -__- I realize that's not helpful so heres a description of my assignment:
UML Diagram:
OABlist
-data: ABLNode[] //create private static class that takes name/number as data
-size : int
-capacity: int
+OABlist () //creates an empty list with capacity = 5
+size(): int //returns the list size
+addorReplace (in name: String, in number : int) : boolean //adds entry according to alphabetical order of name. if node exists, update phone number without creating additional entry
+getNumber(in name : String) : int //returns phone number corresponding to name or -1 if name not in list. Method must be faster than linear.
+remove(in name: string) :int //returns phone number corresponding to name or -1 if name is not on the list. node storing this info is removed from list. also must be faster than linear
+showList() : void //prints all the elements in the list (name and number)
My code:
public class OABlist{ private static class ABLNode{ public String name; public int number; public ABLNode (String in_name, int in_number){ name = in_name; number = in_number; } } private ABLNode[] data; //phoneentry private int size; private int capacity; private static final int initial_cap = 5; public OABlist(){ size =0; capacity = initial_cap; data = new ABLNode[capacity]; } public int size() { return size; } private int getName(String name) { for (int i = 0; i < size; i++) { if (data[i].name.equals(name)){ System.out.println(data[i].name); return i;// The name found at index i } } return -1; // The name DNE in list. } public int getNumber(String name) { int j = getName(name); if (j == -1) return -1; // name not found else return data[j].number; } public boolean addorReplace(String name, int number){ if (name == null || number == -1) throw new IllegalArgumentException("Name/Number are null."); int i = getName(name); if (i >= 0) { data[i].number = number;// Name exists in position i, replace number. } else { //add new name/number to list. if list = full, create larger array int j = 0; while (j < size){ if (name.compareToIgnoreCase(data[j].name) >= 0){// name is bigger j++; } else{ data[j] = new ABLNode(name, number); j++; } } } if(size == capacity){ ABLNode[] newPair = new ABLNode[2 * size]; System.arraycopy(newPair,0,data,0,capacity); data = newPair; } return true; } public int remove(String name){ if (name != null){ String temp = name; if (size == 1) name = null; else if (name == data[capacity].name) // last name = data[capacity - 1].name; else name = data[size].name; size--; } else{ System.out.println("No one to remove"); } return data[size].number; } public void showList(){ int i; for(i = 0; i < size; i++){ System.out.println("List: " + data[i].name + " " + data[i].number ); } } public static void main(String[] args){ OABlist myList = new OABlist(); myList.addorReplace("Joe", 123); myList.addorReplace("Jim", 123); myList.addorReplace("Al", 123); myList.addorReplace("Bill", 123); myList.addorReplace("Carl", 123); myList.showList(); } }