Assignment #7 - Bank Account
Due: Friday, July 18
Objective
This assignment gives students the "half completed" class BankAccount.
Member functions of that class are present, but not defined.
It is the student's job to fill in the member functions of the class, adhering to
strict specifics. Notice that the main() method and private member data are already present.
Do not change any completed code in the starter file, but use the file to fill in your code
in the designated areas (method definitions). The
main() method will test each member function you'll write. Your GOAL is for your output to
match mine below EXACTLY.
Filename: BankAccount.java
Code file you should download and add your code to is HERE.
Exercise:
In the class called BankAccount:
- Notice that the main() routine that uses/tests the BankAccount member functions
is already completed and written. DO NOT CHANGE THE CONTENTS OF MAIN, OR
ANY OF THE METHOD DECLARATIONS. Your job, should you choose to accept it, is to fill in the contents
of the member functions in the BankAccount class so they work properly (as tested by the main method).
REQUIREMENTS:
- Do not change main, the method declarations (parameters, return types, etc), or the instance variables.
- Do not add any other instance variables.
- Fill in the empty method definitions so that they do the following:
Constructor #1 BankAccount(String n) - The first constructor should take in one parameter (string) representing
the account holder's name, and should default the account balance to 0.00. Should initialize both name and balance.
Constructor #2 BankAccount(String n, double b) - This constructor takes in 2 parameters, n
representing name for the account and b representing the initial balance for new account. Should initialize both name
and balance. Ensure that b is not a negative value. If it is, default balance to 0.00
getBalance() - This method is an Accessor that returns the value of balance.
deposit(double d) - Takes in parameter d representing deposit amount. Should update
balance of account to reflect the deposit made. Must error check for negative deposit. If negative,
print out "Cannot deposit a negative amount...", and do not change balance.
withdraw(double w) - Allows user to withdraw money from their account. Should update balance
to reflect withdrawal. MUST error check for negative withdrawal (in which case print "Cannot withdraw
a negative amount"), and also check to see if the withdrawal amount is greater than the account
balance (if so print out "Your account balance is only $ -- you cannot withdraw $")
printBal() - Prints out the account name and current balance (to two decimal places). See sample output below for
exact formatting of the output.
- HINTS:
- Again, DO NOT CHANGE anything in the starter file. Only add your definitions of the member functions.
- Do not try to do more in a method than required.
- Make your output match mine below EXACTLY for the most points on the assignment.
Output should match this exactly when program is run:
Testing default constructor...
Bill Gates Current Balance : $ 0.00
Testing deposit method... depositing $455.14
Bill Gates Current Balance : $ 455.14
Testing deposit method... depositing -$5.00
Cannot deposit a negative amount...
Bill Gates Current Balance : $ 455.14
Retreiving balance...
Withdrawing half of that amount...
Withdrawing... $227.57
Bill Gates Current Balance : $ 227.57
Attempting to create account with negative value, should default to $0.00...
Poor Paul Current Balance : $ 0.00
Creating account with initial valid value $283.33...
Jameis Winston Current Balance : $ 283.33
Attempting to withdraw negative value...
Cannot withdraw a negative amount
Jameis Winston Current Balance : $ 283.33
Attempting to withdraw more than balance...
Your account balance is only $283.33 -- you cannot withdraw $ 3983.93
Jameis Winston Current Balance : $ 283.33
Withdrawing everything in account...
Withdrawing... $283.33
Jameis Winston Current Balance : $ 0.00
Requirements for program
- Each method should do exactly the task specified
- When you write source code, it should be readable and
well-documented.
Submitting:
Program submissions should be done through blackboard, Submit the file:
BankAccount.java