1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Interfaces A Java interface is a collection
Inheritance Inheritance Reserved word protected Reserved word super
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
8. Arrays 8.1 Using Arrays 8.2 Reference versus Value Again 8.3 Passing Array Arguments and Returning Arrays 8.4 An Example: Simulating Rolls of the Dice.
Abstract Classes.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Chapter 15: Operator Overloading
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Object Oriented Design and UML
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Chapter 3 Introduction to Collections – Stacks Modified
Introduction Ellen Walker CPSC 201 Data Structures Hiram College.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
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 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Polymorphi sm. 2 Abstract Classes Java allows abstract classes – use the modifier abstract on a class header to declare an abstract class abstract class.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter VII: Arrays.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Polymorphism.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Slides by Steve Armstrong LeTourneau University Longview, TX
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 5: Enhancing Classes
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
ArrayLists 22-Feb-19.
Chapter 8 Inheritance.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Review: libraries and packages
Object-Oriented Design Part 2
Arrays.
Presentation transcript:

1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b What is a list?

2 Abstraction b Data structures should be abstractions: details are hiddendetails are hidden interface separated from implementationinterface separated from implementation b Benefits: manage complexitymanage complexity reusabilityreusability

3 Abstract Data Types b ADT: organized collection of informationorganized collection of information operations for managing informationoperations for managing information b The set of operations define the interface to the ADT b As long as the ADT accurately fulfills the promises of the interface, it doesn't really matter how the ADT is implemented b Objects are a perfect programming mechanism to create ADTs because their internal details are encapsulated

4 Examples of Structures: b Rational: manipulate fractions (LL p196, B p8) b Die, PairOfDice, Card, DeckOfCards…: you know! (LL) b Wordlist: A list of words (I.e., strings) (B p11) b BankAccount: manage account number and value (B p12) b Association: generalization of BankAccount--key/value pair (B 16) b Store: a bunch of things?… (B 18) int size()int size() boolean isEmpty()boolean isEmpty() void clear()void clear() b Collection: ? (B19) b Vector: a list, similar to array (part of java.util) (LL p302, B p36) b Matrix: manipulate 2-D arrays as matrices (B p43)

5 The Collection Interface public interface Collection extends Store { public boolean contains(Object value); // pre: value is non-null // post: returns true iff the collection contains the value public void add(Object value); // pre: value is non-null // post: the value is added to the collection // the replacement policy is not specified. public Object remove(Object value); // pre: value is non-null // post: removes an object "equal" to value within collection. public Iterator elements(); // post: returns an iterator for traversing collection }

6 Pre- and Postconditions b Precondition: the conditions under which a method may be called and expected to produce correct results b Postcondition: the state of the program once the method has completed, provided the precondition was met b Details - see B. chapter 2

7 Interfaces b A Java interface is a collection of abstract methods and constants b An abstract method is a method header without a method body  An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, it is usually left off b An interface is used to formally define a set of methods that a class will implement

8 Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

9 Interfaces b An interface cannot be instantiated b Methods in an interface have public visibility by default b A class formally implements an interface by stating so in the class headerstating so in the class header providing implementations for each abstract method in the interfaceproviding implementations for each abstract method in the interface b If a class asserts that it implements an interface, it must define all methods in the interface or the compiler will produce errors.

10 Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

11 Interfaces b A class that implements an interface can implement other methods as well b See Speaker.java (LL p236) Speaker.java b See Philosopher.java (LL p237) Philosopher.java b See Dog.java (LL p238) Dog.java b A class can implement multiple interfaces b The interfaces are listed in the implements clause, separated by commas b The class must implement all methods in all interfaces listed in the header

12 Polymorphism via Interfaces b An interface name can be used as the type of an object reference variable Doable obj;  The obj reference can be used to point to any object of any class that implements the Doable interface  The version of doThis that the following line invokes depends on the type of object that obj is referring to: obj.doThis();

13 Polymorphism via Interfaces b That reference is polymorphic, which can be defined as "having many forms"  That line of code might execute different methods at different times if the object that obj points to changes b See Talking.java (LL p240) Talking.java b Note that polymorphic references must be resolved at run time; this is called dynamic binding b Careful use of polymorphic references can lead to elegant, robust software designs

14 Interfaces b The Java standard class library contains many interfaces that are helpful in certain situations  The Comparable interface contains an abstract method called compareTo, which is used to compare two objects  The String class implements Comparable which gives us the ability to put strings in alphabetical order  The Iterator interface contains methods that allow the user to move through a collection of objects easily

15 The Vector Class  An object of class Vector is similar to an array in that it stores multiple values b However, a vector stores objectsstores objects does not have the indexing syntax that arrays havedoes not have the indexing syntax that arrays have has methods that can be used to manipulate elementshas methods that can be used to manipulate elements size of vector can change (expand) to fit more elementssize of vector can change (expand) to fit more elements  The Vector class is part of the java.util package b An alternative (equivalent?) implementation is given in Structures package  See Beatles.java (LL p304) Beatles.java Beatles.java

16 The Vector Class  The Vector class is implemented using an array b Whenever new space is required, a new, larger array is created, and the values are copied from the original to the new array b To insert an element, existing elements are first copied, one by one, to another position in the array  An implementation of Vector and other structures: