Presentation is loading. Please wait.

Presentation is loading. Please wait.

Initializing Objects.

Similar presentations


Presentation on theme: "Initializing Objects."— Presentation transcript:

1 Initializing Objects

2 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).

3 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!

4 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.

5 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

6 Using the Queue Queue q; q = new Queue(); q.enqueue(42);
while(! q.isEmpty()) { System.out.println(q.dequeue()); } 42 84 23

7 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...

8 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; }

9 Queue (Continues) // Purpose: returns true if queue is empty
// Precon: instantiation (or none!) // Purpose: returns true if queue is empty // Postcon: no change to queue public boolean isEmpty() { return (getHead() == null); }

10 Queue (Continues) // Purpose: add a new int to the tail of the queue
// 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); } } // Enqueue

11 Queue (Continues) // Purpose: remove element from head of queue
// 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

12 Queue (Continues) Queue q; q = new Queue(); q.Enqueue(42);
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

13 Running the Code C:\demo>javac *.java C:\demo>java Queue 42 84
23 C:\demo>

14


Download ppt "Initializing Objects."

Similar presentations


Ads by Google