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

Write a Java class called BankAccount (Parts of the code is given below), which

ID: 3782281 • Letter: W

Question

Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the current balance to decrease and toString() method returns account name and the current balance separated by a comma. For example if name is Jake and balance is 40.0 it should return

Jake, $40.00

public class BankAccount {

    private String name;

   

    public void deposit(double amount) {

        balance = balance + amount;

    }

    public void withdraw(double amount) {

       

    }

       

}

Write a client program called BankAccountClient that creates a BankAccount object called B1 and assigns “John” to its name field and 1000 to balance field. Call the deposit method to deposit 500 to this account and call the display method to display the current balance. Then, withdraw 300 from this account and print the object using println. Create another object called B2 without passing parameters and display the name and current balance for this object.

Explanation / Answer

SOURCE CODE:

BankAccount.java

public class BankAccount {
private String name;
private double balance;
  
public BankAccount() {
this.name = null;
this.balance = 0.0;
}

public BankAccount(String name, double balance) {
this.name = name;
this.balance = balance;
}

public double getBalance() {
return balance;
}

public String getName() {
return name;
}

public void deposit(double amount) {
balance = balance + amount;
}

public void withdraw(double amount) {
balance = balance - amount;
}

public String toString() {
return name + ",$" + balance;
}
  
public void display()
{
System.out.println(this.toString());
}
}

BankAccountClient.java


public class BankAccountClient {
public static void main(String args[])
{
BankAccount B1 = new BankAccount("John",1000);
B1.deposit(500);
B1.display();
B1.withdraw(300);
System.out.println(B1);
BankAccount B2 = new BankAccount();
B2.display();
}
}

OUTPUT:

John,$1500.0
John,$1200.0
null,$0.0

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