-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathPerson.java
63 lines (52 loc) · 1.63 KB
/
Person.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package Day_09_ClassObjects.Example1;
// p4n.in
// codeswithpankaj.com
public class Person {
// Instance variables
private String name;
private int age;
private String occupation;
// Constructor
public Person(String name, int age, String occupation) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
// Getter methods
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getOccupation() {
return occupation;
}
// Setter methods
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
// Main method to demonstrate the class
public static void main(String[] args) {
// Create a Person object
Person person = new Person("Pankaj", 30, "Engineer");
// Use the getter methods to retrieve the object's properties
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Occupation: " + person.getOccupation());
// Use the setter methods to modify the object's properties
person.setName("Jane");
person.setAge(35);
person.setOccupation("Doctor");
// Display the modified properties
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Occupation: " + person.getOccupation());
}
}