140 likes | 161 Views
Initializing Objects. Constructors. It is common to want or need to initialize things when an object is made. The object is made when it is dynamically allocated LLNode head; // This makes a reference (~ pointer) head = new LLNode(); // Dynamic allocation
E N D
Constructors • It is common to want or need to initialize things when an object is made. • The object is made when it is dynamically allocated LLNode head; // This makes a reference (~ pointer) head = new LLNode(); // Dynamic allocation • To perform this initialization a special piece of code called a constructor is automatically executed when the object is instantiated • Constructors look like (but are not) methods. • THEY CANNOT HAVE A RETURN TYPE (NOT EVEN VOID).
Constructors class LLNode { private int data; private LLNode next; public LLNode() { setData(0); setNext(null); } public LLNode(int initData, LLNode initNext) { setData(initData); setNext(initNext); } // Accessors and modifiers as before } // LLNode Note: Just as there can be multiple methods with the same name and different signatures there can be multiple constructors!
Constructors class LLNode { private int data; private LLNode next; public LLNode() { this(0, null); } public LLNode(int initData, LLNode initNext) { setData(initData); setNext(initNext); } // Accessors and modifiers as before The reserved word this always refers to the object in which it is used. Here, it invokes the more general constructor.
Now we have... • A LLNode class which will hold an int as well as a reference (pointer) to another LLNode. • It probably isn’t really apparent what all this “behavior” discussion was all about • So we’ll continue and make a Queue Class! • We peek ahead and see how we’ll use the queue class
Using the Queue Queue q; q = new Queue(); q.enqueue(42); q.enqueue(84); q.enqueue(23); while(! q.isEmpty()) { System.out.println(q.dequeue()); } 42 84 23
Design • We start by considering the data. What is the essential data that a Queue must have? • As noted before head and tail pointers! • So our class in minimal form looks like...
Queue class Queue { private LLNode head; private LLNode tail; // Purpose: constructor // Postcon: head and tail references set to null public Queue() { setHead(null); setTail(null); } // PPP omitted for clarity!!! private void setHead(LLNode h) { head = h; } private void setTail(LLNode t) { tail = t; } private LLNode getHead() { return head; } private LLNode getTail() { return tail; }
Queue (Continues) // Precon: instantiation (or none!) // Purpose: returns true if queue is empty // Postcon: no change to queue public boolean isEmpty() { return (getHead() == null); }
Queue (Continues) // Precon: instantiation (or none!) // Purpose: add a new int to the tail of the queue // Postcon: queue now one element longer public void Enqueue(int newData) { LLNode temp; temp = new LLNode(); temp.setData(newData); if(isEmpty()) { setHead(temp); setTail(temp); } else { getTail().setNext(temp); setTail(temp); } } // Enqueue
Queue (Continues) // Precon: instantiation // Purpose: remove element from head of queue // Postcon: If queue was empty no change returns 0 // If queue non-empty returns head public int Dequeue() { int retVal; if(isEmpty()) { retVal = 0; } else { retVal = getHead().getData(); setHead(getHead().getNext()); if(getHead() == null) setTail(null); } return retVal; } // Dequeue
Queue (Continues) public static void main( String arg[] ) { Queue q; q = new Queue(); q.Enqueue(42); q.Enqueue(84); q.Enqueue(23); while(! q.isEmpty()) { System.out.println(q.Dequeue()); } } } // Queue
Running the Code C:\demo>javac *.java C:\demo>java Queue 42 84 23 C:\demo>