170 likes | 202 Views
Review Question 1. Every instance of the Fraction class has a long numerator. We do not call the relation between Fraction and the long type an aggregation. Why not?. What is aggregation?.
E N D
Review Question 1 • Every instance of the Fraction class has a long numerator. • We do not call the relation between Fraction and the long type an aggregation. • Why not?
What is aggregation? • If one of the attributes of a class C is an object reference of type T (other than String), then C is an aggregate and T is the aggregated part. Aggregation = has-a
Review Question 2 • Which class is the aggregate? • Is it a collection? • What can be said about the number/type of attributes in the aggregate? • Is this a composition? A B 3
What is composition? • An aggregation where the aggregate and the parts have a shared lifetime • Constructed together • Destroyed (orphaned) together
Review Question 4 • The accessor to a part in a composition should not return a reference to the actual part instance but to a copy of it • Explain why not
Review Question 6 • Explain the meaning of “aliasing an object”
Review Question 7 • Given an instance of an aggregate, suppose that someone copied the block it occupies in memory, bit by bit to a new block • Argue that the new block would be a shallow copy of the aggregate, not a deep one
Review Question 8 • Given two instances of a class whose attributes are all public, how can you determine whether one instance is a shallow copy of the other?
Exercise 7 • Write a program that determines whether or not the type.lib.Fresh aggregate is a composition with respect to the Date class
Review Question 20 • If a collection is statically allocated, then what should be passed to its constructor? static = compile-time dynamic = run-time
Review Question 21 • Can you add an element to a collection even if it is already in it? Set collections - No List collections - Yes
Review Question 22 • What happens if you attempt to add an element to a full, statically allocated collection?
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
Review Question 28 • A linear search takes 5 sec in the worst case on a collection of 5,000 elements • How long will it take in the worst case on a collection of 20,000 elements? Linear search = complexity O(N) T(N) ≈ N
Review Question 29 • Repeat question 28 but for a search algorithm with O(N1/2) complexity
Review Question 30 • Repeat question 29 but for a search algorithm with O(lgN) complexity • lg = base 2 logarithm