Chapter 3 Review Exercise Solutions
R3.1
An interface is the public view of a class. It tells a program using the class what the functionality is, and how to use it. It does not disclose any details on how things are actually done. An implementation of a class is the actual code for accomplishing the tasks. 
R3.2
Encapsulation is the process of hiding implementation details while publishing an interface for programmers to use. If you hide the implementation details, you can diagnose errors and make changes and improvements to the implementation of a class without disrupting the work of programmers who use the class.
R3.3
The private reserved word prevents a programmer using the class from manipulating instance variables except through the methods of that class. A programmer must use the public interface—the public methods that access or modify the instance variables—to change them.
R3.4
private String value;
or a more descriptive name
private String grade;
R3.5
Represent the time as one entire string value, example “8:35 a.m.”
private String value;  
or as individual components
private int hour;
private int minute;
private String meridian;
R3.6
The programmers using the Time class do not have to do anything. That is the purpose of encapsulation and using a public interface.
R3.7
That depends on what you intend for the Counter class to do. Normally, no, you would not provide a setValue method because the counter, as described, should always start at zero. It may be desirable to add a reset method. If there was a reason to start a counter at a particular default value, then you could add the setValue mutator method.
R3.8
(a) If BankAccount(double initialBalance) did not exist, we could use the default constructor to create a BankAccount and then use the deposit method to increase the balance.
BankAccount account = new BankAccount();
account.deposit(1500);

is equivalent to 

BankAccount account = new BankAccount(1500);

(b) Providing a constructor with no parameters allows you to create a new object and set the instance variables to default values of your choosing in the body of the constructor. If you don’t supply a constructor with no parameters, Java initializes instance variables to their default values, but the Java defaults (0 for numbers, null for objects) may not be what you want for your object. (Remember that null is hardly ever a good default value for an object. See Common Error 3.1.) If you don’t supply a constructor with no parameters, then you are assuming that the default values are Ok or forcing the programmers using this class to supply initial values for every instance variable in the class. 

R3.9
A reset method effectively empties the account without any accountability. It would be best to create a new object if you want to delete an account and start with a new one. Otherwise, use the withdraw method to reduce the balance down to zero, which is how a real bank account should work.
R3.10
The account will have a negative balance. There are no checks to detect or stop this from happening. 
R3.11
The this reference is used to refer to the implicit parameter of a method. It can be used to clarify the code, and to call another constructor of the same class.
R3.12
The method transfers an amount to the account that is given as an explicit parameter.
Here is a sample call:
BankAccount harrysChecking = new BankAccount();
BankAccount momsSavings = new BankAccount(10000);
momsSavings.mystery(harrysChecking, 1000);
R3.13
public class TimeDepositAccount
{
   // Constructor
   public TimeDepositAccount(
         double initialBalance, double interestRate) {}
   // Methods
   public double withdrawAll() {}
   public double getBalance() {}
   public void addInterest() {}
}
R3.14
Instance variables are initialized when the object is created. In this case, area would be initialized to zero and wouldn't get calculated with the correct value until getArea is called. If another method were added to this class that used area, it would incorrectly use the value zero if getArea had not been called first to calculate the correct value.

public class Square
{
   private int sideLength;

   public Square(int length) 
   { 
      sideLength = length; 
   }

   public int getArea () 
   {
      int area; // local variable

      area = sideLength * sideLength;
      return area;
   }
}
R3.15
If the method grow is called, the value of the instance variable area will no longer be correct. You should make area a local variable in the getArea method and remove the calculation from the constructor.
public class Square
{
   private int sideLength;

   public Square(int length) 
   { 
      sideLength = length; 
   }

   public int getArea () 
   {
      int area; // local variable

      area = sideLength * sideLength;
      return area;
   }

   public void grow () 
   {
      sideLength = 2 * sideLength;
   }
}
R3.16
/**
   A class to test the Counter class.
*/
public class CounterTester
{
   /**
      Tests the methods of the Counter class.
      @param args not used
   */
   public static void main(String[] args)
   {
      Counter tally = new Counter();
      int result = tally.getValue();
      System.out.println(result);
      System.out.println("Expected 0");

      tally.count();
      tally.count();
      tally.count();
      result = tally.getValue();
      System.out.println(result);
      System.out.println("Expected 3");
   }
}  

R3.17
/**
   A class to test the Car class.
*/
public class CarTester
{
   /**
      Tests the methods of the Car class.
      @param args not used
   */
   public static void main(String[] args)
   {
      Car myHybrid = new Car(50);   // 50 miles per gallon
      myHybrid.addGas(20);   // Fill tank with 20 gallons
      myHybrid.drive(100);   // Drive 100 miles, use 2 gallons
      myHybrid.addGas(10);   // Add 10 gallons to tank
      myHybrid.drive(200);   // Drive 200 miles, use 4 gallons
      double gasLeft = myHybrid.getGasInTank();// Gas remaining in tank
      System.out.println(gasLeft);
      System.out.println("Expected 24");
   }
}  

R3.18
You need four classes: House, Car, SceneComponent, and SceneViewer.
R3.19
The methods have the same implicit parameter as the paintComponent method. We could have written the calls as this.getHeight() and this.getWidth(). They are accessor methods so they don't need an explicit parameter; the instance variables of the object are all they need to do their work.
R3.20
Add width and height fields to the Car class, initialize them in the Car constructor, and use them in the draw method to determine the dimensions of the body, tires, and windshield.