Practice Questions: Writing Classes

For this question, you will implement methods for a Java class called BankAccount. This bank account is peculiar because it can only accepts $5 bills.
public Class BankAccount
{
  private int numberOfFives;//number of $5 bills in the account

  public int getNumberOfFives()
  {
    return numberOfFives;
  }

  public void setNumberOfFives(int x)
  {
    numberOfFives = x;
  }
}
Add to the BankAccount class a method called closeAccount within the BankAccount class. This method should withdraw all the existing $5 bills from the account and return the amount of money, in dollars, withdrawn. For example, if the account has a balance of 10 $5 bills then this method will return $50.

Add to the BankAccount class a method called transferBalance. Given another BankAccount object and a specified number of $5 bills, this method transfers that number of $5 bills from the account given in the parameter list to this bank account. If there are insufficient bills then it transfers as much money as possible (emptying one of the accounts).

Add to the BankAccount class a constructor that takes one parameter - the number of $5 dollars bills to start the account with.

Assume that the BankAccount class has been fully implemented. Assume that all the methods you wrote are correct. What is printed by the following code segment?

BankAccount bank1 = new BankAccount(2);
BankAccount bank2 = new BankAccount(6);

System.out.println(bank2.getnumberOfFives());
System.out.println(bank1.getnumberOfFives());

bank1.transferBalance(bank2, 7);
   
System.out.println(bank2.getnumberOfFives());
System.out.println(bank1.getnumberOfFives());

System.out.println(bank1.closeAccount());

bank1 = bank2;

bank2.setNumberOfFives(6);

System.out.println(bank2.getNumberOfFives());
System.out.println(bank1.getNumberOfFives());

Consider the Domino class below.
public class Domino
{
  private int num1;
  private int num2;

  public int getNum1() { ... }
  public int getNum2() { ... }
}
Implement the method canConnectTo, which should take in another Domino and return true only if this domino can connect to the given one. Two dominoes may connect together if they have at least one number in common.
Consider the Clock class below:
public class Clock
{
  private int hour;  //0 to 23
  private int min;   //0 to 59

  public int getHour() { ... }
  public int getMinute() { ... }

  public void setHour(int h) { ... }
  public void setMinute(int m) { ... }
}
In the Clock class, implement the method startDaylightSavings, which should set the clock ahead one hour, keeping the hour within the range of 0 to 23.

In the Clock class, implement the method isWithin, which should take in another Clock and a double value h, and return true only if the given clock's time is within h hours of this clock (ahead or behind). For example, 23:15 is within 2.5 hours of 1:00.

In the Clock class, implement the method swapTimes, which should take in another Clock and swap this clock's time with the given clock's time.


Consider the Bus class below.
public class Bus
{
  private int numPassengers;  //the number of people on the bus
  private int capacity;  //the number of seats on the bus

  public int getCapacity() { ... }

  //numPassengers must be greater than 0
  public void removeOnePassenger() { ... }
}
The following must always hold true for a Bus:

0 ≤ numPassengerscapacity

Implement the following methods in Bus:

//makes the bus full and returns the number of people added to the bus
public int fillBus()

//returns the fraction of the bus that is full, in the range 0.0 to 1.0.
public double fractionFull()

//Moves all passengers from another bus onto this one, if they fit.
//Returns true if successful.
//If all the passengers do not fit, then no one is moved,
//and the method returns false.
//(You cannot directly access how many passengers are on the other bus,
//but you may find it helpful to call fractionFull.)
public boolean addPassengersFrom(Bus anotherBus)

Consider the ArrayExaminer class below.
public class ArrayExaminer
{
  private String[] array;
  private int numReturned;

  public ArrayExaminer(String[] someArray)
  {
    array = someArray;
    numReturned = 0;
  }
}
Implement the method getValue which takes in no parameters and should return a string value from the array. The first time getValue is called, it should return the first string in the array. The second time getValue is called, it should return the second string in the array. And so on.

Implement the method getNumLeft, that returns the number of values that have not yet been returned by getValue.

Implement the method getAllValuesAlreadyReturned, which should return an array containing only those values that have already been returned by the getValue method.

Implement the method printAll below, which should take in an ArrayExaminer and print all values contained in its array. You should assume that the given ArrayExaminer has never been used before (its numReturned field is still 0).

public static void printAll(ArrayExaminer e)