Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.

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

COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
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.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 9 Concepts of Programming Languages
Stacks, Queues, and Deques
Abstract Classes and Interfaces
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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.
Stacks And Queues Chapter 18.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Lecture 10: Finishing Off. Overview Stacks, Queues, Lists Generics More about Exceptions Sorting Techniques.
Lecture 9: Technique. Exercise Create a method called duplicates that returns a boolean value. The method takes, as its argument, an array of strings.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
April 27, 2017 COSC Data Structures I Review & Final Exam
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Lecture 10: Object Oriented Programming Tami Meredith.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Java Collections Framework The client view. The Big Picture.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
The List ADT.
Chapter 18: Stacks and Queues.
Week 4 - Friday CS221.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Lecture 9 Concepts of Programming Languages
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Stacks, Queues, and Deques
Java Programming Language
searching Concept: Linear search Binary search
Stacks.
Stacks, Queues, and Deques
structures and their relationships." - Linus Torvalds
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Lecture 8: Advanced OOP Part 2

Overview Review of Subtypes Interfaces Packages Sorting

Exercise public class flag { private boolean raised; // Constructor public flag () { raised = false; } // Public interface methods public boolean isFlying() { return (raised); } public void raise () { raised = true; } public void lower () { raised = false; } } // end class flag Add a copy constructor to this class. That is, add a second constructor that takes a flag object and returns a copy of the object.

Solution public class flag { private boolean raised; // Constructor public flag () { raised = false; } public flag (flag f) { this.raised = f.raised; } // Public interface methods public boolean isFlying() { return (raised); } public void raise () { raised = true; } public void lower () { raised = false; } } // end class flag

Subtyping When we extend a class to create a sub-class, that sub- class can be used in place of its parent/super class We say the extended class is a subtype of its ancestor parent superclass We can access the parent using the keyword super the same way we can access ourself using this A child has its own instance (copy) of the parent class and is said to inherit the methods and data of the parent The child can override the parents methods with its own versions

Which One? public class person {... } public class student extends person {... } person p = new student(); p.display(); Is display called for a person (the type of p ) or for a student (the type of the object stored in p )? It is called for student. It is the OBJECT not the storage location that matters here!

Subtypes We can extend types (e.g., classes) by adding stuff to them to create subtypes The subtype/subclass has everything the supertype/superclass (its parent) has, but it has extra methods and data Two important concepts 1. Implementation Inheritance: A subclass inherits and can use all the methods and data fields of its parent 2. Inclusion Polymorphism: An instance of a subclass can be used anywhere an instance of the parent (superclass) can be used

Single Inheritance Consider a seaplane – it can be both a boat and a plane It would be nice to have it as a subclass of both Boats and Planes (multiple inheritance = multiple parents) Java DOES NOT permit this A class can have ONE direct parent Single inheritance = one parent only Seaplane must be a Boat OR a Plane Vehicle BoatsPlanes Seaplane

The Problem with Hierarchies PersonStudentFull-TimeUndergradGradPart-TimeUndergradGrad PersonStudentUndergradFull-TimePart-TimeGradFull-TimePart-Time

Interfaces A lot of the limitations of single inheritance can be bypassed using (Java) interfaces We say that an ADT has an interface (how its used) and an implementation. It is possible to store the interface in its own file and separate it from the implementation An interface can be implemented by more than one class!

Example public interface flyable { public boolean isFlying(); public void raise (); public void lower (); } // end interface flyable public class flag implements flyable {... } public class kite implements flyable {... }

Multiple Interfaces Having a class implement an interface tells other programmers the things it can do E.g., since it implements flyable, programmers know that flag has a raise method A class can implement MORE THAN ONE interface In this way, we could have a floatable interface, and thus a seaplane could implement both flyable and floatable Interfaces can also be extended

Packages A package is a named collection of classes E.g., java.util and java.io are packages (the. is just part of the name here!) If you want a class to be part of a package you must put package name; as the first line of code (comments and blank lines may come before it since they are not code) The package may then be imported by other classes and its classes used

Packages are needed when code is put into a different directory Normally Java has been looking in the current directory for all your classes You use a package if you want to move something to a different directory The jvm has a classpath variable that it uses to search for packages Directories

Search Search is a very common need when programming A prototypical example would be, "does this array contain the value x" Efficiency generally tends to be an issue in search We search often We search large arrays To measure efficiency of search, we count the number of comparisons we perform – fewer is generally better!

Exercise Write a method called search that returns the index value of any occurrence of val in the array data. public static int search (int[] data, int val) {... }

Improving Search How can we improve this? Well, what if the data was sorted... Check the middle item and then we know if the value is above or below that – One check and we've removed half of the possible values! Check the middle of the remaining range, and we can delete 1/2 the remaining values or 1/4 of the total values If we do this to completion, we will take at most log 2 (data.length) comparisons at most

Binary Search Example log 2 (4) = 2log 2 (8) = 3log 2 (16) = 4 log 2 (32) = 5log 2 (64) = 6log 2 (128) = 7 These are maximums, on average it is less!

Binary Search – Implemented public static int bsearch (int[] data, int val) { int lo = 0, hi = data.length – 1, mid; while (lo <= hi) { mid = (lo + hi) / 2; if (val < data[mid]) { hi = mid - 1; } else if (val > data[mid]) { lo = mid + 1; } else { return (mid); } return (-1) ; } // end bsearch()

The Catch... Binary search really is far more efficient, there's not really anything superior when you have sorted data But, we have to add the cost of the sort to the search! If we search a lot, the sort cost is amortized over many searches and is not very significant If we search once, particularly on small data sets, then linear search can be more efficient Halving the problem is a popular programming technique that can be used in many places

Exercise Here is a search method. Modify it so that it throws an exception rather than returning -1 if the value isn't found. public static int search (int[] data, int val) { for (int i = 0; i < data.length; i++) { if (data[i] == v) return (i); } return (-1); }

Solution public static int search (int[] data, int val) throws Exception { for (int i = 0; i < data.length; i++) { if (data[i] == v) return (i); } throw new Exception ("Search failure"); } // end search

Queues A Queue is an ADT that functions like a bank line-up. Where a stack was FILO (first in, last out), a queue is FIFO (first in, first out) Instead of push and pop, the interface methods are enqueue and dequeue We usually implement a queue using a "circular array" That is, when we hit the back of the array, we wrap around to the front of the array.

Exercise Implement a queue given the following interface: public interface FIFO { public void enqueue(int val) throws Exception; public int dequeue() throws Exception; public int count(); } count returns the number of items currently in the queue. Add a constructor that initialises an empty queue to a provided size. (Note, interfaces in Java can't contain constructors).

Solution public class queue implements FIFO { private int numItems, head, tail; private int[] data; public queue (int size) { data = new int[size]; head = 0; tail = 0; numItems = 0; } public void enqueue(int val) throws Exception { if (numItems == data.length) throw Exception ("Overflow"); data[tail++] = val; numItems += 1; tail = tail % data.length; } public int dequeue() throws Exception { int v; if (numItems == 0) throw new Exception ("Underflow"); v = data[head++]; head = head % data.length; numItems -= 1; return (v); } public int count() { return (n); } }

To Do Read Chapter 8 on Advanced OOP Read Chapter 9 on Exceptions We are almost done EVERYTHING up to 10.3 Do Assignment 8 in the lab today