In this lab, please define ONE base class Person, and two derived classes Studen
ID: 3705863 • Letter: I
Question
In this lab, please define ONE base class Person, and two derived classes Student and Teacher. For each class, you should define a set of data fields that are appropriate for it. You should also declare a "driver" class that contains a main method that can create at least ONE student and at least ONE teacher, and you should also print out the detailed info for the student and the teacher. String: name int: age String: ssn boolean: alive Personive String: id doubje:stpa Student String: stu_id Teacher int: salary String: grade int: num_yr_profExplanation / Answer
Solution:
Note: Implementation is done ine java.
code:
Person.java
package chegg;
public class Person
{
private String name;
private int age;
private String ssn;
private boolean alive;
public String getName()
{
return this.name;
}
public void setName(String argName)
{
this.name=argName;
}
public int getAge()
{
return this.age;
}
public void setAge(int argAge)
{
this.age=argAge;
}
public String getSSN()
{
return this.ssn;
}
public void setSSN(String argSSN)
{
this.ssn=argSSN;
}
public boolean getAlive()
{
return this.alive;
}
public void setAlive(boolean argAlive)
{
this.alive=argAlive;
}
void Display(){
System.out.println("Name : " + name);
System.out.println("Age : " + age);
System.out.println("SSN : " + ssn);
System.out.println("alive : " + alive);
}
}
Student2.java:
package chegg;
public class Student2 extends Person
{
private String stu_id;
private double gpa;
private String grade;
public String getStuId()
{
return
this.stu_id;
}
public void setStuid(String argStuid)
{
this.stu_id=argStuid;
}
public double getGPA()
{
return
this.gpa;
}
public void setGPA(double argGPA)
{
this.gpa=argGPA;
}
public String getGrade()
{
return this.grade;
}
public void setGrade(String argGrade)
{
this.grade=argGrade;
}
@Override
void Display() {
System.out.println("stu_id: " + stu_id);
System.out.println("gpa: " + gpa);
System.out.println("Grade: " + grade);
}
}
Teacher.java:
package chegg;
public class Teacher extends Person
{
private String id;
private int salary;
private int num_yr_prof;
public String getID()
{
return
this.id;
}
public void setId(String argId)
{
this.id=argId;
}
public int getSalary()
{
return
this.salary;
}
public void setSalary(int argSalary)
{
this.salary=argSalary;
}
public int getNumYear()
{
return
this.num_yr_prof;
}
public void setNumYear(int argNumYear)
{
this.num_yr_prof=argNumYear;
}
@Override
void Display(){
System.out.println("ID: " + id);
System.out.println("Salary : " + salary);
System.out.println("Number of Years being a professor: " + num_yr_prof);
}
}
StudentTeacherMain.java:
package chegg;
public class StudentTeacherMain {
public static void main(String args[])
{
Person pObject = new Person("Peter Parker", 18, "332", true);
Student2 sObject = new Student"123", 7.8, "A");
Teacher tObject = new Teacher("1256", 50000, 5);
sObject.setId(100);
sObject.setName("Avenger");
System.out.println("Person :-------------");
pObject.Display();
System.out.println("Student :-----------");
sObject.Display();
System.out.println("Teacher :-----------");
tObject.Display();
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.