w3resource

Java Encapsulation: Implementing the BankAccount Class with Getter and Setter Methods

Java Encapsulatiion: Exercise-2 with Solution

Write a Java program to create a class called BankAccount with private instance variables accountNumber and balance. Provide public getter and setter methods to access and modify these variables.

Sample Solution:

Java Code:

// BankAccount.java
// BankAccount Class

class BankAccount {
  private String accountNumber;
  private double balance;

  public String getAccountNumber() {
    return accountNumber;
  }

  public void setAccountNumber(String accountNumber) {
    this.accountNumber = accountNumber;
  }

  public double getBalance() {
    return balance;
  }

  public void setBalance(double balance) {
    this.balance = balance;
  }
}

Explanation:

In the above code, we have a BankAccount class that encapsulates the private instance variables accountNumber and balance. By making these variables private, we ensure that they can only be accessed and modified through the public getter and setter methods.

The getter methods (getAccountNumber() and getBalance()) are public methods that provide access to private variables. They allow other classes to retrieve accountNumber and balance values. In this case, getAccountNumber() returns the value of accountNumber as a String, and getBalance() returns the value of balance as a double.

The setter methods (setAccountNumber() and setBalance()) are also public methods that modify private variables. Private variables are created by accepting parameters (String accountNumber and double balance). In this case, setAccountNumber() sets accountNumber value based on the provided argument. SetBalance() sets the balance value based on the provided argument.

// Main.java
// Main Class

public class Main {
  public static void main(String[] args) {
    // Create an instance of BankAccount
    BankAccount account = new BankAccount();

    // Set values using setter methods
    account.setAccountNumber("SB-09876");
    account.setBalance(2000.0);

    // Get values using getter methods
    String accountNumber = account.getAccountNumber();
    double balance = account.getBalance();

    // Print the values
    System.out.println("Account Number: " + accountNumber);
    System.out.println("Balance: " + balance);
  }
}

In the above Main class, an instance of the BankAccount class is created using the BankAccount constructor. The private variables accountNumber and balance are then set using the setter methods.

To retrieve these values, the getter methods are called (String accountNumber = account.getAccountNumber() and double balance = account.getBalance()). After retrieving the values, System.out.println () is used to print them ().

Sample Output:

Account Number: SB-09876
Balance: 2000.0

Flowchart:

Flowchart: Java Encapsulation: Implementing the BankAccount Class with Getter and Setter Methods.
Flowchart: Java Encapsulation: Implementing the BankAccount Class with Getter and Setter Methods.
Flowchart: Java Encapsulation: Implementing the BankAccount Class with Getter and Setter Methods.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Implementing Person Class with Getter and Setter Methods.
Next: Implementing Rectangle Class with Getter and Setter Methods.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.