Download presentation
Presentation is loading. Please wait.
Published byChloe Victoria Gordon Modified over 8 years ago
1
CSE 1020:Aggregation Mark Shtern 1-1
2
Summary Development process Testing Debugging 1-2
3
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 10 -3 Test cases – 0 0 0.5 0.41 1 0.69 1-3
4
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
5
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
6
UML diagram 1-6
7
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
8
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
9
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
10
Accessors and Mutators Assessors – getX() Stock stock = inv.getStock(); Mutators – setX(T newInstance) creditCard.setExpiryDate(expiry); 1-10
11
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
12
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
13
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
14
Collection Creating the Collection – Static allocation – Dynamic allocation Adding/Removing Elements – The collection is full – The element is already present 1-14
15
Index Traversal public int size() public type get(int index) for (int i=0; i< bag.size(); i++){ e=bag.get(i); } 1-15
16
Iterator-Based Traversal Visit all elements without missing one and without visiting the same element more than once Order is unimportant 1-16
17
Searching found = false; for (E e : bag) { found =found || e.equals(given); } 1-17
18
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
19
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
20
Summary Aggregation Composition Index Based traversal Iterator-Based Traversal Copying – Aliasing – Shallow – Deep 1-20
21
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
22
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
23
The Search faster than O(N) If the cards are stacked such that their numbers are ordered 1-23
24
Exercise 7 Write a program that determines whether or not the type.lib.Fresh aggregate is a composition with respect to the Date class
25
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
26
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
27
I/O Stream Keyboard input – Standard input – Keyboard source – Program data sink – Channel stream InputStream 1-27
28
System class in out 1-28
29
InputStream InputStream keyboard = System.in; int input = keyboard.read(); 1-29
30
Input Stream BufferedReader buffer = new BufferedReader (new InputStreamReader(System.in)); 1-30
31
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
32
Screen Output Standard Output Output Stream Standard Error PrintStream 1-32
33
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
34
Ex 821 Consider the aggregation chain shown below 1-34
35
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.