Given program:
BankAccount.java:
//Define the class "BankAccount"
public class BankAccount
{
//Declare required variables
private double balance;
private int accountNumber;
// Define a default constructor
public BankAccount
{
//Initialize the balance amount
balance=0;
}
// Define the parametrized constructor
public BankAccount(double initialBalance)
{
//Initialize the balance amount
balance=initialBalance;
}
//Give function definition to deposit amount
public void deposit(double amount)
{
//Compute the balance
balance=balance+amount;
}
//Give function definition to withdraw amount
public void withdraw(double amount)
{
//Compute the balance
balance=balance-amount;
}
/*Give function definition to return the
balance amount*/
public double getBalance()
{
//Return statement
return balance;
}
}
BankAccountTester.java:
//Define the class BankAccountTester
class BankAccountTester
{
//Define the main method
public static void main(String[] args)
{
//Create an object for the BankAccount class and initialize it with the amount
BankAccount account = new BankAccount(1000);
//Withdraw 500 from the account
account.withdraw(500);
//Withdraw 400 from the account
account.withdraw(400);
//Print the expected result
System.out.println("Remaining balance in the account is: $"+account.getBalanceAmount());
}
}
Explanation:
BankAccount.java:
In the above program,
- Define a class “BankAccount”.
- Declare the variable “balance”.
- Define a default constructor.
- Initialize the balance amount.
- Define a parameterized constructor.
- Initialize the balance amount.
- Give the function definition “deposit()” to withdraw amount from the account.
- Compute the balance amount.
- Give the function definition “withdraw()” to withdraw amount from the account.
- Compute the balance amount
- Give function definition to return the balance amount.
- Return “balance”.
BankAccountTester.java:
In the above program,
- Define the class “BankAccountTester”.
- Define the method “main ()”.
- Create an object for the “BankAccount” class and initialize the balance amount.
- Withdraw 500 from the balance using “withdraw ()” method.
- Again withdraw 400 from the balance using “withdraw ()” method.
- Finally, print the expected result.
Want to see the full answer?
Check out a sample textbook solutionChapter 3 Solutions
Big Java, Binder Ready Version: Early Objects
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education