3.6 Passing and returning objects 3.6.1 Questions 1. What is aliasing? Are there possible problems with aliasing? 2. How do you send a message to an object? 3. What "pass by value" means? Explain how passing arguments works in Java. 4. What is the difference between shallow copy and deep copy? 5. What is wrong in the following code? classMyObject implements Cloneable public Object clone() public String toString() } 6. How many methods has the Cloneable interface? What are the reasons of this interface existence? - 7. Is C++ special keyword const available in Java? 8. What is the difference between a read-only object and an immutable object? What is an immutable object? Give an example of a read-only class. 9. What will be the results of the following code? public class StringUtil public static void main(String[] args) } 3.6.2 Problems 1. Implement the following classes and add deep cloneability to all classes via cloning. Class Point attributes: int x, int y methods: setters, getters, draw, clone Class Line attributes: Point point1, Point point2 methods: setters, getters, draw, clone Class Circle attributes: Point center, int radius methods: setters, getters, draw, area, clone Class Shape attributes: Circle circle, Line line methods: draw, clone 2. Implement arithmetical operations on complex numbers having the form a+i*b using the mutable-companion class technique. Implement a method that adds the result of the expression (2+3*i)*(4+5*i)*(6+7*i) to the object owner of the method. 3. Implement the clone method of class Shape using Java serialization. 3.7 Collection of Objects 3.7.1 Questions 1. What are the main differences between an array and an ArrayList? 2. There is a good practice in OO programming not to have public attributes. Think of a good reason why the designers of Java have broken this rule and have exposed the length attribute of arrays? 3. Which of the following are ordered collections and which ones are not: a. HashSet b. TreeSet c. WeakHashMap d. TreeMap e. LinkedList 4. What is an Iterator? What is the difference between an Iterator and an Enumerator? 5. What is the difference between Collections and Maps? 6. Make a comparison between SoftReference and WeakReferences? 7. How can you obtain a sychronized (thread-safe) version of a LinkedList 8. What specific List would you chose as a container if you would have to perform random removals and wish for the best performance? 3.7.2 Problems 1. Write a program that does the following: a. creates two ArrayLists each with at least three elements (call them a and b) b. merges b into a in an interleaved fashion (so if a=[a,b,c] and b w,x,y,z], then a becomes [a, w, b, x, c, y, z]) c. removes every second element from b (so b would become [w,y]) d. finally removes from a every element that is in b (so a would become [a, b, x, c, z]) 2. Create a class Country containing two attributes: Name and Capital, both Strings. Make it Comparable so that the comparison only cares about the country's name. Fill an ArrayList with objects of your class. Demonstrate that sorting works properly. Now make a Comparator that only cares about the capital and demonstrate that sorting works properly. Perform a binary search for the country whose capital is Paris using your second Comparator. 3. Write a program to implement a memory sensitive cache. 4. Write a program illustrating a "publish-subscribe" mechanism using a simple interface and and a Collection.
Document Info
Accesari:
1489
Apreciat:
Comenteaza documentul:
Nu esti inregistrat Trebuie sa fii utilizator inregistrat pentru a putea comenta