EE3206 or EE5805 Java Programming and Applications
Tutorial 3 – Programming with Objects and Classes
Submission Required (Question 4--Due on 24 Sept 08)
1. Create a project “JTutor03”. Write a Java class SimpleBankAccount with a public
data field balance.
Write your own main method to create an object of
SimpleBankAccount. Test the object by reading from and writing to the balance.
2. The bank requires that a minimum balanace should be $100 upon creation of an
account. For this purpose, create another class DefaultBankAccount in the same
project.
It has a public data field balance and a no-arg constructor to initialize the
balance. Test the object by reading from and writing to the balance.
3. The DefaultBankAccount is not well protected as its balance is a public field.
Rewrite the class by changing its balance field to private and providing getters and
setters as suggested below.
Save the class as ProtectedBankAccount. You must
make sure the balance is enough for transaction. Test the object by using the
getters and setters to access the balance.
Method Headers:
-public double getBalance() // return balance
-public boolean withdraw(double amount) // return true on success
-public boolean deposit(double amount) // return true on success
Sample Test Output:
My initial account balance is 100.0
After depositing $200:
My account balance is 300.0
After withdrawing $200:
My account balance is 100.0
After withdrawing $200 again:
My account balance is 100.0
4. Create another class RealBankAccount by renaming the ProtectedBankAccount.
Write an instance method transfer based on the given header and static version
below. The instance method transfers money from its own account to another
account.
Method Header
-public boolean transfer(RealBankAccount to, double amount)
-Static Method - transfer
-public static boolean transfer(RealBankAccount from, RealBankAccount to, double amount) {
if(amount <= from.getBalance() && -amount <= to.getBalance())
{
from.withdraw(amount);
to.deposit(amount);
return true;
} // end if
else
return false;
}
Write a main method in RealBankAccount to test the class. The main method takes
three arguments from command line: balance of account A, balance of account B,
transferred amount. Demonstrate your result to the tutor during the tutorial class.
Example 1: java RealBankAccount 200 300 100
Account A balance is 200.0
Account B balance is 300.0
After transferring $100.0 from A to B:
Account A balance is 100.0
Account B balance is 400.0
Example 2: java RealBankAccount 300 400 500
Account A balance is 300.0
Account B balance is 400.0
After transferring $500.0 from A to B:
Account A balance is 300.0
Account B balance is 400.0
You have just progressively built up your bank account program. You may find that
each revision is just done by adding some extra features to the existing one, with most
of the class properties being same.
However, you have to redefine the same things in a new class every time. This is inconvenient and may cause errors when you copy and
paste the code. What even worse is that you are now maintaining multiple pieces of
code.
For example, you are asked to change the type and name of the balance in all
these four classes.
This could be a disaster when you have a fundamental change in
the base class of a big project that have hundreds of classes.
In the next lesson, you will see how we handle this problem.
- END -
Keine Kommentare:
Kommentar veröffentlichen