Problem Design and implement these 4 files: A parent class called Person with na
ID: 3750928 • Letter: P
Question
Problem
Design and implement these 4 files:
A parent class called Person with name and id attributes—id should be an integer
Student inherits from Person and adds degree and major attributes (eg Bachelor of Science degree, major in computing science)
Faculty inherits from Person and adds faculty and department attributes (eg Faculty of Science, computing science department)
A driver file to test the 3 classes above.
The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set (mutator) methods for each attribute, and a toString method
Child classes should call parent methods whenever possible to minimize code duplication.
The driver program must test all the methods in each of the classes. Include comments in your output to describe what you are testing, for example System.out.println(“testing Person toString, accessor and mutator”);. Print out some blank lines in the output to make it easier to read and understand what is being output.
Explanation / Answer
Below is your code: -
Person.java
public class Person
{
private String name;
private int id;
public Person() {
}// default constructor
public Person(String name, int id)// parameterized constructor
{
this.id = id;
this.name = name;
}
// setter method
public void setName(String name)
{
this.name = name;
}
public void setId(int id)
{
this.id = id;
}
// getter methods
public String getName()
{
return this.name;
}
public int getId()
{
return this.id;
}
// toString method
public String toString()
{
return "Name : " + this.name + " Id : " + this.id;
}
}
Student.java
public class Student extends Person
{
private String degree, major;
public Student() {
}
public Student(String name, int id, String degree, String major)
{
super(name, id);
this.degree = degree;
this.major = major;
}
public void setDegree(String degree)
{
this.degree = degree;
}
public void setMajor(String major)
{
this.major = major;
}
public String getMajor()
{
return this.major;
}
public String getDegree()
{
return this.degree;
}
public String toString()
{
return super.toString() + " Degree : " + this.degree + " Major : " + this.major;
}
}
Faculty.java
public class Faculty extends Person
{
private String faculty, Department;
public Faculty() {
}
public Faculty(String name, int id, String faculty, String Department)
{
super(name, id);
this.faculty = faculty;
this.Department = Department;
}
public void setfaculty(String faculty)
{
this.faculty = faculty;
}
public void setDepartment(String Department)
{
this.Department = Department;
}
public String getDepartment()
{
return this.Department;
}
public String getfaculty()
{
return this.faculty;
}
public String toString()
{
return super.toString() + " faculty : " + this.faculty + " Department : " + this.Department;
}
}
Test.java
public class Test {
public static void main(String[] args)
{
Person pers = new Person();
System.out.println("Testing default constructor of Person class : ");
System.out.println(pers);
Person pers1 = new Person("Vijay", 21);
System.out.println();
System.out.println("Testing parametrized constructor and toString of Person class.");
System.out.println();
System.out.println(pers1);
pers.setName("Rakesh");
pers.setId(121);
System.out.println();
System.out.println("Testing setters and getters : ");
System.out.println("Person Details: Name: "+pers.getName()+" ID: "+pers.getId());
System.out.println();
Student st1 = new Student("Jay", 79, "B.E Computer", "JAVA");
System.out.println("Here we use the parameterize construnctor of student class and toString() method .");
System.out.println(" " + st1);
Student st2 = new Student();
st2.setName("Viral");
st2.setId(79);
st2.setDegree("B.E. Computer");
st2.setMajor("VB .Net");
System.out.println(
" Here we use the defualt construnctor of student class and toString() method of the Person class and accessor and mutator method .");
System.out.println(" Name : " + st2.getName() + " Id : " + st2.getId() + " Degree : " + st2.getDegree()
+ " Major" + st2.getMajor());
System.out.println();
Faculty f1 = new Faculty("Madhav", 1001, "Faculty of Scince", "Department of Computer science");
System.out.println(
"Here we use the parameterize construnctor of Faculty class and toString() method both the parent and child class.");
System.out.println(" " + f1);
Faculty f2 = new Faculty();
f2.setName("Viral");
f2.setId(79);
f2.setfaculty("Faculty of management");
f2.setDepartment("Department of Management.");
System.out.println(
" Here we use the defualt construnctor of student class and toString() method of the Person class and accessor and mutator method .");
System.out.println(" Name : " + f2.getName() + " Id : " + f2.getId() + " Facaulty : " + f2.getfaculty()
+ " Department " + f2.getDepartment());
}
}
Output
Testing default constructor of Person class :
Name : null
Id : 0
Testing parametrized constructor and toString of Person class.
Name : Vijay
Id : 21
Testing setters and getters :
Person Details:
Name: Rakesh
ID: 121
Here we use the parameterize construnctor of student class and toString() method .
Name : Jay
Id : 79
Degree : B.E Computer
Major : JAVA
Here we use the defualt construnctor of student class and toString() method of the Person class
and accessor and mutator method .
Name : Viral
Id : 79
Degree : B.E. Computer
MajorVB .Net
Here we use the parameterize construnctor of Faculty class and toString() method both the parent and child class.
Name : Madhav
Id : 1001
faculty : Faculty of Scince
Department : Department of Computer science
Here we use the defualt construnctor of student class and toString() method of the Person class
and accessor and mutator method .
Name : Viral
Id : 79
Facaulty : Faculty of management
Department Department of Management.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.