009-001-007

Access Control: Private Fields

Hard

Problem Description

Access Control: Private Fields

In this problem, you will create a program that implements a BankAccount class with a private balance field and accessor methods, reads a deposit amount from standard input, then displays the balance after the deposit.

Learning Objective: Understand how to hide fields from external access using private modifier and access only through methods

Overview

Create a BankAccount class and define balance as a private field. Allow getting balance and making deposits only through methods.

Specifications

  • Define following in BankAccount class
    • private int balance field
    • public void deposit(int amount) method (add to balance)
    • public int getBalance() method (return balance)
  • Execute following in main method
    • Read one integer (deposit amount) from standard input
    • Create BankAccount instance
    • Call deposit() with the read amount
    • Get balance with getBalance() and output as "Balance: balance"

Input Format

amount (integer)

Output Format

Balance: balance

Example

Input:

1000

Output:

Balance: 1000

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up