Question
Continue working on the Account class. Add one more instance variable called acctNum, which is used to save accout number. Overload the contructor with three
- Continue working on the Account class. Add one more instance variable called acctNum, which is used to save accout number.
- Overload the contructor with three parameters, one for each instance variable.
- Rewrite the deposit method with += operator.
- Add the method toString, which should return a string containing the name, account number, and balance of the account. The method header is provided below.
public String toString()
- Add method chargeFee, which should deduct a service fee $10.00 from the account instance variable balance.
- Add method changeName which takes a string as a parameter (newName) and changes the name on the account to be that string.
name = newName;
- Finally, use the provided file ManageAccounts.java as a skeleton of the testing program. Complete the code in this file to test your Account class.
Account.java:
// ******************************************************* // Account.java // // A bank account class with methods to deposit to, withdraw from, // change the name on, charge a fee to, and print a summary of the account. // ******************************************************* public class Account { // Your code here: declare an instance variable called balance // Your code here: declare an instance variable called name; // Your code here: declare an instance variable called acctNum; // --------------------------------------------- //Constructor -- initializes balance, owner, and account number // -------------------------------------------- public Account(double initBal, String owner, int number) { // Your code here: use the parameters to initialize instance variables } // -------------------------------------------- // Checks to see if balance is sufficient for withdrawal. // If so, decrements balance by amount; if not, prints message. // -------------------------------------------- public void withdraw(double amount) { if (balance >= amount) balance -= amount; else System.out.println("Insufficient funds"); } // -------------------------------------------- // Adds deposit amount to balance. // -------------------------------------------- public void deposit(double amount) { balance += amount; } // -------------------------------------------- // Returns balance. // -------------------------------------------- public double getBalance() { return balance; } // -------------------------------------------- // Returns a string containing the name, account number, and balance. // -------------------------------------------- public String toString() { // YOUR CODE HERE; } // -------------------------------------------- // Deducts $10 service fee // // -------------------------------------------- public void chargeFee() { // YOUR CODE HERE; } // -------------------------------------------- // Changes the name on the account // -------------------------------------------- public void changeName(String newName) { // YOUR CODE HERE; } }
ManageAccounts.java:
// ************************************************************ // ManageAccounts.java // // Use Account class to create and manage Sally and Joe's // bank accounts // ************************************************************ public class ManageAccounts { public static void main(String[] args) { Account acct1, acct2; //create account1 for Sally with $1000 acct1 = new Account(1000, "Sally", 1111); //create account2 for Joe with $500 acct2 = new Account(500,"Joe",1112); //deposit $100 to Joe's account by calling certain method acct2.deposit(100); //print Joe's new balance (use getBalance()) System.out.println(acct2.getBalance()); //your code here: withdraw $50 from Sally's account //your code here: print Sally's new balance (use getBalance()) //your code here: charge fees to both accounts //your code here: change the name on Joe's account to Joseph //your code here: print both acct1 and acct2 } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started