Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Create an Account class (Account.java) that a bank might use to represen

ID: 3920048 • Letter: P

Question

Problem

Create an Account class (Account.java) that a bank might use to represent a customer's bank account.

Specification

Include a private variable of type int to represent the account balance.

Provide a constructor that has a parameter for initial balance and uses it to initialize the account balance.

Provide 3 member functions:

credit - adds an amount passed as an argument to the current balance.

debit - subtracts an amount passed as an argument from the current balance.

getBalance - returns the balance amount.

Create a program (AccountTest.java) that creates two Account objects and validate the methods of the class Account. (Call the methods passing the appropriate values to make sure they produce the expected results.)

Explanation / Answer

public class Account { private int balance; public Account(int balance) { this.balance = balance; } public int getBalance() { return balance; } public void credit(int amount) { balance += amount; } public void debit(int amount) { balance -= amount; } } public class AccountTest { public static void main(String[] args) { Account account1 = new Account(1000); Account account2 = new Account(500); account1.credit(500); System.out.println("Account1 balance: 1500 = " + account1.getBalance()); account2.debit(200); System.out.println("Account2 balance: 300 = " + account2.getBalance()); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote