Person.java
Code:
public class Person {
private String name;
private int age;
public Person() {
this.name = "Unknown";
this.age = 0;
}
public String getName() {jav
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Main.java
Code:
public class Main {
public static void main(String[] args) {
// Create a new Person object
Person person = new Person();
// Set properties using setter methods
person.setName("John Doe");
person.setAge(30);
// Get and display the properties using getter methods
System.out.println("Person's Name: " + person.getName());
System.out.println("Person's Age: " + person.getAge());
// Use custom method to display all info
person.displayInfo();
}
}
Output:
No comments:
Post a Comment