Create a BankAccount class that has the following: a) Instance variables for: na
ID: 3566697 • Letter: C
Question
Create a BankAccount class that has the following:
a) Instance variables for: name and balance
b) Accessor/mutator method for each instance variable (You will end up having 4 of these types of methods)
c) Create a displayAccountInfo() method that displays the account information as follows:
Name:
Create a BankAccountDriver class that does the following:
b) Prompt the user for their name and balance. After each prompt, call the correct mutator method on the instance to set that data.
Name: John Doe Account Balance: 5000
Name: John Doe
<account holder's="" name=""> Account Balance: $5,000.00
Explanation / Answer
The BankAccount class is given below:
import java.util.Scanner;
public class BankAccount {
// declare instance variables
String name;
double balance;
Scanner in = new Scanner(System.in);
public BankAccount() {
System.out.println("Name: ");
setName(in.nextLine());
System.out.println("Balance: ");
setBalance(in.nextDouble());
}
// method to set the name
public void setName(String s) {
name = s;
}
// method to get the name
public String getName() {
return name;
}
// method to set balance
public void setBalance(double amount) {
balance = amount;
}
// method to get the balance
public double getBalance() {
return balance;
}
// method displayAccountInfo()
public void displayAccountInfo() {
System.out.println("The account holder name is: $"+this.getName());
System.out.println("The account balance is: "+this.getBalance());
}
}
The BankAccountDriver class is given below:
public class BankAccountDriver {
// main method
public static void main(String[] args) {
// create objects to access methods of BankAccount
BankAccount ba1 = new BankAccount();
ba1.displayAccountInfo();
BankAccount ba2 = new BankAccount();
ba2.displayAccountInfo();
}
}
Sample Output:
Name:
John Marshall
Balance:
2000
The account holder name is: John Marshall
The account balance is: $2000.0
Name:
Miranda James
Balance:
5500
The account holder name is: Miranda James
The account balance is: $5500.0