Class Test
Class Test
Question
One of the key concepts of object-oriented programming (OOP) is
encapsulation, emphasizes concealing sensitive data from unauthorized access
or alteration, while setters and getters are provided for modification purposes.
To demonstrate this concept, create a Java program comprising two classes:
Person and Main. In the Person class, define the attributes of an individual,
including their first name, last name, age, gender, marital status, native
language, and specify a method that prints the person's particulars. In the Main
class, you should have at least two (2) objects and then utilize setters and getters
to assign and retrieve the values for these attributes, with the user providing the
input values for the first object. Then provide your own values for the second
object and invoke the print function on it.
Code
Person Class
public class Person {
private String firstName;
private String lastName;
private int age;
private String gender;
private String maritalStatus;
private String nativeLanguage;
public Person() {
Main Class
import java.util.Scanner;
System.out.println("Person 1: ");
person1.printPerson();
System.out.println("Person 2: ");
person2.printPerson();
}
Code
import java.util.Scanner;
scanner.close();
}