Aggregation. Chapter 8. Java By Abstraction Chapter 8. Outline. 8.1 What is Aggregation? Copyright 2006 Pearson Education Canada Inc.

Similar documents
Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

About this exam review

Lecture 11.1 I/O Streams

Lecture 4: Exceptions. I/O

Chapter 6 Introduction to Defining Classes

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Objects and State. COMP1400 Week 9. Wednesday, 19 September 12

Objects and Iterators

B-Trees. Based on materials by D. Frey and T. Anastasio

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

CSC Java Programming, Fall Java Data Types and Control Constructs

Classes, interfaces, & documentation. Review of basic building blocks

Java Review. Fundamentals of Computer Science

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

For this section, we will implement a class with only non-static features, that represents a rectangle

This exam is open book. Each question is worth 3 points.

PIC 20A Streams and I/O

Introduction to Programming Using Java (98-388)

CMSC 341 Lecture 7 Lists

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Inheritance and Interfaces

Collections, Maps and Generics

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Understanding class definitions

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Programming II (CS300)

Today s Agenda. Quick Review

HST 952. Computing for Biomedical Scientists Lecture 8

COURSE DESCRIPTION. John Lewis and William Loftus; Java: Software Solutions; Addison Wesley

MIDTERM REVIEW. midterminformation.htm

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Ticket Machine Project(s)

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Active Learning: Streams

CS 251 Intermediate Programming Java I/O Streams

Exam 1 - (20 points)

Byte and Character Streams. Reading and Writing Console input and output

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

OUTCOMES BASED LEARNING MATRIX

And Even More and More C++ Fundamentals of Computer Science

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Programming for Engineers in Python

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

COMP 250 Winter 2011 Reading: Java background January 5, 2011

CSE wi: Practice Midterm

CS 2113 Software Engineering

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

An Introduction to C++

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Recitation 3 Class and Objects

Total Score /1 /20 /41 /15 /23 Grader

CSE 214 Computer Science II Java Classes and Information Hiding

Internet Technology 2/7/2013

Armide Documentation. Release Kyle Mayes

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Abstract Classes, Exceptions

Chapter 13 Abstract Classes and Interfaces

CSCI-140 Midterm Review October 10, 2015 Presented by the RIT Computer Science Community

Solutions to Exercises in Chapter 9

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Final exam. CS 2110, December 15, 2016, 9:00AM

CMSC131 Final Exam Practice Questions

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CMSC 202H. Classes and Objects: Reusing Classes with Composition

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

CS 302 Week 9. Jim Williams

Lecture 8. Object Oriented Design

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

CSE wi Final Exam 3/12/18. Name UW ID#

Class design guidelines. Most of this material comes from Horstmann, Cay: Object-Oriented Design & Patterns (chapter 3)

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Do not turn to the next page until the start of the exam.

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Defining Classes and Methods

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

M11/5/COMSC/HP2/ENG/TZ0/XX/M MARKSCHEME. May 2011 COMPUTER SCIENCE. Higher Level. Paper pages

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

CS 112 Programming 2. Lecture 10. Abstract Classes & Interfaces (1) Chapter 13 Abstract Classes and Interfaces

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

ISY00245 Principles of Programming. Module 7

TaxiBot New attributes Variables Math! TaxiBot

Chapter 9 Inheritance

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Input & Output in Java. Standard I/O Exception Handling

Unit 9: Network Programming

Transcription:

Chapter 8 Aggregation Java By Abstraction 8- Outline 8. What is Aggregation? 8.. Definition and Terminology 8..2 The Aggregate s Constructor 8..3 Accessors and Mutators 8..4 The Client s Perspective 8..5 Case Study: I/O Streams 8..6 Case Study: Graphics 8.2 Working with Collections 8.2. Creating the Collection 8.2.2 Adding/Removing Elements 8.2.3 Indexed Traversals 8.2.4 Iterator-Based Traversals 8.2.5 Searching 8.2.6 Search Complexity Java By Abstraction 8-2 8. What is Aggregation? If one of the attributes of a class C is an object reference of type T *, then C is an aggregate and T is the aggregated part. Every instance of C must have an instance of T (or else the attribute would be null). Aggregation = has-a * T!= String Java By Abstraction 8-3

Examples (a) CDPlayer CD (b) Wallet * Bill (c) 4 Wheel Car Radio Java By Abstraction 8-4 8.. Definition and Terminology Multiplicity Variable Multiplicity Collections (part=element) Composition (shared lifetime) The Camera Film Relation Java By Abstraction 8-5 Examples CreditCard 2 Date Calendar Date Java By Abstraction 8-6 2

Examples (a) Investment Stock (b) Portfolio * Investment Java By Abstraction 8-7 8..2 The Aggregate s Constructor When a client instantiates C, who instantiates T? Create an Investment Create a CreditCard What signature (for the Investment constructor) makes Investment a composition? Java By Abstraction 8-8 8..3 Accessors and Mutators Aggregates must provide an accessor thru which the part can be accessed In a composition, the accessor returns a clone of the part An aggregate may provide a mutator so the client can mutate the part In a non-composition, such a mutator is not needed (why?) Java By Abstraction 8-9 3

