CSE 1020:Aggregation Mark Shtern 1-1. Summary Development process Testing Debugging 1-2.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
Chapter 5: Loops and Files.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Io package as Java’s basic I/O system continue’d.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Writing Classes (Chapter 4)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Composition (Part 2) 1. Class Invariants  class invariant  some property of the state of the object that is established by a constructor and maintained.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
Trader Registration Form Trader Registration Input Printing a Trade Card Adding a New Car Printing a Cars for Sale List Moving Blocked Cars Moving Sold.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
30/10/ Iteration Loops Do While (condition is true) … Loop.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Loops and Files. 5.1 The Increment and Decrement Operators.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
CSE 1020:Using Objects Mark Shtern 1-1. Summary Read API Method binding and overloading Development process Input validation Assert 1-2.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSE 1020:Using API Mark Shtern 1-1. Summary Delegation – Static Method, Object Client view vs Implementer view API, UML Errors Software Engineering Utility.
java.io supports console and file I/O
Abstract Data Type.
Text File Input/Output
I/O Basics.
Chapter 13: File Input and Output
INPUT STATEMENTS GC 201.
Chapter 8: Collections: Arrays
CSE 1020:Programming by Delegation
CSE 1030: Data Structure Mark Shtern.
CSE 1020:Inheritance Mark Shtern.
Basics of OOP A class is the blueprint of an object.
Introduction to Data Structure
CSE 1030: Aggregation and Composition
File Input and Output.
CSE 1020:Software Development
Review for Midterm 3.
Presentation transcript:

CSE 1020:Aggregation Mark Shtern 1-1

Summary Development process Testing Debugging 1-2

Exercises 7.12 Write an app that reads a double x from the user, ensures that it is in [0,1], or else terminates with error message, and then computes and outputs the following sum: x - x 2 /2 + x 3 /3 - x 4 /4 … The apps stop adding when the absolute value of the term is less than Test cases – 0   

Aggregation A class is said to be an aggregate if only of its attribute is an object reference C class reference T class “has-a” relationship: C (the whole) has a T (the part) 1-4

Example 8.1 Determine whether the first can ordinarily be consider an aggregate of the second: – Camera, Film – Vehicle, Car – Library, Book – Animal, Dog – Car, Tree 1-5 V X V X X

UML diagram 1-6

Composition If creating an instance of the aggregate automatically leads to creating an instance of the aggregated part, then the aggregation becomes known as a composition Composition  the aggregate and the aggregated objects have a shared lifetime Only implementer knows whether aggregation is a composition 1-7

Example 8.2 Assume that a Camera has attribute of the type Film. Describe a hypothetical scenario in which aggregation is – A composition – Not a composition 1-8

Aggregate’s Constructor Who does instantiate an aggregated object? – The aggregate’s constructor – The client int number = 15; double cost = 12.25; Stock stock = new Stock(“.AB”); Investment inv = new Investment(stock, number, cost); 1-9

Accessors and Mutators Assessors – getX() Stock stock = inv.getStock(); Mutators – setX(T newInstance) creditCard.setExpiryDate(expiry); 1-10

Example 8.3 Determine whether the getIssueDate accessor of the CreditCard class returns a reference to the actual date embedded in the credit card or to a copy of it 1-11

Aggregate cloning Aliasing Coping – Set y equal to x Shallow Coping – New instance of the aggregate with the same state as given one – Non-primitive attributes are simply aliased Deep Copying – New instance of the aggregate with the same state as given one – Non-primitive attributes are itself deep-copied 1-12

Example 8.4 Given that card is reference to a credit card, clone it using deep copy. You can assume as a precondition that the it is not null. 1-13

Collection Creating the Collection – Static allocation – Dynamic allocation Adding/Removing Elements – The collection is full – The element is already present 1-14

Index Traversal public int size() public type get(int index) for (int i=0; i< bag.size(); i++){ e=bag.get(i); } 1-15

Iterator-Based Traversal Visit all elements without missing one and without visiting the same element more than once Order is unimportant 1-16

Searching found = false; for (E e : bag) { found =found || e.equals(given); } 1-17

Searching found = false; E lastVisited = null; for (int i=0; i<bag.size() && !found; i++) { lastVisited = bag.get(i); found = lastVisited.equals(given); } 1-18

Example 8.5 Write a program that uses the getRandom method of the GlobalCredit class to generate a collection of randomly created credit cards. It should then prompt the user to enter a credit card number and determine whether the card is in the collection. If it is than its balance must be displayed, otherwise a message must be printed. 1-19

Summary Aggregation Composition Index Based traversal Iterator-Based Traversal Copying – Aliasing – Shallow – Deep 1-20

Search Complexity The complexity of a search is defined as the number of tests it must perform in the worst case before it can reach conclusion Traversal-based search is an exhaustive search The complexity of exhaustive search is N Big-O notaion  O(N) 1-21

Complexity and Time execution T(N) ~ aF(N) Problem: – An O(N^2) program runs in 5 sec when N is 1000, – How long will the program run when N is 2000? 1-22

The Search faster than O(N) If the cards are stacked such that their numbers are ordered 1-23

Exercise 7 Write a program that determines whether or not the type.lib.Fresh aggregate is a composition with respect to the Date class

Exercise 9 Write a program that generates a random portfolio of investments using the getRandom method of the Portfolio class Use indexed access to traverse the portfolio (visit all its elements), and output its total market value

Exercise 10 Write a program that generates a random portfolio of investments using the getRandom method of the Portfolio class Use iterator-based access to traverse the portfolio (visit all its elements), and determine which investment has the highest market value

I/O Stream Keyboard input – Standard input – Keyboard  source – Program  data sink – Channel  stream InputStream 1-27

System class in out 1-28

InputStream InputStream keyboard = System.in; int input = keyboard.read(); 1-29

Input Stream BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in)); 1-30

File input Open Stream BufferedReader filer = new BufferedReader (new InputStreamReader(new FileInputStream (fileName))); Reading for (String line = filer.readLine(); line != null; line = filer.readline()) { //process } Close Stream filer.close(); 1-31

Screen Output Standard Output Output Stream Standard Error PrintStream 1-32

File Output PrintWriter FileOutputStream Open file PrintWriter filer = new PrintWriter (new BufferWriter (new OutputStreamWriter (new FileOutputStream(filename))); Close file filer.close() Flush buffer 1-33

Ex 821 Consider the aggregation chain shown below 1-34

Use this chain to create a frame that has a menu bar with two drop-down menu: File and View. The file menu should contain two items: Open and Exit and the view menu should contains one item: Arrange 1-35