You will create two classes to demonstrate inheritance - a Person and a Student
ID: 3572858 • Letter: Y
Question
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
Example PrintInfo output:
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:
Example PrintInfo output:
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 StudentTester to test your work. Your tester should do the following
PrettyPrint Person:
PrettyPrint Student:
Explanation / Answer
HI, Please find my implementation.
Please let me know in case of any issue.
public class Person {
protected String name;
protected int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
public void printInfo(){
System.out.println("Name: "+name);
System.out.println("Age: "+age);
}
}
class Student extends Person{
private int ID;
private double GPA;
private static int nextID = 1;
public Student(String name, int age) {
super(name, age);
GPA = 4.0;
ID = nextID;
nextID = nextID + 1;
}
public Student(String name, int age, double GPA) {
super(name, age);
this.GPA = GPA;
ID = nextID;
nextID = nextID + 1;
}
public int getID() {
return ID;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
public void printInfo(){
super.printInfo();
System.out.println("GPA: "+GPA);
System.out.println("ID: "+ID);
}
}
public class StudentTester {
public static void prettyPrint(Person person){
System.out.println("-----PERSON-----");
person.printInfo();
System.out.println("--------------------");
}
public static void prettyPrint(Student student){
System.out.println("-----Student-----");
student.printInfo();
}
public static void main(String[] args) {
Person person = new Person("Joe Anderson", 19);
person.setAge(20);
person.setName("Alex Bob");
System.out.println("Name: "+person.getName()+", Age: "+person.getAge());
Student s1 = new Student("Pravesh", 24);
Student s2 = new Student("Lunda A", 22, 3.4);
prettyPrint(person);
prettyPrint(s1);
prettyPrint(s2);
}
}
/*
Sample run:
Name: Alex Bob, Age: 20
-----PERSON-----
Name: Alex Bob
Age: 20
--------------------
-----Student-----
Name: Pravesh
Age: 24
GPA: 4.0
ID: 1
-----Student-----
Name: Lunda A
Age: 22
GPA: 3.4
ID: 2
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.