
CS140: Computer Programming using Java
Summer 2010
Homework 3
1. Write the Java class file for the class Account that represents a bank account. The
class should have the following data variables:
• accountNumber: an integer that holds the account number.
• balance: a double that is used to store the balance of the account.
Your class should provide the following methods:
• setAccountNumber: This method will take the account number as an
integer parameter and stores it in the accountNumber data member. The
method does not return anything.
• setBalance: This method will take the balance as a double parameter and
stores it in the balance data member variable. The method does not return
anything.
• getAccountNumber: This method returns the account number.
• getBalance: Which returns the current account balance.
• debitAccount: This method deducts funds from the account. It takes the
amount to be deducted as a parameter then subtracts it from the balance.
• creditAccount: This method adds funds to the account. It takes the amount
to be credited as a parameter then adds it to the balance.
2. Write a small Java program that tests the Account class. The program should:
• Generate three objects from the Account class called checkingAccount,
savingAccount, and moneyMarketAcount. The three accounts should be
initialized as follows:
Object Name Account number Starting Balance
checkingAccount 10001 $1,000
savingAccount 10002 $2,000
moneyMarketAcount 10003 $200
• Display the account numbers of the three accounts.
• Display the starting balances of the three accounts.
• Deposit $1,000 in the checkingAcount.
• Withdraw $1000 from the savingAccount.
• Deposit $500 to the moneyMarketAcount.
• Display the new balances of the three accounts after performing the above
transactions.