009-001-013

Encapsulation with Private Modifier

Medium

Problem Description

Encapsulation with Private Modifier

In this problem, you will create a program that implements a BankAccount class with a private balance field accessible only through getter/setter methods, and displays the account balance to standard output after each deposit operation.

Learning Objective: Understand field encapsulation with private modifier and the role of getters/setters

Overview

The private modifier prevents direct access from outside the class. Getter and setter methods provide safe access.

Specifications

  • Create a BankAccount class
  • Make balance field private
  • getBalance() returns balance
  • deposit(int amount) adds money (ignores amounts <= 0)
  • Read initial balance, first deposit amount, and second deposit amount from standard input, then display the balance after each operation

Input Format

initial_balance
first_deposit_amount
second_deposit_amount

Output Format

Balance: <initial_balance>
Balance: <balance_after_first_deposit>
Balance: <balance_after_second_deposit>

Example

Input:

1000
500
-100

Output:

Balance: 1000
Balance: 1500
Balance: 1500

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