(Inheritance ) Write a Java program to meet the following requirements: 1. Desig
ID: 3807508 • Letter: #
Question
(Inheritance ) Write a Java program to meet the following requirements:
1. Design and declaration. All classes’ data fields should be private. (40%)
a. Design a class named Person (5%)
i. Person has a data field: name. Default: name is “unknown”,
ii. Person has a method printInfo() which will print all data fields.
iii. and its two subclasses named Student and Employee.
b. Design a class named Employee which is a subclass of Person. (5%)
i. Employee has a data field: salary. Default: salary is 0,
ii. Employee has a method printInfo()which will print all data fields( including
data fields in super class) (Hint: use super keyword to get info from super class.)
c. Design a class named Faculty which is a subclass of Employee. (10%)
i. Faculty has a data field: dept. Default: dept is “none”,
ii. Faculty has a method printInfo()which will print all data fields( including
data fields in super class)
d. Design a class named Student which is a subclass of Person. (10%)
i. Student has two data fields: age, default 0; email address, default: “none”
ii. Student has a method printInfo()which will print all data fields( including
data fields in super class)
e. Implement necessary constructors, accessor and mutator methods for all the above
classes.(10%)
2. Write a test program to create two Students and two Faculties, assign (NOT read from console) the
following information, and invokes their printInfo methods. xxxx is your Kean ID. (10%)
A Student object name: unknown, age: -1, email: none
A Student object name: “demo_s2”, age: 23, email:
A Faculty object name: “demo_f1”, salary: -1, dept: none
A Faculty object name: “demo_f2”, salary: 10000, dept: “CS”
3. Your program output should look like below: (10%)
Student name: unknown, age: -1, Email: none
Student name: demo_s2, age: 23, Email: xyz.com
Faculty name: demo_f1, salary: -1, dept: none
Faculty name: demo_f2, salary: 10000, dept: CS
Program4 is developed by <your Kean email ID>.
• This project should be coded in ONE java file.
• How to submit:
– Please name the Java Class as XXXX_Program4 and file name as XXXX_Program4.java
where XXXX is your Kean email ID.
– Please submit the program online at http://imc.kean.edu/students/
• Assigned and Due date (by submit timestamp):
– Assigned on 3/22/2017.
– Due 11:59pm on 4/15/2017
• Grading:
– Design, input/output, format: 20%, Functionality (correctness): 20%, requirements: 60%
• Late penalty: 10 points off per week for the first week, additional 20 points off per week after the first
week.
Explanation / Answer
class Person {
private String name;
public Person() {
name = "unknown";
}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void printInfo() {
System.out.println("Person [name=" + name + "]");
}
}
class Employee extends Person {
private double salary;
public Employee() {
salary = -1;
}
public Employee(double salary) {
this.salary = salary;
}
public Employee(String name, double salary) {
super(name);
this.salary = salary;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public void printInfo() {
System.out.println("Employee [name = " + getName()+ ", salary = " + salary + "]");
}
}
class Faculty extends Employee {
private String dept;
public Faculty() {
dept = "none";
}
public Faculty(String dept) {
this.dept = dept;
}
public Faculty(String name, double salary, String dept) {
super(name, salary);
this.dept = dept;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
@Override
public void printInfo() {
System.out.println("Faculty [name = " + getName() + ", salary() =" + getSalary() + ", dept =" + dept + "]");
}
}
class Student extends Person {
private int age;
private String email;
public Student() {
age = -1;
email = "none";
}
public Student(String name, int age, String email) {
super(name);
this.age = age;
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public void printInfo() {
System.out.println("Student [name = " + getName() +", age = " + age + ", email = " + email + "]");
}
}
public class Program4 {
public static void main(String args[]) {
Student studentOne = new Student();
Student studentTwo = new Student("demo_s2", 23, "xyz.com");
Faculty facultyOne = new Faculty();
Faculty facultyTwo = new Faculty("demo_f2", 10000, "CS");
studentOne.printInfo();
studentTwo.printInfo();
facultyOne.printInfo();
facultyTwo.printInfo();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.