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

8.9 Lab: Person and Student You will create two classes to demonstrate inheritan

ID: 3571252 • Letter: 8

Question

8.9 Lab: Person and Student

You will create two classes to demonstrate inheritance - a Person and a Student

A person class is a simple data class consisting of the following methods

getName - returns a string

setName - takes a string

getAge - returns an int

setAge - takes an it

printInfo - prints the name and age

Example PrintInfo output:

Name: Joe Anderson Age: 19 Give the fields of the person class protected access. Include a constructor to set both the Name and Age A student IS A person. It will do most things a person does but a bit more Define a student as an extension of a person. Add the following methods getID retums an int (see more info below) getGPA retums a double printinfo prints the name, age, GPA and ID (use super to call the base class printinfo) Example Printinfo output: Name: Joe Anderson Age: 19 GPA: 3.9 ID: 102 For the ID, use a static class int to represent the nextID. For each new student, assign the ID to the student and increment the nextID. You do not need a set ID method, as they are only to be assigned in the constructor. Include a constructor to set the Name and Age assign 4.0 to the GPA. Include a second constructor to set the Name, Age, and GPA Create a class called Student Tester to test your work. Your tester should do the following Define your main method Define two static methods o pretty Print with a parameter of a Person (see output below)-Calls Printlnfo of object but adds more formatting o pretty Print with a parameter of a Student (see output below) calls Printinfo of object but adds more formatting Create one Person objects o set the name and age using the constructor

Explanation / Answer

Person.java

public class Person {
   //Declaring variables
protected String name;
protected int age;

//Parameterized constructor
public Person(String name, int age) {
   super();
   this.name = name;
   this.age = age;
}

//Setters and getter methods
public String getName() {
   return name;
}
public void setName(String name) {
   this.name = name;
}
public int getAge() {
   return age;
}
public void setAge(int age) {
   this.age = age;
}

//this method is used to display the Person Information
public void printInfo()
{
System.out.println("Name: "+getName());
System.out.println("Age: "+getAge());
}
}

_________________

Student.java

public class Student extends Person {
   //Declaring variables
private int id;
private double GPA;

//Declaring static variable
static int nextID;


//Parameterized constructor with 2 parameters
public Student(String name, int age) {
   super(name, age);
   this.id =++nextID;
   GPA =4.0;
}

//Parameterized constructor with three parameters
public Student(String name, int age, double gPA) {
   super(name, age);
   this.id =++nextID;
   GPA = gPA;
}

//Setters and getters
public int getId() {
   return id;
}

public double getGPA() {
   return GPA;
}
public void setGPA(double gPA) {
   GPA = gPA;
}

//this method is used to display the Student Information
public void printInfo()
{
   //Calling the super class priontInfo() method
   super.printInfo();
   System.out.println("GPA:"+getGPA());
   System.out.println("ID:"+getId());
}
}

____________________________

StudentTester.java

public class StudentTester {

   public static void main(String[] args) {
       //Creating the Person class Object by passing the values as arguments
       Person p=new Person("Joe Anderson",19);
      
       //Calling the setter methods
       p.setName("Kane Williams");
       p.setAge(22);
      
       //Displaying the Person information using the getter() methods
       System.out.println("____Displaying Person Information using getter methods____");
       System.out.println("Name :"+p.getName());
       System.out.println("Age :"+p.getAge());
      
       //Creating the Student class Object
Student st1=new Student("Micheal Wan",23);
  
//Creating the Student class Object
Student st2=new Student("Robet Billy",27, 3.8);
  
//Calling the setter methods on the Student Class Object
st2.setName("Ken Rodgers");
st2.setAge(25);
st2.setGPA(3.9);
  
//Calling the getter methods on the Student class object
System.out.println("____Displaying Student Information using getter methods____");
System.out.println("Name:"+st2.getName());
System.out.println("Age:"+st2.getAge());
System.out.println("GPA:"+st2.getGPA());
System.out.println("ID:"+st2.getId());
  
  
//Calling the prettyPrint() method by passing the Person class reference as input
System.out.println("___Calling prettyPrint() method on Objects___");
prettyPrint(p);
  
//Calling the prettyPrint() method by passing the Student class reference as input
prettyPrint(st1);
  
//Calling the prettyPrint() method by passing the Student class reference as input
prettyPrint(st2);
   }

  
   //This method will displays the Person information
   private static void prettyPrint(Person p) {
       System.out.println("---- Person ----");
       p.printInfo();
       System.out.println("---------------");
   }
  
   //This method will displays the Student information
   private static void prettyPrint(Student st) {
       System.out.println("---- Student ----");
       st.printInfo();
       System.out.println("---------------");
   }

}

________________________

____Displaying Person Information using getter methods____
Name :Kane Williams
Age :22
____Displaying Student Information using getter methods____
Name:Ken Rodgers
Age:25
GPA:3.9
ID:2
___Calling prettyPrint() method on Objects___
---- Person ----
Name: Kane Williams
Age: 22
---------------
---- Student ----
Name: Micheal Wan
Age: 23
GPA:4.0
ID:1
---------------
---- Student ----
Name: Ken Rodgers
Age: 25
GPA:3.9
ID:2
---------------

___________Thank You

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