Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. File Account.java contains a partial implementation for a class representing

ID: 3631042 • Letter: 1

Question

1. File Account.java contains a partial implementation for a class representing a bank account. Save it to your directory and study it to see what methods it contains. Then complete the Account class as described below. Note that you won't be able to test your methods until you write ManageAccounts in question #2.
a. Fill in the code for method toString, which should return a string containing the name, account number, and balance for the account. The string it returns should be in the following format (note that “Mary”, “$100.00” or “1234” are just some examples of their values):
Name: Mary
Account Number: 1234
Balance: $100.00

b. Fill in the code for method chargeFee, which should deduct a service fee from the account.
c. Fill in the code for method changeName which takes a string as a parameter and changes the name on the account to be that string.
2. File ManageAccounts.java contains a program that uses the Account class above. Save it to your directory, and complete it as indicated by the comments.
3. Modify ManageAccounts so that it prints the balance after the calls to chargeFees. Instead of using the getBalance method like you did after the deposit and withdrawal, use the balance that is returned from the chargeFees method. You can either store it in a variable and then print the value of the variable, or embed the method call in a println statement.

//*******************************************************
// 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) {

}
}

// CSE 110 Lab: ManageAccounts class
// [Your Name]
// Lab Section:

// ****************************************************************
// 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, account number 2222

// 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 (use toString())

}
}

Explanation / Answer

Note: Requirement 3 says that you have to print balances without using getBalance() and that the chargeFee() method returns the balance so I use public double chargeFee() instead of public void chargeFee() and then I add a few print lines in ManageAccounts to fulfill that requirement.

//*******************************************************
// 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() {
    String mystring = "Name: " + name + " " +
    "Account Number: " + acctNum + " " + "Balance: " + balance;
    return mystring;
}

// ----------------------------------------------
// Deducts $10 service fee
// ----------------------------------------------
public double chargeFee() {
    balance -=10;
    return balance;
}

// ----------------------------------------------
// Changes the name on the account
// ----------------------------------------------
public void changeName(String newName) {
    name = newName;
}
}

// CSE 110 Lab: ManageAccounts class
// [Your Name]
// Lab Section:

// ****************************************************************
// 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, account number 2222
    acct2 = new Account(500, "Joe", 2222);

// deposit $100 to Joe's account
    acct2.deposit(100);

// print Joe's new balance (use getBalance())
    System.out.println("New balance for Joe after $100 deposit: " + acct2.getBalance());

// withdraw $50 from Sally's account
    acct1.withdraw(50);
// print Sally's new balance (use getBalance())
    System.out.println("New balance for Sally after $50 withdrawal: " + acct1.getBalance());

// charge fees to both accounts and print new balance using value returned from chargeFee
    double fee;
    fee = acct2.chargeFee();
    System.out.println("New balance for Joe after fee: " + fee);
   
    fee = acct1.chargeFee();
    System.out.println("New balance for Sally after fee: " + fee);
   
// change the name on Joe's account to Joseph
    acct2.changeName("Joseph");

// print summary for both accounts (use toString())
    System.out.println(acct2.toString());
    System.out.println(acct1.toString());
}
}