8..4 The Client s Perspective Aggregation = Layered Abstraction Sounds like an implementer s concern Why don't implementers hide it? If they did: q Investment would have to handle symbol, name, and price q CreditCard would have to accept day, month, and year. Java By Abstraction 8-0 Example-: Copying an Aggregate Given a reference x to an aggregate, make a copy of it and call it y. Java By Abstraction 8- Example-: Copying an Aggregate Given a reference x to an aggregate, make a copy of it and call it y. Three different copies: An Alias A Shallow Copy A Deep Copy Java By Abstraction 8-2 4

8..5 Case Study: I/O Streams InputStream InputStreamReader BufferedReader read(): int reads one byte read(): int reads one character readline(): String reads one line Input Stream Reader Buffered Reader BufferedReader buffer = new BufferedReader( new InputStreamReader(System.in)); Java By Abstraction 8-3 File Input: InputStream read(): int reads one byte InputStreamReader BufferedReader FileInputStream read(): int reads one character readline(): String reads one line read(): int reads one byte BufferedReader filer = new BufferedReader( new InputStreamReader( new FileInputStream(filename)); Java By Abstraction 8-4 8..6 Case Study: Graphics JFrame X Container UniPanel Graphics2D Java By Abstraction 8-5 5

Graphics JFrame getcontentpane(): Container setcontentpane(container) Container add(component) Java By Abstraction 8-6 Graphics Container add( Component) type:lib::unipanel getgraphics2d() getwidth(): int getheight(): int repaint() * Java By Abstraction 8-7 Graphics type:lib::unipanel getgraphics2d() getwidth(): int getheight(): int repaint() Graphics2D getcolor(): Color getfont(): Font getstroke(): Stroke setcolor(color) setfont(font) setstroke(stroke) Java By Abstraction 8-8 6

Graphics Graphics2D getcolor(): Color getfont(): Font getstroke(): Stroke setcolor(color) setfont(font) setstroke(stroke) Color Font Stroke Java By Abstraction 8-9 8.2 Working with Collections Collection Statically Allocated Dynamically Allocated List Set List Set Java By Abstraction 8-20 8.2. Creating the Collection Cannot specify elements as parameters Create an empty one then populate Constructor Summary - Portfolio Portfolio(java.lang.String title, int capacity) Construct an empty portfolio having the passed name and capable of holding the specified number of investments. Constructor Summary - GlobalCredit GlobalCredit() Construct a GC processing centre having the name "NoName". Java By Abstraction 8-2 7

8.2.2 Adding / Removing Elements All collections provide a void or a boolean add to enable clients to populate. These methods are boolean for diff reasons: Method Summary - Portfolio boolean add(investment inv) Attempt to add the passed investment to this portfolio. Method Summary - GlobalCredit boolean add(creditcard card) Attempt to add the passed credit card to this GCC. Java By Abstraction 8-22 8.2.3 Indexed Traversals Traversal in lieu of accessors Traverse = Visit each element once. Don t miss and don t over-visit. Indexed = Pretend the elements are numbered (0 offset). Two methods: get(int) and size() Java By Abstraction 8-23 Example of an indexed traversal Given a reference x to a Portfolio, list all its investments in a tabular fashion: Inv. Market Book Net 00 3450.00 2870.00 580.00 002 450.00 500.00-50.00........ -------------------------------------- Total Java By Abstraction 8-24 8

8.2.4 Iterator-Based Traversals More abstract than indexed Relies on the enhanced for loop Works if the collection implements Iterable for (E e : bag) { // visit element e } Java By Abstraction 8-25 Example of a chained traversal Given a reference x to a GlobalCredit, list all its credit cards in a tabular fashion: Card No Balance Exp 36m? 90732-5 76.85 67282-8.64 46484-0 34.49 < 75597-2 232.43...... ---------------------------------- The last column indicates if the card will expire within 36 months Java By Abstraction 8-26 8.2.5 Searching Searching can be done via a traversal: Set up a traversal loop In each iteration, compare the element we are searching for with an element of the collection. Set a boolean flag accordingly The result (found or not found) must be somehow remembered after the loop is exited. Java By Abstraction 8-27 9

A search example: Given a reference gc to a random GlobalCredit, determine whether a given card c is in it. Attempt # (incorrect): boolean found = false; for (CreditCard card : gc) { found = card.equals(c); } Java By Abstraction 8-28 A search example, cont. Correct it by adding the loop invariant: The value of found is the same as the sentence: c is equal to one of the elements seen so far Attempt #2 (correct): boolean found = false; for (CreditCard card : gc) { found = found card.equals(c); } Java By Abstraction 8-29 8.2.6 Search Complexity Traversal-based search is Exhaustive N comparisons in the worst case. It is thus a linear search A bag contains N numbered balls and you can pick one ball one at a time. Can you determine if ball number 55 is in the bag by picking less than N times? In the worst case? Java By Abstraction 8-30 0

Search Complexity Traversal-Based search: O(N). Complexity of an algorithm can be: O(), O(lgN), O(N), O(N 2 ) O(2 N ), O(N!) Can break the O(N) barrier by prearranging the elements in some manner Sorting, Hashing, Tree structures can lead to sub-linear search complexity. GlobalCredit offers a non-exhaustive search. It is sub-linear Java By Abstraction 8-3