Programming 12: BankAccount (Java Cohort)

Part 1. Create a class called BankAccount without a main method. It should have the following properties:

1. Two private state variables:

  • double balance … This is how much money is currently in the account.
  • String name …The name of the person owning the account.

2. One constructor that accepts two parameters:

  • One should be a double variable that is used to initialize the state variable balance.
  • The other should be a String that is used to initialize the state variable name.

3. Three public methods:

  • deposit:
    • returns nothing.
    • accepts a double that is the amount of money being deposited. It is added to the balance to produce a new balance.
  • withdraw:
    • returns nothing.
    • accepts a double that is the amount of money being taken out of the account. If the amount does not exceed the balance, the amount is subtracted from the balance to produce a new balance.
  • printAccount:
    • returns nothing.
    • accepts no parameters.
    • prints the account holder’s name and its balance (e.g. “The John Doe account has balance of $100.00.”).

Part 2. Create a Test class that has a main method in which you:

  1. Create a BankAccount object called myAccount, with an initial amount of $1000 and your name.
  2. Call the deposit method to deposit $500 and then call the printAccount method.
  3. Call the withdraw method to withdraw $100 and then call the printAccount method.
  4. Call the withdraw method again to withdraw $500 $1500 (overdraw) and then call the printAccount method.

Take a screenshot of the output. Zip the entire project folder which includes BankAccount.java and Test.java. Upload the screenshot and zip file onto Google Drive.

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someone
This entry was posted in Programming 11/12. Bookmark the permalink.