Write a Java class called BankAccount (Parts of the code is given below), which
ID: 3669612 • 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) 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
Explanation / Answer
import java.io.*;
import java.util.*;
public class BankAccount
{
public BankAccount(double b, String n)
{
double balance = b;
String name = n;
}
public void deposit(double d)
{
balance += d;
}
public void withdraw(double w)
{
balance -= w;
}
public String nickname()
{
System.out.print("Enter a new name: ");
Scanner kbIn = new Scanner(System.in);
String n = kbIn.nextLine();
return n;
}
double balance;
String name;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.