Debugging Linked List Class Static - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

Debugging Linked List Class Static

Description:

Then we'll write the add, traverse, find and delete methods (plus helpers if necessary) ... ell.traverse('Should be 111 222 333 444'); ell.deleteFirst(222) ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 74
Provided by: BillL161
Category:

less

Transcript and Presenter's Notes

Title: Debugging Linked List Class Static


1
DebuggingLinked List Class Static
Lecture 22
2
Debugging Java Code
3
Debugging
  • Beginning programmers typically find just getting
    a program to compile a big challenge
  • Error messages are often meaningless
  • Error messages are often in the wrong place
  • Error messages often suggest a course of action
    which is dead wrong
  • Sometimes the excitement of getting the program
    to compile leads the programmer to forget that
    the program should also work.
  • What can go wrong
  • Wrong answer
  • Illegal operation (exceptions)

4
Strategies
  • Write it right the first time. It isnt easier to
    find errors later!
  • The compiler only finds language errors not logic
    errors.
  • Read the error message carefully. Sometimes they
    contain useful information.
  • A null pointer exception is not mysterious!
  • Queue q
  • q null
  • q.enqueue(5)

Null Pointer Exception
5
Strategies
  • Let the computer help you find the errors
  • In every class define a constant
  • public final static boolean DEBUG true
  • When things arent working add lines of code like
    this
  • if(DEBUG)
  • System.out.println
  • (methodgt location variable variable)
  • Keep adding these statements until the source of
    the problem is found
  • Errors are normally caused by the computer doing
    what you told it to do!
  • When the code works...change DEBUG to false
  • Dont remove the if(DEBUG) statements

6
The Main
  • A typical Java program consists of a bunch of
    class files.
  • An obvious question might be, How does it all
    start up?
  • No matter how many classes are available, one of
    them must be sent to the JVM to start things off.
  • The mechanism is simple. Any file sent to the JVM
    must have a public static method called main.
    Thats where the action starts

7
The Main for Debugging
  • Starting with main has an additional benefit.
  • As each class is written it can have its own main
    sometimes known as a debugging or test main.
  • This test main should test all the methods in the
    class paying special attention to special or
    limiting cases.
  • Once testing is complete Leave the test main in
    the source file!
  • It wont have any effect and may be beneficial if
    later modifications are made to the class.

8
The Main for Debugging
class Driver ... public static void
main(String args) ...
class Stack ... public static void
main(String args) ...
class Menu ... public static void main(String
args) ...
class Widget ... public static void
main(String args) ...
class Blivet ... public static void
main(String args) ...
9
Debugging
  • Write code in small chunks
  • Compile and test frequently (whenever possible)
  • Use the test main in every class!
  • Lets create a linked list class that will hold
    Student Records.
  • Well start by creating a StudentRecord class.

10
StudentRecord
  • class StudentRecord
  • public String name
  • public double gpa
  • public int ssn
  • public StudentRecord(String n,
  • double g, int s)
  • name n
  • gpa gpa
  • ssn s

11
toString or ! toString
  • You may recall that System.out.println takes as a
    parameter a String.
  • So why does it work if we say
  • System.out.println(5)
  • Java converts it for us!
  • Java also has a similar behavior when the
    argument is a reference to an object
  • If we type
  • StudentRecord sr
  • // Make one here...
  • System.out.println(sr)
  • We really get
  • System.out.println(sr.toString())

12
So the wise programmer...
  • Always define a toString() method in every class.
  • THE toString() METHOD DOES NOT PRINT ANYTHING
  • it merely returns a String to its caller
  • If you omit the toString() method, Java will use
    a default toString() which probably wont help
    you to debug.
  • What should toString() look like?

13
StudentRecord toString
  • // Precon fields should be initialized
  • // Purpose return string representation
  • // Postcon no change to StudentRecord
  • public String toString()
  • String retVal
  • retVal name
  • retVal gpa
  • retVal ssn
  • return retVal

