Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
50 slides
-
Memory Upload • Test methods of class LinkedList • Data
fields of class GroceryMenu • Methods of class GroceryMenu -
Step #3 for LinkedList • Step #3: Debug and test
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 LinkedList.java for a driver (test program) for class LinkedList -
Testing the Constructor • What would be a good way
to test the constructor for the LinkedList class? • The constructor code is quite simple, so we can only instantiate a LinkedList object and print the empty list -
Testing the Constructor • If nothing goes wrong, such as
the program crashing, we will assume that the constructor code has no bugs ListInterface<String> list = new LinkedList<String>(); System.out.print(list.toString()); -
Testing the add() Method • How can we test the
add() method? • Again, we need feedback, so we add one (1) node, and then print the list • We also add several nodes and print -
Testing the add() Method • If nothing goes wrong, we
will assume that the add() method has no bugs list.add(new String("A")); System.out.print(list.toString()); list.add(new String("B")); list.add(new String("C")); list.add(new String("D")); System.out.print(list.toString()); -
Testing the get() Method • How can we test the
get() method? • The get() method is useful for editing, so we should use a list of Items, which have the setNumber() mutator method ListInterface<Item> list2 = new LinkedList<Item>(); list2.add(new Item("apple",3)); list2.add(new Item("banana",5)); list2.add(new Item("carrot",7)); Item food = list2.get(2); food.setNumber(88888); System.out.print(list2); -
Testing the get() Method • Although it would take forever
to test for all possible code combinations, at a minimum, we want to test for any boundary conditions for each method • For example, since it is easy to be off by 1, the main() method of program LinkedList.java has code that tests for n=0, 1, size, and size+1 for the get(n) method -
Testing the remove() Method • Similar to the tests for
the other methods, the main() method calls the remove() method several times and then prints the list to check for any bugs in the program • For example, we want to make sure that remove(2) removes the 2nd item in the list • We also want to check for boundary conditions, such as trying to remove an item in the size+1 position -
Testing the remove() Method • Test code for the remove()
method that removes middle, last, and first node from a list ListInterface<String> list = new LinkedList<String>(); list.add(new String("A")); list.add(new String("B")); list.add(new String("C")); list.remove(2); System.out.print(list.toString()); list.remove(2); System.out.print(list.toString()); list.remove(1); System.out.print(list.toString()); -
Testing the remove() Method • We can also test removing
from the size+1 position, by using a try-catch block that catches a ListException Integer currentSize=list2.getSize(); try { list2.remove(currentSize + 1); } catch (ListException e) { System.out.println(e.toString()); } -
ListException • Class ListException is a very small class, so
it is included at the end of the ListInterface.java file • Class ListException is tested in the main() method of class LinkedList -
Why Test? • You are probably thinking now that testing
code is not necessary if we are careful in writing our code • However, no matter how careful you are, it is easy to create bugs • Since our code is broken into several interdependent classes, testing the classes as we make them will reduce the number of bugs in our final program -
Step #2 for GroceryMenu • Step #2: Design classes with
data fields and methods (and an algorithm for each method) to create objects • See GroceryMenu.java for data fields and methods of this class -
GroceryMenu Data Fields • We have two (2) data fields
for our GroceryMenu class • A linked list to store grocery items private LinkedList<Item> groceryList = new LinkedList<Item>(); • The name of our database which stores the grocery list data on the hard disk private String database = new String("database.csv"); -
GroceryMenu Constructor • Since the data fields have already been
initialized, the constructor does not have any code • This is my style of programming, as most textbook code initializes the data fields in the constructor public GroceryMenu(){ } -
GroceryMenu main() Method • The main() method in the class
GroceryMenu is quite short • We simply initialize a GroceryMenu object, and call method displayLoop() • The main() method does have to be the longest method in your program public static void main(String[] args) { GroceryMenu menu = new GroceryMenu(); menu.displayLoop();} -
Memory • This is a representation of the menu object
in memory GroceryMenu menu = new GroceryMenu(); menu "database.csv" groceryList database 0 head size null -
Algorithm for displayLoop() 1. Load data from file to linked
list 2. Loop until user wants to exit a. Prompt the user to enter a choice b. Choice #1: add item to list c. Choice #2: edit item in list d. Choice #3: delete item from list e. Choice #4: display list f. Choice #5: exit the program 3. Write data from linked list to file -
Code for displayLoop() • Exceptions are caught in this method
• Taking advantage of inheritance, we use the top superclass in the exception class hierarchy, which is class Exception, to catch any exception that might happen try{ this.readFromDatabase(); } catch(Exception exception){ JOptionPane.showMessageDialog( null, exception); } -
Code for displayLoop() • The method showOptionDialog() of class JOptionPane
is somewhat complicated, because it has many parameters • Please read my comments in my code as well as the Java API explanation to see what parameters are required • The method allows the user to press a button and returns the button’s number -
Code for displayLoop() • Inside the while loop, we have
several if statements that call the appropriate method depending on the user’s choice • Most methods are surrounded by try-catch blocks just in case an exception might occur -
Code for displayLoop() • After the while loop, we write
the grocery list data to a database, so it can be stored on the hard disk • This way, we can use our grocery list again when we start up the program -
Algorithm for addToList() 1. Prompt the user for the item’s
name 2. Prompt the user for item’s number 3. Convert the number from a String to an Integer 4. Instantiate (create) an Item object 5. Add new Item object to grocery list -
Big-O for addToList() • Big-O is O(n), because O(1) +
O(1) + O(1) + O(1) + O(n) = O(n) 1. Prompt user for item’s name O(1) 2. Prompt user for item’s number O(1) 3. Convert the number from a String to an Integer O(1) 4. Instantiate (create) Item object O(1) 5. Add new Item object to grocery list O(n) -
Memory for addToList() • After adding 2 items to menu
object menu "database.csv" groceryList database 2 head size data next data next null name number negi 7 name number natto 5 -
Algorithm for editList() 1. Prompt the user for item’s position
2. Convert the position from a String to an Integer 3. Get the address of the item in list 4. Prompt the user for item’s number 5. Convert the number from a String to an Integer 6. Set the number of the item -
Big-O for editList() • The Big-O is O(n), because each
step of algorithm is O(1) + O(1) + O(n) + O(1) + O(1) + O(1) = O(n) 1. Prompt user for item’s position O(1) 2. Convert from String to Integer O(1) 3. Get address of the item in list O(n) 4. Prompt user for item’s number O(1) 5. Convert from String to Integer O(1) 6. Set the number of the item O(1) -
Memory for editList() Item groceryItem = groceryList.get(2); groceryItem menu "database.csv"
groceryList database 2 head size data next data next null name number 7 name number natto 5 negi -
Memory for editList() groceryItem.setNumber(14); groceryItem menu "database.csv" groceryList database 2
head size data next data next null name number negi 14 name number natto 5 -
Exceptions for editList() • Method editList() could throw several exceptions
• Method get() of class LinkedList will throw ListException if position is less than 1 or greater than list size, or if the list is empty • Method setNumber() of class Item will throw ItemException if number is less than 1 -
Exceptions for editList() • Since these exceptions are NOT caught
inside method editList(), the exception will propagate to method displayLoop(), and then caught here try{ this.editList(); } catch(Exception exception){ JOptionPane.showMessageDialog(null, "Unable to edit item in grocery list."); } -
Algorithm removeFromList() 1. Prompt the user for item’s position 2.
Convert the position from a String to an Integer 3. Remove the item from the grocery list -
Big-O removeFromList() • The Big-O is O(n), because, if we
add up the Big-O for each step of the algorithm, we get: O(1) + O(1) + O(n) = O(n) 1.Prompt user for item’s position O(1) 2.Convert the position from a String to an Integer O(1) 3.Remove the item from the grocery list O(n) -
Big-O for displayList() • Since method displayList() calls method toString(),
which loops through all the grocery items, the Big- O is O(n) -
Algorithm Read Database 1. Set up a file connection 2.
Create Scanner object for file input 3. Declare variables position, name, and number 4. Remove 1st line (header) from file 5. While the file still has another line a. Read one line from the file b. Break line into tokens separated by ", " c. Get the position from the line -
Algorithm Read Database d. Get the name from the line
e. Get the number from the line f. Instantiate (create) a new item g. Add the new item to the grocery list 6. Close the connection to the file -
Big-O readFromDatabase() 1. Set up a file connection O(1) 2.
Create Scanner object O(1) 3. Declare variables position, name, and number O(1) 4. Remove 1st line from file O(1) 5. While file still has another line O(n) a. Read one line from the file O(1) b. Break line into tokens O(1) c. Get the position from the line O(1) -
Big-O readFromDatabase() d. Get the name from the line O(1)
e. Get the number from the line O(1) f. Instantiate (create) a new item O(1) g. Add new item to the grocery list O(n) 6. Close the connection to the file O(1) Add Big-O for each step: O(1) + O(1) + O(1) + O(1) + O(1) + O(n) * (O(1) + O(1) + O(1) + O(1) + O(1) + O(1) + O(n)) + O(1) = O(n) * O(n) = O(n2) -
Code readFromDatabase() • We use the while loop to keep
reading one line at a time from the file, which is text file "database.csv" File fileConnection = new File(database); Scanner fileReader = new Scanner(fileConnection); String line = fileReader.nextLine(); while(fileReader.hasNextLine()){ line = fileReader.nextLine(); . . . } -
Code readFromDatabase() • We use another Scanner object to break
each line into tokens (words or numbers), which are separated by a comma and a space (", ") line = fileReader.nextLine(); Scanner tokenInput = new Scanner(line).useDelimiter(", "); position = tokenInput.next(); name = tokenInput.next(); number = tokenInput.next(); Item newItem = new Item(name, Integer.parseInt(number) ); groceryList.add(newItem); -
Algorithm writeToDatabase() 1. Open a file connection 2. Write the
column headings to the file 3. Store all data in list in a string 4. Loop from 0 to length of string a. Get one character from the string b. If the character is a newline ("\n"), write println() to file c. If character is NOT newline ("\n"), write character to file 5. Close the file connection -
Big-O writeToDatabase() 1. Open a file connection O(1) 2. Write
column headings to file O(1) 3. Store all data in list in a string O(n) 4. Loop from 0 to length of string O(n) a. Get one character from the string O(1) b. If newline, write println() to file O(1) c. If NOT newline, write character O(1) 5. Close the file connection O(1) -
Big-O writeToDatabase() • If we add the Big-O for each
step of the algorithm, then we get O(n) • O(1) + O(1) + O(n) + O(n) * (O(1) + O(1) + O(1)) + O(1) • O(n) + O(n) * O(1) • O(n) + O(n) • O(n) -
Code writeToDatabase() • To make a newline in a file
using class PrintWriter, we have to use println() instead of print("\n”) for(int i=0;i<allData.length();i++){ String character = allData.substring(i, i+1); if(character.equals("\n")){ fileWriter.println(); } else{ fileWriter.print(character); } } -
Testing GroceryMenu • Since the class GroceryMenu uses a GUI
(graphical user interface) for input and output, I tested the program by entering the data myself and correcting the bugs that I found -
Program GroceryMenu • Make sure you run the program GroceryMenu,
so you understand what the program does • Also, look through the code of classes GroceryMenu, LinkedList, Item, and Node to make sure you understand how each line of code of each method works -
Memory Defragmenter • Test methods of LinkedList class • Class
GroceryMenu data fields • Class GroceryMenu methods main(), displayLoop(), addToList(), editList(), removeFromList(), displayList(), readFromDatabase(), and writeToDatabase() -
Task Manager • Before the next class, you need to:
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