Write a Java class called BankAccount (Parts of the code is given below), which has two private fields: name (String) and balance (double), and three methods: deposit(double amount), withdraw(double amount) and toString(). Write the necessary constructors, accessor methods and mutator methods. The deposit() method adds the amount to the account causing the current balance to increase, withdraw() method subtracts the amount causing the current balance to decrease and toString() method should return the name and the current balance separated by a comma. For example, if you print out the object with name Jake and balance 40.0 then it should print: Jake, $40.00 public class BankAccount { private String name; ... public void deposit(double amount) { balance = balance + amount; } public void withdraw(double amount) { balance = balance – amount; } public BankAccount(){}; public BankAccount(String name_new, double bal_new) { ... } ... } Write a client program called BankAccountClient that creates a BankAccount object called B1 and initializes name and balance at the time of instantiating the object using the default constructor. Call the deposit() method to add $500 to this account and call the toString() method to print the current balance using System.out.println() from the main(). Next, call the withdraw() method to subtract $300 from this account and print B1 using System.out.println() from the main(). Create another object called B2 without initializing it and display the name and current balance for this object using System.out.println(). Finally, use the accessor/mutator methods to assign name and balance to B2. Print the object B2, using System.out.println()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a Java class called BankAccount (Parts of the code is given below), which has two private
fields: name (String) and balance (double), and three methods: deposit(double amount),
withdraw(double amount) and toString(). Write the necessary constructors, accessor methods and
mutator methods. The deposit() method adds the amount to the account causing the current balance
to increase, withdraw() method subtracts the amount causing the current balance to decrease and
toString() method should return the name and the current balance separated by a comma. For
example, if you print out the object with name Jake and balance 40.0 then it should print:
Jake, $40.00
public class BankAccount {
private String name;

...
public void deposit(double amount) {
balance = balance + amount;
}
public void withdraw(double amount) {
balance = balance – amount;
}
public BankAccount(){};
public BankAccount(String name_new, double bal_new) {
...
}
...
}
Write a client program called BankAccountClient that creates a BankAccount object called B1 and
initializes name and balance at the time of instantiating the object using the default constructor.
Call the deposit() method to add $500 to this account and call the toString() method to print the
current balance using System.out.println() from the main(). Next, call the withdraw() method to
subtract $300 from this account and print B1 using System.out.println() from the main(). Create
another object called B2 without initializing it and display the name and current balance for this
object using System.out.println(). Finally, use the accessor/mutator methods to assign name and
balance to B2. Print the object B2, using System.out.println().

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education