Cmp Sci 187: Midterm Review Based on Lecture Notes.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Java Review Interface, Casting, Generics, Iterator.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Chapter 3 Stacks.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Fall 2007CS 225 Program Correctness and Efficiency Chapter 2.
Stacks, Queues, and Deques
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable, class name, method name, etc. Operand: a named memory.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Topic 3 The Stack ADT.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Lists and the Collection Interface Review inheritance & collections.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
Coming up: Inheritance
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
April 27, 2017 COSC Data Structures I Review & Final Exam
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Click to edit Master text styles Stacks Data Structure.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
THURSDAY, OCTOBER 17 IN LAB
Java Programming Language
Introduction to Data Structure
Presentation transcript:

Cmp Sci 187: Midterm Review Based on Lecture Notes

2 What Did We Cover ? Basic Java (review) Software Design (Phone Directory) Correctness and Efficiency: Exceptions, Testing, Efficiency (Big-O) Inheritance and Class Hierarchies Lists and the Collection Interface Building Block for Fundamental Data Structures Stacks: Perhaps the Simplest Data Structure Queues: The Second Simplest

3 Classes and Objects The class is the unit of programming A Java program is a collection of classes A class describes objects (instances) Describes their common characteristics: is a blueprint Thus all the instances have these same characteristics These characteristics are: Data fields for each object Methods (operations) that do work on the objects

4 Methods: Referencing and Creating Objects You can declare reference variables They reference objects of specified types Two reference variables can reference the same object The new operator creates an instance of a class A constructor executes when a new object is created Example: String greeting = ″Hello″; value greetings H e l o String char []

5 Abstract Data Types, Interfaces A major goal of software engineering: write reusable code Abstract data type (ADT): data + methods A Java interface is a way to specify an ADT Names, parameters, return types of methods No indication of how achieved (procedural abstraction) No representation (data abstraction) A class may implement an interface Must provide bodies for all methods of the interface

6 Java 5 Generics ArrayList = new ArrayList (); Inner Classes Block Scoping, can make use of fields of outer class Static nested class Auto (Un)Boxing Primitive wrapped object

7 Exceptions Categories of program errors Why you should catch exceptions The Exception hierarchy Checked and unchecked exceptions The try - catch - finally sequence Throwing an exception: What it means How to do it

8 The Class Throwable Throwable is the superclass of all exceptions All exception classes inherit its methods Throwable ErrorException AssertionError RuntimeException Other Error Classes Checked Exception Classes UnChecked Exception Classes

9 Efficiency Big-O notation What it is How to use it to analyze an algorithm’s efficiency

10 Efficiency Examples for (int i = 1; i < n; i *= 2) { do something with x[i] } Sequence is 1, 2, 4, 8,..., ~n. Number of iterations = log 2 n = log n.

11 Inheritance Inheritance and how it facilitates code reuse How does Java find the “right” method to execute? (When more than one has the same name...) Defining and using abstract classes Class Object : its methods and how to override them

12 A Superclass and a Subclass Consider two classes: Computer and Laptop A laptop is a kind of computer: therefore a subclass variables of Computer and all subclasses additional variables for class Laptop (and its subclasses) methods of Computer and all subclasses additional Methods for class Laptop (and its subclasses) String maker String cpu int ram int disk int getRam() int getDisk() String toString() double lcd double weight double getlcd() Laptop Computer

13 Is-a Versus Has-a Relationships Confusing has-a and is-a leads to misusing inheritance Model a has-a relationship with an attribute (variable) public class C {... private B part;...} Model an is-a relationship with inheritance If every C is-a B then model C as a subclass of B Show this: in C include extends B : public class C extends B {... }

14 Class Object Object is the root of the class hierarchy Every class has Object as a superclass All classes inherit the methods of Object But may override them boolean equals(Object o) String toString() int hashCode() Object clone()

15 Inheriting from Interfaces vs Classes A class can extend 0 or 1 superclass Called single inheritance An interface cannot extend a class at all (Because it is not a class) A class or interface can implement 0 or more interfaces Called multiple inheritance

16 Inheritance Java does not implement multiple inheritance Get some of the advantages of multiple inheritance: Interfaces Delegation Sample class hierarchy: drawable shapes

17 Collection Hierarchy Collection Queue List AbstractList AbstractCollection Iterable AbstractSequential Collection VectorArrayList StackLinkedList

18 Lists (1) The List interface Writing an array-based implementation of List Linked list data structures: Singly-linked Doubly-linked Circular Implementing List with a linked list The Iterator interface hasNext(), next(), remove() Implementing Iterator for a linked list

19 Implementing an ArrayList Class KWArrayList : simple implementation of ArrayList Physical size of array indicated by data field capacity Number of data items indicated by the data field size 0 Size Cap -1 OccupiedAvailable

20 Implementing ArrayList.add(E) 0 Size Cap -1 OccupiedAvailable 0 Size Cap -1 OccupiedAvailable

21 Implementing ArrayList.add(int,E) 0 Size Occupied Available index

22 Implementing ArrayList.remove(int) 0 Size Occupied

23 Linked List next = = prev data = null Node next = = prev data = Node next = = prev data = Node TomSue String DLList head size=2 ListNode Element

24 Implementing DLList With a “Dummy” Node DLList head = next = = prev data = null Node The “dummy” node is always present Eliminates null pointer cases Even for an empty list Effect is to simplify the code Helps for singly-linked and non-circular too

25 Implementing DLList Circularly DLList head = next = = prev data = null Node next = = prev data = “Tom” Node next = = prev data = “Sue” Node Next Prev Next Prev

26 DLList Insertion DLList head = next = = prev data = null Node next = = prev data = “Tom” Node next = = prev data = “Sue” Node Next Prev Next Prev next = = prev data = “Pat” Node Prev Next

27 DLList Removal DLList head = next = = prev data = null Node next = = prev data = “Tom” Node next = = prev data = “Sue” Node Next Prev Next Prev next = = prev data = “Pat” Node Prev Next

28 Stacks The Stack data type and its four methods: push(E), pop(), peek(), and empty() How the Java libraries implement Stack How to implement Stack using: An array A linked list Using Stack in applications Finding palindromes Testing for balanced (properly nested) parentheses Evaluating arithmetic expressions

29 PostFix Form * Input Stack Output //1 1 1 //2 + + //3 2 2 //4 * * //5 3 3 //6 + === * + //7 + //8 4 4 //9 + //10

30 Evaluate Postfix * Input Stack //1 1 1 // // //4 * 6 1 //5 + 7 // // //8 11 //9

31 Queue (1) Representing a waiting line, i.e., queue FIFO The methods of the Queue interface: offer, remove, poll, peek, and element Implement the Queue interface: Singly-linked list Circular array (a.k.a., circular buffer) Doubly-linked list

32 Queue (2) Applications of queues: Simulating physical systems with waiting lines... Using Queues and random number generators