Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write source code in java. Define a class named Person and save the class in a f

ID: 3808231 • Letter: W

Question

Write source code in java.

Define a class named Person and save the class in a file named Person.java.

This class has three data fields:

     - name: String

     - age : int

     - gender: String

The class has two constructors:

     1. Constructor without arguments: initialize name and gender to empty string, and age to 0.

     2. Constructor with two String type arrangements and an int type argument. Use them to initialize the three data fields properly.

Add the getter and setter methods properly.

Then write a demo program to test the class.

Explanation / Answer


public class Person {
   private int age;
   private String name;
   private String gender;
  
   public Person(){
       super();
       this.age=0;
       this.name="";
       this.gender="";
   }
   public Person(String gender, String name, int age){
       this.gender = gender;
       this.name = name;
       this.age = age;
   }
   public int getAge(){
       return this.age;
   }
   public String getGender(){
       return this.gender;
   }
   public String getName(){
       return this.name;
   }
  
   public void setAge(int age){
       this.age = age;
   }
   public void setGender(String gender){
       this.gender = gender;
   }
   public void setName(String name){
       this.name = name;
   }
  
   public static void main(String args[]){
       Person person = new Person();
       person.setAge(21);
       person.setGender("Male");
       person.setName("Behra");
       Person person2 = new Person("Female","Ananya",21);
       System.out.println(person.getName()+" "+person.getGender()+" "+person.getAge());
       System.out.println(person2.getName()+" "+person2.getGender()+" "+person2.getAge());
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote