In this lab, you will work with object-oriented techniques in Java.
Use File - Close All to close all existing source windows, then create a new Java Project named Lab05.
Create a new class named SavingsAccount in the bank package. The class SHOULD NOT have a main method -- you will write a separate testing class in a later step.
This class represents a savings account for a bank's IT system.
Define the following fields as private members of the class:
Type Name Description
----- ---- -----------
int accountNumber Unique ID for this account
java.util.Date createDate The date the account was created
double balance Current account balance
Now let's add some behaviors to the class. Write the following methods within the class's block:
public void withdraw(double amount)
{
}
public void deposit(double amount)
{
}
public String toString()
{
return "Account number: " + accountNumber +
" Creation date: " + createDate +
" Account balance: " + balance;
}
Add code to the withdraw and deposit methods so that they correctly update the balance using the amount parameter.
Create a new class named TestSavingsAccount in a new package named test. This new class SHOULD have a main method.
In main, create a SavingsAccount object named acct. Call deposit and withdraw on the object, passing values of your choice. Then display the object to the console:
System.out.println(acct);
Note: You will need to import the SavingsAccount class since it resides in a different package.
Run your program -- are you satisfied with the balance? Don't worry if you don't see reasonable values for the creation date and account number. You will fix those later.
Add the following line to main:
acct.accountNumber = 12;
Save to compile. Why doesn't this work? You will get around this issue in the next part, but for now, comment-out the offending line.
In the last part, there was no way to change the account number and creation date from outside of the class since the fields were private.
In this part, you will provide controlled access so the outside world can examine or modify a SavingsAccount object's state.
The standard Java technique for exposing state is to write a pair of methods variously referred to as get/set methods or accessor/mutator methods. Writing such methods is trivial but tedious -- the good news is that Eclipse will write them for you! Follow this procedure:
Open the source editor for SavingsAccount.java
Right-click on the source window and choose Source - Generate Getter and Setter.
In the wizard, select the accountNumber and createDate entries so that getters and setters are generated for both, then press OK.
Note: We do not provide get/set methods for balance since that field is accessed via the deposit and withdraw methods.
Examine the generated methods and make sure that you understand them. Feel free to move them to a different location in the class if you don't like where the wizard inserted them.
Edit TestSavingsAccount, and in main, immediately after creating the SavingsAccount object, call the set methods to initialize it to values of your choice. For the date, you can use:
new java.util.Date()
Then try running your program as before. Do you see correct output?
In this part, you will learn how any Java class can override behavior from the superclass of all Java classes: Object.
In the first part of this lab, you created a class named SavingsAccount, and you did not specify its superclass. In Java, whenever you don't explicitly specify a superclass, Java uses the predefined java.lang.Object class as the superclass. Thus, the Object class provides the basic behavior for all Java objects.
Use the JavaDoc to scan the documentation of the Object class. There's much in there that will be difficult to understand until you learn more Java, but you can get the general idea of what Object is all about.
In particular, note that it defines a method named toString -- we will work with that method later in this part of lab.
Edit the SavingsAccount class, and comment out the existing toString method.
Try running the TestSavingsAccount class -- what output did this produce?
When you pass an object reference to System.out.println, it calls the toString method to obtain a String to send to the console. At this point, there is no toString method in SavingsAccount, so Java uses the implementation inherited from Object, which returns the object's class name and its hashcode. While this is interesting, the output doesn't look very pretty or informative.
The trick is to override the toString method in the SavingsAccount class so that Java uses your implementation rather than the implementation from Object.
Remove the comments from the toString method and run TestSavingsAccount again. Do the results make sense?
In this part, you will practice the techniques covered in this lab to reinforce the concepts.
Create a new class named AccountList in the bank package. This class should not have a main method.
This class will keep track of SavingsAccount objects. The proper way to do this would be to use an array or a collection, but we haven't covered these yet, so this class will just maintain a couple of SavingsAccount references as fields.
Define the following fields:
Type Name Description
---- ---- -----------
int count Number of accounts
SavingsAccount acct1 First account
SavingsAccount acct2 Second account
We will fix the count at 2 for this lab:
private int count = 2;
Use Eclipse to generate ONLY A get method for count. Do NOT generate get/set methods for the SavingsAccount fields.
Write two methods, one named addAccount1 and the other named addAccount2 that accept a SavingsAccount reference and assign it to one of the SavingsAccount fields. Both methods should return void.
Write a method named calcAverageBalance that accepts no arguments and returns a double.
Implement the method by returning the average balance score of the two SavingsAccount objects.
Override toString and return a String containing labeled values for the count and the average balance.
Create a new class named TestAccountList in the test package. This class should define a main method.
Add code to the main method so that it:
Run and test your program.