Design three classes: Person, Student and faculty classes as below. 2. Implement
ID: 3607200 • Letter: D
Question
Design three classes: Person, Student and faculty classes as below.
2. Implement toString() methods for each class that will print all data fields including data fields in super class. (Using "super.toString())
3. In main method:
a. create one student object and one faculty object
b. print the faculty's information(name, id, birthday, salary) by passing the student object to System.out.println()
c. print the student's information (name, id, birthday) by passing the student object to System.out.println().
Person -name: String -birthday: String +Person (String name, String birthdate + toString() : StringExplanation / Answer
import java.util.*;
class Main {
public static void main(String args[]){
//Create Student Object
Student st = new Student("Prakash K","25-06-1994",6);
//Create Faculty Object
Faculty f = new Faculty("Ravi V","12-02-1984",34,25000);
//Printing Student Information
System.out.println("Student Information:");
System.out.println(st.toString());
//Printing Faculty Information
System.out.println(" Faculty Information:");
System.out.println(f.toString());
}
}
class Person {
String name;
String birthday;
Person(String name, String birthday){
this.name = name;
this.birthday = birthday;
}
//Implementing toString Method
public String toString(){
return "Name : "+this.name+" Birthday: "+this.birthday;
}
}
class Student extends Person {
int ID;
Student(String name, String birthday, int ID){
super(name,birthday);
this.ID = ID;
}
//Implementing toString Method to print all data fields including data fields in super class.
public String toString(){
return super.toString()+" ID: "+this.ID;
}
}
class Faculty extends Person {
int ID;
double salary;
Faculty(String name, String birthday, int ID, double salary){
super(name,birthday);
this.ID = ID;
this.salary = salary;
}
//Implementing toString Method to print all data fields including data fields in super class.
public String toString(){
return super.toString()+" ID: "+this.ID+" Salary: "+this.salary;
}
}
/* Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.