Copyright © Cay S. Horstmann 2009 
This work is licensed under a Creative Commons
Attribution-Noncommercial-Share Alike 3.0 United States License.
See lab 1 for reporting instructions.
The steps tagged with this icon are optional. Do them if you have time and want to practice more.
A. Enhancing the BankAccount class
account in BlueJ and a class
BankAccount. Paste in the code from BankAccount.java. This is the class from
Chapter 3 of your text book.BankAccount() constructor. Right-click on the
object and invoke the deposit method, depositing $1000. What happens?Your task is to enhance the BankAccount class so that the
fee can be deducted by calling the void endOfMonth() method.
Add the method (with a blank body) and a suitable comment to the
BankAccount class. What did you
add?
deposit and withdraw
methods. What changes did you make?endOfMonth method. To test it, add a class
BankAccountTester with the following contents:
/**
A class to test the BankAccount class.
*/
public class BankAccountTester
{
/**
Tests the methods of the BankAccount class.
@param args not used
*/
public static void main(String[] args)
{
BankAccount harrysChecking = new BankAccount();
harrysChecking.deposit(2000);
harrysChecking.withdraw(500);
System.out.println(harrysChecking.getBalance());
System.out.println("Expected: 1500");
harrysChecking.endOfMonth();
System.out.println(harrysChecking.getBalance());
System.out.println("Expected: 1498");
harrysChecking.deposit(1000);
harrysChecking.withdraw(500);
harrysChecking.endOfMonth();
System.out.println(harrysChecking.getBalance());
System.out.println("Expected: 1996");
}
}
To run the tester class, right-click and select main.
What does your program print? If you didn't get the right output the first time, explain what error you made.
Adding the interest does not count as a transaction with a fee. The interest is added before fees are deducted.
Enhance the bank account class to simulate this behavior. First, add a
parameter double interestRate to the endOfMonth
method. Add a @param comment too. Add an instance variable
minimumBalance to the class. Initialize it to the
initialBalance in the BankAccount(double initialBalance)
constructor. Update the minimumBalance variable in the
withdraw method, like this:
minimumBalance = Math.min(minimumBalance, balance);
In the endOfMonth method, compute the interest as
double interest = minimumBalance * interestRate / 100 / 12;
Here, we divide by 100 to compute the percentage, and by 12 to convert
to a monthly rate. For example, if the annual interest rate is 6%, the
monthly rate is 0.5% or 0.005. What is your
endOfMonth method now?
endOfMonth method is correct. What is
the code of your tester class now? endOfMonth method to give 5 free transactions. Use
Math.max(..., 0) to compute the fee. What is the code for your endOfMonth
method?B. A Car class
Car class that simulates a car.
When constructing a Car, you supply the fuel efficiency (miles
per gallon). The class has methods for adding gas, checking the gas level,
checking the odometer (i.e., total miles driven), and driving for a number
of miles. Here is a typical tester program:
/**
This program tests the Car class.
*/
public class CarTester
{
public static void main(String [] args)
{
Car myHybrid = new Car(50); // 50 miles per gallon
myHybrid.addGas(20);
myHybrid.drive(50); // consumes 1 gallon
myHybrid.drive(50); // and another gallon
myHybrid.addGas(10);
double gasLeft = myHybrid.getGasInTank();
System.out.print("Gas left: ");
System.out.println(gasLeft);
System.out.println("Expected: 28");
System.out.print("Miles driven: ");
System.out.println(myHybrid.getMilesDriven());
System.out.println("Expected: 100");
}
}
In BlueJ, make a project car and classes Car
and CarTester. Start out with this Car class:
/**
A car can drive and consume fuel.
*/
public class Car
{
/**
Constructs a car with a given fuel efficiency.
@param anEfficiency the fuel efficiency of the car
*/
public Car(double anEfficiency)
{
}
/** Adds gas to the tank.
@param amount the amount of fuel to add
*/
public void addGas(double amount)
{
}
/**
Drives a certain amount, consuming gas.
@param distance the distance driven
*/
public void drive(double distance)
{
}
/**
Gets the amount of gas left in the tank.
@return the amount of gas
*/
public double getGasInTank()
{
return 0;
}
/**
Gets the total miles driven by this car.
@return the miles driven
*/
public double getMilesDriven()
{
return 0;
}
}
Now run the tester program. What output do you get? Why don't the actual values match the expected values?
milesDriven and update it in the drive method.
Also fix up the getMilesDriven method. What changes did you make?Car class and make a new
Car(50) object. Right-click on the object and call
drive(50) twice. Call getMilesDriven. What answer did you get?addGas and gasInTank
methods. Test your changes by making a Car object in BlueJ and
invoking methods. What methods did you invoke? What
result did you get?drive method, you will want to compute
double gasConsumed = distance / efficiency;
What problem do you run into when you try this?
drive method and test it by making a
Car object in BlueJ and invoking methods. What methods did you invoke? What result did you
get?CarTester. What output did
you get?C. A Car with Alice and Netbeans
public class MyCar extends Car {
unchanged. Remove the empty MyCar constructor. Copy all
instance variables and methods from the Car class of the
previous part inside, but rename the constructor MyCar (to
match the name of the class). What happens when you
try to run the program?private MyCar car1 = new MyCar();
to
private MyCar car1 = new MyCar(50);
in MyScene.java. Now your program should compile and run.

drive method of MyCar, add a call
move(MoveDirection.FORWARD, distance / 100);
This will make the car move. We divide by 100 so that the car doesn't
leave the screen. In the run method of MyScene,
add
car1.addGas(20); car1.drive(50); car1.drive(50);
Run the program. What happens?
car1.say("Gas: " + car1.getGasInTank());
Run the program again. What happens?
CatClock whose tail makes a splendid gauge.

Add this import statement to MyScene:
import org.alice.apis.moveandturn.gallery.objects.CatClock;
Add this code to the run method in MyScene:
CatClock clock = new CatClock(); clock.resize(2); addComponent(clock); clock.turnToFace(camera); Model gauge = clock.getPart(CatClock.Part.ClockTail);
As always, don't worry about the Alice magic. You will never be asked to
memorize or create it. Now we want to move the gauge whenever the
addGas or drive method is called. The easiest way
is to pass the gauge as another parameter:
car1.addGas(20, gauge);
What happens when you try to compile?
addGas method in MyCar needs to be modified to
take that parameter. Here it is:
public void addGas(double amount, Model gauge)
{
gas = gas + amount;
gauge.roll(RollDirection.RIGHT, (0.25 / 20) * amount );
}
Here, I figure the tank holds 20 gallons, and a full tank should roll the gauge by a quarter turn.
Now compile and run. What happens?
Almost there... We want to move the gauge to the left
during driving. Add the gauge parameter to the
drive calls. Add a Model gauge parameter to the
drive method. Add this line to the method:
gauge.roll(RollDirection.LEFT, (0.25 / 20) * gasConsumed);
Compile and run. You'll see the gauge, erm, cat tail, moving to indicate the gas consumption.
Ok, you peeked. Here is the outline.
DoTogether.invokeAndWait(
new Runnable()
{
public void run()
{
move(MoveDirection.FORWARD, distance / 100);
}
},
new Runnable()
{
public void run()
{
gauge.roll(RollDirection.LEFT, (0.25 / 20) * gasConsumed);
}
});
You also need to declare the variables used inside the run
methods as “final”:
public void drive(final double distance, final Model gauge)
{
final double gasConsumed = distance / efficiency;
...
}
We won't explain this—you'll learn more about it in CS151.
Try it out. Now the car and the gauge move together.