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 {
// Declare a private String variable for account number
private String accountNumber;
// Declare a private double variable for balance
private double balance;
// Getter method for account number
public String getAccountNumber() {
return accountNumber;
}
// Setter method for account number
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
// Getter method for balance
public double getBalance() {
return balance;
}
// Setter method for 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 ().
Output:
Account Number: SB-09876 Balance: 2000.0
Flowchart:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/encapsulation/java-encapsulation-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics