EECS2030Z Test 3

PRACTICE


GETTING STARTED

  1. Download the following jar file containing the practice test classes and add it to your Test 3 project.

Programming Question

To practice implementing a class that uses composition, implement the class described by this API. You do not have to include javadoc comments.

To practice implementing a class that uses aggregation, implement the class described by this API. You will have to do this in another project, or remove the test3P.jar file from your current project.

Solutions can be viewed for ItemizedBill and for Item.


Written Questions

A.

State the definition of a class invariant. What invariant does ItemizedBill have? What invariant does Item have?

A class invariant is a condition regarding the state of a an object that is always true. The API for ItemizedBill states that "the total price of the bill is guaranteed to be equal to the sum of the prices of the items in the bill." The API for Item states that "the price of an item is guaranteed to be greater than or equal to zero".

B.

What does the hashCode method do? What classes call hashCode?

hashCode returns an int value called a hash code that is computed based on the state of the object (i.e., using the fields of the object). The hased containers (such as HashSet and HashMap call hashCode to determine where they should store elements in their internal data structure.

C.

What does the term information hiding mean? What benefit does information hiding have for clients of a Java class?

Information hiding is the principle of hiding implementation details behind a stable interface. A class implemented using information hiding benefits clients because the implementation of the class can change without affecting client code that has already been written.

D.

Is the compareTo method of Item consistent with the equals method of Item? If it is not consistent, what might you have to change in the implementation to make it consistent with equals?

No. compareTo in Item compares items using their price, but equals compares items using their description and their price. To make compareTo consistent with equals, we would have to change compareTo or equals to compare items using the same fields (either use the item price only, the item description only, or both the item price and item description).

E.

Attempt to draw the UML class diagram for Item. You may have to guess what fields Item has. Make sure to show all of the public constructors and methods.

Item
- description : String
- price : int
+ Item(Item)
+ Item(String, int)
+ compareTo(Item) : int
+ equals(Object) : boolean
+ getDescription() : String
+ getPrice() : int
+ hashCode() : int
+ setDesc(String)
+ setPrice(int)
+ toString() : String

F.

What are the differences between a singleton and a multiton?

A singleton represents one instance having some state. A multiton represents multiple instances with each instance having a unique state.

G.

Is Item most likely a composition or an aggregation of a String (that represents the item's description)? How could you try to determine if it is a composition or aggregation?

Item is most likely an aggregation of a string (because String is immutable and it is usually not necessary to use composition with immutable objects).

If Item is an aggregation then it is probably the case that the following code prints true:

String d = "item description";
Item item = new Item(d, 1);
System.out.println(item.getDescription() == d);
H.

What is a privacy leak? Why is it important to prevent privacy leaks when implementing a class that has class invariants?

A privacy leak occurs when a client can obtain a reference to a field of an object (when they are not supposed to). If the field is mutable, then the client can directly modify the state of the field which makes it impossible for the object to maintain any class invariant defined using that field.