CS-140 Lab 7 -- ArrayLists and Interfaces

Time to use some of the stuff we've been learning about.

Getting Started

Create a lab07 package in your Eclipse cs140 project. Import Account.java, Transaction.java, and Tester.java either from your homework 2 submission, or you can download and use the ones I posted as the solution to hw02 on the web. (If you right click on the lab07 package in the Package Explorer pane in Eclipse, one of the options is "Import...". Use the "File System" import wizard, and then the "Browse..." button on the "From Directory" field allows selection of the directory where your hw02 files reside.) Once you import these files, you will need to update the package to lab07. Run the Tester in Eclipse just to make sure everything still works.

First, let's fix a mistake I made in the design of the Transaction class. Since the printHeader and printTrailer methods never reference a specific object, they should probably be class methods rather than instance methods. Change these methods to make them static, and make the changes to the invocation of these methods so that the Tester still works.

Next, in the Account class, change the log field from an Array to an ArrayList and remove the integer used to keep track of how many transactions are in the log. (ArrayLists keep track of this for you.) This will force several other changes throughout your code. Make sure Tester still works after these changes.

Now, rename the Account class to SavingsAccount. The easiest way to do this is to right click on Account.java in the package Explorer pane, and chose Refactor/Rename... on the popup menu. Check the "update references" box to save even more typing. Also, move the line in the Transaction printHeader method that prints Transactions for account:, move it into the SavingsAccount printStatement method, and change it to say Transactions for Savings Account.... Remove the arguments from the Transactions printHeader method, and move the monthName static array from the Transaction class to the SavingsAccount class. Also change the print in the report method and the withdraw method of the SavingsAccount class to say Savings Account:....<\p>

One more fix in the design... change the withdraw method to return a double value... the amount actually withdrawn from your SavingsAccount. That way, if there is not enough balance to cover the withdrawal, you will know how much you actually got out of the account.

Rerun Tester to make sure stuff still works.

Now, copy your SavingsAccount class to a new class CheckingAccount. This can be done by editing the SavingsAccount.java file, and clicking on the File/SaveAs... menu item. You will need to make several changes to the resulting class.

Test all of these changes.

Making an Interface

Add an interface to your package called Account. Add the common methods from both the SavingsAccount and CheckingAccount classes into the Account interface. Change the SavingsAccount and CheckingAccount classes to implement the Account interface. Recompile and retest.

Extra Credit

What happens if you create a checking account, but specify null as the savings account number? Can you change your code to accommodate this scenario?

Grading Criteria

This lab is worth 10 points. You will get the full 10 points if your code compiles without warnings, and produces the correct output. The following are reasons for deductions:

If you do the extra credit, you will get up to 3 extra points added to your grade, as long as your grade does not exceed 10 points.