15-100 Fall 2007 Practice Exam #3

NOTE: The sample questions below do not necessarily represent the exact questions you will receive on the exam, but they cover similar topics that you are expected to understand.

1. Let int[][] data = new data[7][5]; . Complete the code below so that the array data contains the following matrix of values:

    1   2   5  10  17
    2   3   6  11  18
    4   5   8  13  20
    8   9  12  17  24
   16  17  20  25  32
   32  33  36  41  48
   64  65  68  73  80

for (int row = 0; row <= __________; row++)
{
    // initialize first value in the current row

    if (row == 0)
         data[0][0] = 1;
    else
         data[row][0] = ________________________________________;

    // initialize all other values in the current row

    for (int col = 1; col <= __________; col++)
    {
         data[row][col] = ________________________________________;
    }
}

2. Assume a TimeOfDay class has instance variables hour, minute, and seconds (all ints). Also TimeOfDay implements the Comparable interface. Show how to define the following required method for the TimeOfDay class:

public int compareTo(Object obj)
{
    TimeOfDay t = ____________________;

    if (this.hour != t.hour)
 
        return _________________________;
 
    else if (this.minute != t.minute)
 
        return _________________________;
 
    else
 
        return _________________________;
}
3. Consider the definition of an Account class below.
/**
   A bank account has a balance that can be changed by 
   deposits and withdrawals.
*/

public class Account {  
    private double balance;
    private int interestRate;

    public Account() {
        balance = 0.0;
        interestRate = 50;        // default 50% interest rate!
    }

    public Account(double initBalance, int rate) {
        balance = initBalance;
        interestRate = rate;
    }

    public void deposit(double amount) {
        balance = balance + amount;
    }

    public void withdraw(double amount) {
        balance = balance - amount;
    }

    public double getBalance() {
        return balance;
    }

    public void addInterest() {
        double interest = balance * interestRate/100.0;
        balance += interest;
    }
}
Using the definition of the Account class above, determine the output of each of the following Java program fragments. (You may assume that all values are output with 2 decimal places.)
(a)
Account homer = new Account(1000.00, 5);        // interest rate = 5%
Account marge = new Account(500.00, 10);        // interest rate = 10%
homer.deposit(marge.getBalance());
System.out.println("Homer's balance is $" + homer.getBalance());
marge.deposit(homer.getBalance());
System.out.println("Marge's balance is $" + marge.getBalance());

(b)
Account bart = new Account(250.00, 10);        // interest rate = 10%
Account lisa = bart;
bart.addInterest();
System.out.println("Bart's balance is $" + bart.getBalance());
System.out.println("Lisa's balance is $" + lisa.getBalance());

(c) How does the compiler know which constructor to use when we create a new Account object? Explain.

(d) TRUE OR FALSE: deposit, withdraw, addInterest and getBalance define the state of the bank account. Explain.

(e) Redefining a method with same and signature in a subclass is called _________________.

4. An AccountCollection class that maintains a set of bank accounts is defined as follows:

 
public class AccountCollection {

    private Account[] accountArray;
    private int numAccounts;

    public AccountCollection() {
        accountArray = new Account[1];
        numAccounts = 0;
    }
 
    //  other methods go here


}

Using the definition of AccountCollection class, complete the method below for this class that returns a reference to the account with the minumum balance. If there is more than one account with the minimum balance, return the first reference in the array to an account with minimum balance. If there is no such account, return a null reference. You may not make any assumptions about the magnitudes of account balances.

 
public Account getMinAccount(){




}

5. A car has the following properties. A car has a fuel efficiency (measured in miles/gallon) and a fuel level in its gas tank (measured in gallons). When a car is created, it will have a fuel level of 0 and a fuel efficiency supplied by the creator of the object. You can drive a car for a certain number of miles and you can add fuel to the car. Finally, you can get the current amount of fuel in the car.

Implement a Java class for a Car as described above. Use the description above to determine what instance variables and methods your class should have. You do not need to define any more than what is described above for your Car class.

Your class should work with the following Java class:

public class CarTest {

    public static void main(String[] args) {
        final double TANK_SIZE = 15.0;      // Tank holds 15 gallons max.
        Car alero = new Car(30);            // efficiency: 30 miles per gallon
        alero.addFuel(TANK_SIZE);           // Fill up the tank
        alero.drive(240);                   // drive 240 miles
        System.out.println("My Alero has " + 
            alero.getFuel() + " gallons of fuel left.");
        alero.addFuel(5);
        alero.drive(300);
        System.out.println("Alero: " + alero);
    }
}
The output of this method is:
My Alero has 7.0 gallons of fuel left.
Alero: 30 mpg, 2.0 gallons