1.

R1. Choosing Classes

Consider the following problem:

A company allows its employees to check out certain items, such as handheld computers and music players to gain personal experience with them. Popular
items can be reserved on a first come/first served basis. A reservation list is kept for each item. There is a fine for overdue items.

Your development team's task is to write a software program that allows the stockroom clerk to check out items and check them back in, to reserve items, to notify employees when a reserved item has been returned, to produce reports of overdue items, and to track payment of fines.

What classes would you choose to implement this program?


Answer:


2.

R2. Cohesion and Coupling

Consider the java.awt.Toolkit class in the standard Java library. Is its public interface cohesive? Explain why or why not.


Answer:


3.

Which of the following classes depend on each other?

   String
   StringTokenizer
   Print
   Stream
   Random

Hint: Look at the API documentation.


Answer:


4.

R3. Accessor and Mutator Methods

Look up the methods of the StringTokenizer class in the API documentation. Indicate which methods are accessors, and which are mutators.

Hint: Remember that accessor methods do not change the state of the object. Therefore, if you call an accessor method multiple times in a row with the same parameters, you always get the same answer.


Answer:


5.

A class is immutable if it has no mutator methods.

List at least four immutable classes.


Answer:


6.

R4. Side Effects

The following class has a method with a side effect:
/**
  A purse computes the total value of a collection of coins.
*/
public class Purse
{
  /**
     Constructs an empty purse.
  */

  public Purse()
  {
     total = 0;
  }

  /**
     Add a coin to the purse.
     @param aCoin the coin to add
  */

  public void add(Coin aCoin)
  {
     total = total + aCoin.getValue();
     System.out.println("The total is now " + total);
  }

  /**
     Get the total value of the coins in the purse.
     @return the sum of all coin values
  */

  public double getTotal()
  {
     return total;
  }

  private double total;
}

Describe the side effect, and explain why it is not desirable.


Answer:


7.

How would you eliminate the side effect?


Answer:


8.

R5. Preconditions and Postconditions

What are the preconditions of the substring method of the String class?


Answer:


9.

What happens if you violate one of those preconditions?


Answer:


10.

Coins should not have a negative value or a value of zero. Document an appropriate precondition of the Coin constructor to ensure these requirements. Implement the constructor and enforce the precondition by throwing an IllegalArgumentException if it is not fulfilled.


Answer:


11.

Given the precondition of the preceding problem, what postconditions can you attach to which methods of the Purse class? Give the javadoc comments and method signatures of all methods that have a nontrivial postcondition.


Answer:


12.

R6. Static Methods

A static method has no implicit parameters. Sometimes, we use static methods because all the method parameters are numbers. Numbers are not objects, so they cannot be implicit parameters of a method.

Write two static methods that compute

   the circumference of a circle with a given radius r
   the area of a circle with a given radius r

Place the two static methods into a class Geometry


Answer:


13.

An overuse of static methods is often a sign of poor object-oriented design. Explain how you can compute the circumference and area of circles in a more object-oriented fashion.


Answer:


14.

P1. Static Fields

Consider the Needle class from Chapter 6 of the textbook.

Each needle object has its own random number generator object, which is wasteful. A single random number generator can be shared among all needle objects.

Reimplement the Needle class so that all objects share a static generator.


Answer:


15.

P2. Packages

Place the Coin and Purse programs of Chapter 7 into a package named com.horstmann.bigjava.ch7.
Into which directory should you place the files Coin.java and Purse.java?

(You can find the files in the ch7/purse subdirectory of your textbook's code.)


Answer:


16.

What modifications do you need to make to the files Coin.java and Purse.java?


Answer:


17.

Place the PurseTest program of Chapter 7 into the default package.

Into which directory should you place the file PurseTest.java?


Answer:


18.

What modifications do you need to make to the file PurseTest.java?


Answer: