Question: Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double) and three methods
Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double) and three methods deposit (double amount), withdraw(double amount) and displayBalance(). 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 displayBalance method prints out the account name and the current balance separated by a comma. For example if name is Jake and balance is 40.0 it should print
Jake, $40.00
public class BankAccount { 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 call the display method to display the current balance. Create another object called B2 and display the name and current balance for this object.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
