class, we created an array-based grocery list • Now that we know about object oriented programming (OOP) and linked nodes, let’s see how to apply these concepts to create another grocery list that will demonstrate the advantages of OOP
program 1. Identify the objects and their attributes and behaviors 2. Design classes with data fields and methods (and an algorithm for each method) to create the objects 3. Debug and test the classes as you write the classes
their attributes, and behaviors • Object #1: grocery items • Attributes: name of item and number of items • Behaviors: create an item, display an item, and edit the item’s number
data fields and methods (and an algorithm for each method) to create objects • See Item.java for data fields and methods of class Item Data fields: String name and Integer number Methods: Item(), toString(), and setNumber()
the classes as you write the classes • This is done by simply writing a main method that tests all of the constructors and methods of a class • See main() method in Item.java for a driver (test program) for class Item
fields and methods of class LinkedList • Data fields: Node<T> head, which points to linked nodes • Methods: LinkedList(), add(), get(), delete(), and toString()
a LinkedList (linked nodes) class with a Node class? • The LinkedList class has reference variable called “head”, which points to the first node in the list of linked nodes • If the list is empty, “head” is null • After nodes are added to the list, the “next” data field of the last node will be null
Node! • Data field head is a variable • Data field head stores the location (address) of the first node ListInterface<String> list = new LinkedList<String>(); list 0 head size null
LinkedList, head will point to a node, which will point to the object String letter=new String("A"); list.add(letter); list letter 1 head size A data next null
item to beginning of list a. Assign head to a node that points to the item 2. If list NOT empty, add to end of list a. Keep track of both the previous and current nodes b. While current node is not past end of list (while current is NOT null) i. assign previous node to current node ii. assign current node to next node
how the add() method adds String object "C" to the linked list letter = new String("C"); list.add(letter); letter list 2 head size A data next data next null B C
track of the previous and current node, as we loop to the end of the list Variable current is used to stop the loop when current becomes null Because current becomes null, current will not point to anything We need previous to point to the last node, so we can point this node to the new last node
an item to the beginning of list, so O(1) • However, in the worst case, we add to the end of the linked list, so O(n) • This is because we have to loop through all the nodes to access the last node • Therefore, Big-O for add() is O(n)
2. If position is less than 1, or larger than list size, throw ListException 3. Find the node for given position a. Initialize Integer variable counter to 1 b. Initialize Node variable current to head c. While counter NOT equal to position i. Point current to next node ii. Increment counter
memory model to understand how the get() method works • Assume we already added Strings "A", "B", and "C" to a list of Strings list 3 head size A data next data next B C data next null
specific object in the list • For example, we add 2 items to the list, and then we want to change the item’s number for the 2nd item • The following slides show how this is done in memory by using the “box and balloon” model
node from the front, so O(1) • In the worst case, we get a node from the end, so O(n) • Since we have to loop to end of the linked list, we loop roughly n times • So Big-O for get() method is O(n)
design loops, because it is easy for your program to have a bug when your loop tests for equals or NOT equals • To compare objects, use equals() method • To compare addresses, use == (or !=) operators • To compare two primitive data types, use == (or !=) operators
method uses != operator in the while loop to compare the address of the Node variable current to null • We want to loop to the end of the list, when current becomes null Node<T> previous = head; Node<T> current = head.getNext(); while(current != null){ previous = current; current = current.getNext();}
the get() method uses equals() method in the while loop to compare counter and position, which are Integer objects • We want to loop position number of times to get the appropriate node public T get(Integer position){ . . . Integer counter = new Integer(1); . . . while(!counter.equals(position)){ . . .
data type int in in the get() method, then we would use the != operator in the while loop • The code would change Integer to int and equals() to != public T get(int position){ . . . int counter = 1; . . . while(counter != position){ . . .
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture