a. Describe how you can use two different BankAccount objects (acc1 and acc2) on the BlueJ workbench to transfer funds from one account to the other.
I will describe this using my code which does this sort of transfering:
public void transferMoney(double amount, BankAccount from, BankAccount to)
{
from.withdraw(amount);
to.deposit(amount);
}
Essentially what this does, is it takes withdraws a certain amount from one BankAccount object and it deposts a certain amount in another, to act as though they transfered money.
b. Use the relationship between BankAccount class and GoldBankAccount class to describe how inheritance works in Java. As you write your description you should use the following terms in an appropriate way.
The relationship between BankAccount and GoldBankAccount deals with inheritance, in which BankAccount is the superclass of GoldBankAccount, and GoldBankAccount is the subclass of BankAccount. In an inheritance hierarchy, the BankAccount class would therefore appear above the GoldBankAccount class. In the superclass constructor, the fields important to it, and GoldBankAccount are found. Therefore, there is no need to reuse those fields, or recreate them in the subtypes of the superclass, that would simply defeat the purpose of inheritance. Another feature of this inheritance relationship is that we can create a BankAccount Object and assign a subtype with subtype variables to it.
c. A Bank class would store all the accounts held by a bank. The data structure that holds these accounts is a single ArrayList. How is it that a single ArrayList can store objects of different types (BankAccount, and GoldBankAccount)?
It can store objects of type BankAccount which include all subtypes, such as GoldBankAccount. Therefore it can store them both.
d. Implement the Bank class to include a method that computes the total of all the balances held by the bank.
Done:
import java.util.ArrayList;
/**
* Write a description of class Bank here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bank
{
// instance variables - replace the example below with your own
private ArrayList banks;
/**
* Constructor for objects of class Bank
*/
public Bank()
{
banks = new ArrayList <BankAccount> ();
}
public void addBankAccount(BankAccount object)
{
banks.add(object);
}
public int amountOfBanks()
{
return banks.size();
}
}
e. Why are are wrapper classes necessary in Java?
Because otherwise we couldn't be able to use primitive types in ArrayLists, as ArrayLists can only hold objects, but int and boolean, for example, aren't necessarily objects. Therefore there are wrapper classes for those classes, so they can be used in ArrayLists.
For those of you who work quickly..
(I will finish this later)
Create two new classes: JuniorGoldAccount, which is a more restrictive version of the GoldAccount class, and PlatinumAccount, which offers even more advantages than the GoldAccount.
Test your classes and include the code in this document
No comments:
Post a Comment