Initializing Objects.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Based on Java Software Development, 5th Ed. By Lewis &Loftus
So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Building a Linked List in Java. Linked List In the Procedural Paradigm a linked list consisted of: –A pointer to the head of the list –Nodes (in dynamic.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Road Map Introduction to object oriented programming. Classes
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
Data Structures - Queues
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
More on Polymorphism. Ever have one of those days?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
The Queue Data Structure Mugurel Ionu Andreica Spring 2012.
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky
Introduction to Classes and Objects. Real Life When a design engineer needs an electrical motor he doesn’t need to worry about –How a foundry will cast.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
The Vector Class. Vector Class Write a Vector class. This will be a container class that will hold objects. It should allow the following operations:
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Linked Data Structures
GC211 Data structure Lecture 3 Sara Alhajjam.
Review Array Array Elements Accessing array elements
Lecture No.09 Data Structures Dr. Sohail Aslam
QueueStack CS1020.
CC 215 Data Structures Queue ADT
Week 4 - Friday CS221.
Doubly Linked List Review - We are writing this code
Stack and Queue APURBO DATTA.
Java collections library
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Lecture 21 Stacks and Queues Richard Gesick.
Memory Organization Process 1 (browser) Code Process 3 (word)
Lesson Objectives Aims
Recap Week 2 and 3.
Queues CSC212.
Stacks and Queues.
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
Lecture 16 Stacks and Queues CSE /26/2018.
Queues Definition of a Queue Examples of Queues
Ordered Structures Wellesley College CS230 Lecture 10
CSCS-200 Data Structure and Algorithms
Lecture 16 Stacks and Queues CSE /26/2018.
Revised based on textbook author’s notes.
Presentation transcript:

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 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); 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) // 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); }

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

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

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

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