In Java, write the following class: BankAccount Contains the following instance
ID: 3778978 • Letter: I
Question
In Java, write the following class:
BankAccount
Contains the following instance variables:
name of type String,
deposit of type double initialized to a zero.
balance of type double initialized to a zero
Contains the following constructors:
A default constructor BankAccount(){}
A 2-arg constructor BankAccount(String name, double deposit) that takes in a string value and sets the name and takes in a double value to set the deposit. Set balance = balance+deposit within this 2-arg constructor.
Contains the following methods:
A setter for name and deposit instance variables called setName and setDeposit, respectively. When you set the deposit to a new value, also set balance = balance+deposit within the same setter setDeposit method.
A getter for name and balance instance variables.
-Thanks
Explanation / Answer
BankAccount.java
public class BankAccount {
//declaring instance variables
private String name;
private double deposit=0;
private double balance=0;
//Zero argumented constructor
public BankAccount() {
super();
}
//Parameterized constructor
public BankAccount(String name, double deposit) {
super();
this.name = name;
this.deposit = deposit;
this.balance=balance+deposit;
}
//Setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setDeposit(double deposit) {
this.balance =balance+ deposit;
}
public double getBalance() {
return balance;
}
/* toString() method is used to display
* the contents of an object inside it.
*/
@Override
public String toString() {
return "BankAccount# Name=" + name + " Balance=" + balance;
}
}
__________________________
Driver.java
public class Driver {
public static void main(String[] args) {
/* Creating the BankAccount class object by passing
* the name and deposit amount as arguments
*/
BankAccount ba1=new BankAccount("Willaims",5000);
//Displaying the bank account information
System.out.println(ba1.toString());
/* Calling the setters method by passing
* the deposit amount as argument
*/
ba1.setDeposit(10000);
//Displaying the bank account information
System.out.println("____After Depositing 10000 into Bank Account____");
System.out.println(ba1.toString());
}
}
__________________
output:
BankAccount#
Name=Willaims
Balance=5000.0
____After Depositing 10000 into Bank Account____
BankAccount#
Name=Willaims
Balance=15000.0
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.