1. Modify the Account.java Program listed below with the following features a. A
ID: 3659797 • Letter: 1
Question
1. Modify the Account.java Program listed below with the following features
a. Add two additional attributes: name and the accountNumber
b. Add a constructor that takes the name, account number and the initial balance .
c. Modify the default constructor so that it sets name to an empty string and the account number to 0000 in addition to setting the balance to 0.
d. Add the method toString, which should return a string containing the name, account number, and balance for the account.
e. Add the method chargeFee, which should deduct a service fee from the account and returns updated balance.
f. Add the method changeName which takes a string as a parameter and changes the name on the account to be that string.
Explanation / Answer
// ******************************************************* // 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 { private double balance; private String name; private long acctNum; // --------------------------------------------- //Constructor -- initializes balance, owner, and account number // -------------------------------------------- public Account(double initBal, String owner, long number) { balance = initBal; name = owner; acctNum = number; } // -------------------------------------------- // 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() { } // -------------------------------------------- // Deducts $10 service fee // // -------------------------------------------- public void chargeFee() { } // -------------------------------------------- // Changes the name on the account // -------------------------------------------- public void changeName(String newName) { } } // ************************************************************ // 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 //deposit $100 to Joe's account //print Joe's new balance (use getBalance()) //withdraw $50 from Sally's account //print Sally's new balance (use getBalance()) //charge fees to both accounts //change the name on Joe's account to Joseph //print summary for both accounts } } PLEASE RATE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.