Java Encapsulation: Implementing Person Class with Getter and Setter Methods
Java Encapsulation: Exercise-1 with Solution
Write a Java program to create a class called Person with private instance variables name, age. and country. Provide public getter and setter methods to access and modify these variables.
Sample Solution:
Java Code:
// Person.java
// Person Class
class Person {
// Private field to store the name of the person
private String name;
// Private field to store the age of the person
private int age;
// Private field to store the country of the person
private String country;
// Public method to get the name of the person
public String getName() {
return name;
}
// Public method to set the name of the person
public void setName(String name) {
this.name = name;
}
// Public method to get the age of the person
public int getAge() {
return age;
}
// Public method to set the age of the person
public void setAge(int age) {
this.age = age;
}
// Public method to get the country of the person
public String getCountry() {
return country;
}
// Public method to set the country of the person
public void setCountry(String country) {
this.country = country;
}
}
// Main.java
// Main Class
public class Main {
public static void main(String[] args) {
// Create an instance of Person
Person person = new Person();
// Set values using setter methods
person.setName("Arthfael Viktorija");
person.setAge(25);
person.setCountry("USA");
// Get values using getter methods
String name = person.getName();
int age = person.getAge();
String country = person.getCountry();
// Print the values
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Country: " + country);
}
}
Output:
Name: Arthfael Viktorija Age: 25 Country: US
Explanation:
In the above exercise, the Person class encapsulates the private instance variables name, age, and country. These variables are declared private, which means they cannot be accessed outside the Person class.
To access and modify these private variables, public getter and setter methods are provided. The getter methods (getName(), getAge(), and getCountry()) allow other classes to retrieve the values of private variables. The setter methods (setName(), setAge(), and setCountry()) provide a way to modify the values of private variables.
In the Main class, an instance of the Person class is created using the Person constructor (Person person = new Person()). Then, the setter methods are used to set the values of the private variables name, age, and country.
To retrieve these values, the getter methods are called (String name = person.getName(), int age = person.getAge(), String country = person.getCountry()). Finally, the retrieved values are printed using System.out.println().
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Java Encapsulation Exercises Home.
Next: Implementing the BankAccount 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-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics