1 / 14

Initializing Objects

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

melvyn
Download Presentation

Initializing Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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); q.enqueue(84); q.enqueue(23); 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) // 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) // 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

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

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

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

More Related