14
Now the test main!
  • // Purpose test main
  • public static void main(String args)
  • StudentRecord sr
  • sr new StudentRecord(
  • "George Burdell", 4.0,
    987654321)
  • System.out.println(
  • "Should be George Burdell 4.0 987654321
    " sr)
  • sr.name "Bill Gates"
  • sr.ssn 123456789
  • sr.gpa 0.3
  • System.out.println(
  • "Should be Bill Gates 0.3 123456789 "
    sr)
  • // StudentRecord

15
And the test!
  • C\demogtjava StudentRecord
  • Should be George Burdell 4.0 987654321 George
    Burdell 0.0 987654321
  • Should be Bill Gates 0.3 123456789 Bill Gates
    0.3 123456789
  • C\demogt

16
What's wrong?
  • class StudentRecord
  • public String name
  • public double gpa
  • public int ssn
  • public StudentRecord(String n,
  • double g, int s)
  • name n
  • gpa gpa
  • ssn s

17
Corrected
  • class StudentRecord
  • public String name
  • public double gpa
  • public int ssn
  • public StudentRecord(String n,
  • double g, int s)
  • name n
  • gpa g
  • ssn s

18
Under the Hood
  • A detail that we need to clarify
  • In Pseudocode, we defined a record
  • Now we define a class
  • In Pseudocode, we could make record on the heap
  • Now we can make an object on the heap
  • In Pseudocode, the new operator returned a a
    value we stored in a pointer variable
  • Now the new command will return a value that
    well store in a reference

19
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

20
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

w1
21
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

w1
w2
22
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

w1
widget object (1)
w2
23
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

w1
widget object (1)
w2
24
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

widget object (2)
w1
widget object (1)
w2
25
Example
  • Assume we have a class Widget
  • Widget w1
  • Widget w2
  • w1 new Widget()
  • w2 w1
  • w1 new Widget()
  • w2 null

widget object (2)
w1
w2
26
Questions?
27
Building a Linked List in Java
28
Linked List
  • In the Procedural Paradigm a linked list
    consisted of
  • A pointer to the head of the list
  • Nodes (in dynamic memory i.e. the heap)
    containing data and additional next pointers
  • Modules that would perform a variety of functions
    as needed add (in order), traverse, find,
    delete, etc.
  • In the Object Oriented Paradigm a linked list
    will consist of a class which contains
  • The head pointer (globally scoped in the class)
  • Methods to perform the function listed above
  • We will also need a class to hold the nodes.
    Well start with that...

29
Node
  • class Node
  • StudentRecord data
  • Node next
  • public Node(StudentRecord data)
  • this.data data // !!!!!!!!!!!!!!!!!!!!!!!!!!
  • this.next next
  • // Constructor
  • public String toString()
  • if(data null)
  • return "Node null"
  • else
  • return "Node " data.toString()

30
Are we done?
  • What about the test main???

31
Remember the main(e)
  • public static void main(String args)
  • StudentRecord sr
  • new StudentRecord("Bob", 3.5, 123456789)
  • Node n1 new Node(null)
  • System.out.println("Empty node test\n" n1)
  • // Load up and print
  • n1.data sr
  • System.out.println("Bob n1)
  • sr new StudentRecord("Mary", 3.7, 987654321)
  • Node n2 new Node(sr)
  • n1.next n2
  • System.out.println("Bob n1)
  • System.out.println("Mary n2)
  • // main
  • // Node

32
Lets see whats happening
33
Tracing
  • StudentRecord sr
  • new StudentRecord("Bob", 3.5, 123456789)

name "Bob" gpa 3.5 ssn 123456789
sr
34
Tracing
  • Node n1 new Node(null)

name "Bob" gpa 3.5 ssn 123456789
sr
data
n1
next
35
Tracing
  • n1.data sr

name "Bob" gpa 3.5 ssn 123456789
sr
data
n1
next
36
Tracing
  • sr new StudentRecord("Mary", 3.7, 987654321)

name "Bob" gpa 3.5 ssn 123456789
name "Mary" gpa 3.7 ssn 987654321
sr
data
n1
next
37
Tracing
  • Node n2 new Node(sr)

name "Bob" gpa 3.5 ssn 123456789
name "Mary" gpa 3.7 ssn 987654321
sr
data
n1
next
data
n2
next
38
Tracing
  • n1.next n2

name "Bob" gpa 3.5 ssn 123456789
name "Mary" gpa 3.7 ssn 987654321
sr
data
n1
next
data
n2
next
39
Key to Understanding Java
  • Understand that the "variables" that you may
    think of as objects (not primitives) are ALWAYS
    references (like pointers) to objects which live
    in the heap.
  • The objects live and die immobile in the heap
  • All the apparent movement of objects is just the
    moving, copying, setting-to-null of references

40
Review
  • We have created and tested two classes
  • class StudentRecord
  • class Node
  • We now construct the class LinkedList
  • Well start with the fields and simple
    accessors/modifiers
  • Then well write the add, traverse, find and
    delete methods (plus helpers if necessary)

41
LinkedList
  • class LinkedList
  • private Node head
  • public LinkedList()
  • head null
  • // constructor

42
LinkedList (add method)
  • // Purpose add in order by SSN
  • // Postcon list will contain one additional
    item
  • public void add(StudentRecord sr)
  • if(head null
  • head.data.ssn gt sr.ssn )
  • Node temp new Node(sr)
  • temp.next head
  • head temp
  • else
  • add( head, sr )
  • // add

43
LinkedList (add helper method)
  • private void add(Node cur, StudentRecord sr)
  • if( cur.next null
  • cur.next.data.ssn gt sr.ssn )
  • Node temp new Node(sr)
  • temp.next cur.next
  • cur.next temp
  • else
  • add(cur.next, sr)
  • // add

44
LinkedList (traverse )
  • // Purpose traverse list
  • // Postcon no change to list
  • public void traverse()
  • traverse(head)
  • // traverse
  • // Purpose traverse helper
  • private void traverse(Node cur)
  • if(cur ! null)
  • System.out.println(cur)
  • traverse(cur.next)
  • // traverse

45
LinkedList (traverse )
  • // Purpose print out string passed in as
    parameter
  • // then traverse list (useful for
  • // debugging)
  • // Postcon No change to list
  • public void traverse(String s)
  • System.out.println(s)
  • traverse()
  • // traverse

46
LinkedList (traverse iteratively )
  • // Purpose traverse iteratively (just shown for
  • // comparison
  • // Postcon No change to list
  • public void traverseI()
  • Node cur head
  • while(cur ! null)
  • System.out.println(cur)
  • cur cur.next
  • // traverseI
  • public void traverseI(String s)
  • System.out.println(s)
  • traverseI()
  • // traverseI

47
LinkedList (find )
  • // Purpose Locate record by SSN
  • // Postcon No change to list
  • public StudentRecord find(int targSsn)
  • return find(head, targSsn)
  • // find
  • // Purpose Find helper
  • private StudentRecord find(Node cur, int
    targSsn)
  • if(cur null) return null
  • else
  • if(cur.data.ssn targSsn)
  • return cur.data
  • else
  • return find(cur.next, targSsn)
  • // find

48
LinkedList (deleteFirst)occurence
  • // Purpose delete first occurence of record
    with
  • // matching SSN
  • // Postcon If SSN found record will be removed
    thus
  • // list will be one shorter
  • public void deleteFirst(int targSsn)
  • if( head ! null )
  • if( head.data.ssn targSsn)
  • head head.next
  • else
  • deleteFirst( head, targSsn)
  • // deleteFirst

49
LinkedList (deleteFirst)occurence
  • // Purpose delete first occurence helper
  • private void deleteFirst(Node cur, int targSsn)
  • if(cur.next ! null)
  • if(cur.next.data.ssn targSsn)
  • cur.next cur.next.next
  • else
  • deleteFirst(cur.next, targSsn)
  • // deleteFirst

50
LinkedList (deleteAll)occurences
  • // Purpose delete all occurences matching a
    target
  • // SSN
  • // Postcon All matching occurences will be
  • // eliminated
  • public void deleteAll(int targSsn)
  • // (Extra Credit!!!)

51
LinkedList (main)
  • public static void main(String args)
  • LinkedList ell new LinkedList()
  • ell.traverse("Empty list traversal")
  • ell.add(new StudentRecord("Adam", 3.0, 333))
  • ell.add(new StudentRecord("Bozo", 2.0, 222))
  • ell.add(new StudentRecord("Carl", 1.0, 444))
  • ell.traverseI("Should be 222 333 444")
  • ell.add(new StudentRecord("Doug", 0.0, 111))
  • ell.traverse("Should be 111 222 333 444")
  • ell.deleteFirst(222)
  • ell.traverseI("Should be 111 333 444")
  • ell.deleteFirst(999)
  • ell.deleteFirst(333)
  • ell.deleteFirst(111)
  • ell.deleteFirst(444)
  • ell.traverse("Empty list???")
  • // main
  • // LinkedList

52
Application
  • / Demo application to allow user to add, find,
    list,
  • and delete student records
  • /
  • class Application
  • // Purpose print menu and get user choice
  • // Postcon returns choice as int
  • public static int menuChoice()
  • System.out.println("Enter 1 to add")
  • System.out.println("Enter 2 to find")
  • System.out.println("Enter 3 to list")
  • System.out.println("Enter 4 to delete")
  • System.out.println("Enter 5 to quit")
  • return IOGadget.readInt("Choice")

53
Application
  • // Purpose get information to fill student
    record
  • // Postcon returns filled StudentRecord
  • public static StudentRecord getSR()
  • String name IOGadget.readLine("Name")
  • double gpa IOGadget.readDouble("GPA")
  • int ssn IOGadget.readInt("SSN")
  • return new StudentRecord(name, gpa, ssn)

54
Application
  • // print menu and fulfill request
  • public static void menuloop()
  • LinkedList list new LinkedList()
  • int choice
  • do
  • choice menuChoice()
  • if(choice 1)
  • list.add(getSR())
  • else if(choice 2)
  • StudentRecord sr
  • list.find(IOGadget.readInt("SSN
    "))
  • if(sr null)
  • System.out.println("Not found")
  • else
  • System.out.println(sr)

55
Application
  • else if(choice 3)
  • list.traverse("Student List")
  • else if(choice 4)
  • int ssn IOGadget.readInt("SSN?")
  • list.deleteFirst(ssn)
  • else if(choice 5)
  • System.out.println("Exiting")
  • else
  • System.out.println("Illegal choice")
  • while(choice ! 5)
  • // menuloop

56
Application
  • public static void main(String args)
  • menuloop()
  • // class Application

57
Diagram
class Application menuloop LinkedList
list main menuloop()
class LinkedList LLNode head methods
class LLNode StuRec data LLNode next
methods
Instance of
Instance of
Instance of
LinkedList object head
LLNode object StuRec data LLNode next
LLNode object StuRec data LLNode next
58
Questions?
59
Static?
60
Static
  • Not dynamic
  • class Widget
  • static int s
  • int d // dynamic
  • // or instance
  • // variable

61
Instantiation
  • Widget w1 new Widget()
  • Widget w2 new Widget()
  • Widget w3 new Widget()
  • There are now 4 variables
  • w1.d
  • w2.d
  • w3.d
  • Widget.s, w1.s, w2.s, w3.s are all the same!

62
A Picture
w1
class Widget static int s int d
w2
w3
63
class Widget int d static int s
public static void main(String args)
Widget w1 new Widget() Widget w2 new
Widget() Widget w3 new Widget()
w1.d 101 w2.d 202 w3.d 303
Widget.s 999
System.out.println("w1.d " w1.d " w1.s "
w1.s) System.out.println("w2.d "
w2.d " w2.s " w2.s)
System.out.println("w3.d " w3.d " w3.s "
w3.s)
w1.d 101 w1.s 999 w2.d 202 w2.s 999 w3.d
303 w3.s 999
64
Why
  • Think of static variables as living in the class
  • Dynamic variables live in the object
  • BUT note that the objects can refer to the static
    variables with no problem

65
Note
  • The dynamic or instance variables were all
    accessed using their reference.
  • The static (sometimes called class) variables can
    be accessed using a reference or the class name

66
So why static methods?
  • A method that only refers to static variables and
    which could be invoked using
  • class.method()
  • must be marked static

67
Perhaps an example?
68
class Widget public static final int
CAPACITY 100 int serial static int
count public Widget() count serial
count // how many more must be made?
public static int makeMore()
return CAPACITY count // how
many made since this one? public int
newer() return count serial
public String toString() return "Widget "
serial " of " count //
continued
69
class Widget public static final int
CAPACITY 100 int serial static int
count public Widget() count serial
count // how many more must be made?
public static int makeMore()
return CAPACITY count // how
many made since this one? public int
newer() return count serial
public String toString() return "Widget "
serial " of " count //
continued
Static methods refer only to static data
Dynamic methods refer to both static and dynamic
data
70
// continuing class Widget public static void
main(String args) Widget w1 new
Widget() Widget w2 new Widget()
System.out.println(w1 w1)
System.out.println(w2 w2)
System.out.println(Total made count)
System.out.println(Need to make
makeMore() more) System.out.println(
There are w1.newer()
widgets newer than w1)
71
// continuing class Widget public static void
main(String args) Widget w1 new
Widget() Widget w2 new Widget()
System.out.println(w1 w1)
System.out.println(w2 w2)
System.out.println(Total made count)
System.out.println(Need to make
makeMore() more) System.out.println(
There are w1.newer()
widgets newer than w1)
Why not Widget.count?
Would w2.makeMore() work?
72
Questions?
73
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com