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

File Account.java contains a definition for a simple bank account class with met

ID: 3813517 • Letter: F

Question

File Account.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then modify it as follows:

Overload the constructor as follows:

public Account (double initBal, String owner, long number) - initializes the balance, owner, and account number as specified

public Account (double initBal, String owner) - initializes the balance and owner as specified; randomly generates the account number.

public Account (String owner) - initializes the owner as specified; sets the initial balance to 0 and randomly generates the account number.

Overload the withdraw method with one that also takes a fee and deducts that fee from the account.

File TestAccount.java contains a simple program that exercises these methods. Save it to your directory, study it to see what it does, and use it to test your modified Account class.

//************************************************************

// Account.java

//

// A bank account class with methods to deposit to, withdraw from,

// change the name on, and get a String representation

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

{

return "Name:" + name +

" Account Number: " + acctNum +

" Balance: " + balance;

}

}

//************************************************************

// TestAccount.java

//

// A simple driver to test the overloaded methods of

// the Account class.

//************************************************************

import java.util.Scanner;

public class TestAccount

{

public static void main(String[] args)

{

String name;

double balance;

long acctNum;

Account acct;

Scanner scan = new Scanner(System.in);

System.out.println("Enter account holder's first name");

name = scan.next();

acct = new Account(name);

System.out.println("Account for " + name + ":");

System.out.println(acct);

System.out.println(" Enter initial balance");

balance = scan.nextDouble();

acct = new Account(balance,name);

System.out.println("Account for " + name + ":");

System.out.println(acct);

System.out.println(" Enter account number");

acctNum = scan.nextLong();

acct = new Account(balance,name,acctNum);

System.out.println("Account for " + name + ":");

System.out.println(acct);

System.out.print(" Depositing 100 into account, balance is now ");

acct.deposit(100);

System.out.println(acct.getBalance());

System.out.print(" Withdrawing $25, balance is now ");

acct.withdraw(25);

System.out.println(acct.getBalance());

System.out.print(" Withdrawing $25 with $2 fee, balance is now ");

acct.withdraw(25,2);

System.out.println(acct.getBalance());

System.out.println(" Bye!");

}

}

Explanation / Answer

Hi

I have updated the code and highlighted the code changes below

TestAccount.java


import java.util.Scanner;

public class TestAccount
{
public static void main(String[] args)
{
String name;
double balance;
long acctNum;
Account acct;

Scanner scan = new Scanner(System.in);

System.out.println("Enter account holder's first name");
name = scan.next();
acct = new Account(name);
System.out.println("Account for " + name + ":");
System.out.println(acct);

System.out.println(" Enter initial balance");
balance = scan.nextDouble();
acct = new Account(balance,name);
System.out.println("Account for " + name + ":");
System.out.println(acct);

System.out.println(" Enter account number");
acctNum = scan.nextLong();
acct = new Account(balance,name,acctNum);
System.out.println("Account for " + name + ":");
System.out.println(acct);

System.out.print(" Depositing 100 into account, balance is now ");
acct.deposit(100);
System.out.println(acct.getBalance());
System.out.print(" Withdrawing $25, balance is now ");
acct.withdraw(25);
System.out.println(acct.getBalance());
System.out.print(" Withdrawing $25 with $2 fee, balance is now ");
acct.withdraw(25,2);
System.out.println(acct.getBalance());

System.out.println(" Bye!");
}
}

Account.java


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;
}

public Account (double initBal, String owner) {
   balance = initBal;
   name = owner;
}
public Account (String owner) {
   name = owner;
  
}

//-------------------------------------------------
//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");
}


public void withdraw(double amount, double fee)
{
if (balance >= (amount + fee))
balance -= (amount+fee);
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()
{
return "Name:" + name +
" Account Number: " + acctNum +
" Balance: " + balance;
}
}

Output:

Enter account holder's first name
Suresh
Account for Suresh:
Name:Suresh
Account Number: 0
Balance: 0.0

Enter initial balance
1000
Account for Suresh:
Name:Suresh
Account Number: 0
Balance: 1000.0

Enter account number
111
Account for Suresh:
Name:Suresh
Account Number: 111
Balance: 1000.0

Depositing 100 into account, balance is now 1100.0

Withdrawing $25, balance is now 1075.0

Withdrawing $25 with $2 fee, balance is now 1048.0

Bye!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote