Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Design and declaration. All classes’ data fields should be private. (40%) a.

ID: 3801400 • Letter: 1

Question

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 school ID. (10%)

A Student object name: unknown, age: -1, email: none

A Student object name: “demo_s2”, age: 23, email: xyz @ 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@certain email

Faculty name: demo_f1, salary: -1, dept: none

Faculty name: demo_f2, salary: 10000, dept: CS

• This should be coded in ONE java file.

Explanation / Answer

Here is the code for above scenario:

package demo;
class Person{
   private String name;

   public Person() {
       name = "unknown";
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }
   public void printInfo(){
       System.out.println("Name:"+name);
   }
}
class Student extends Person{
   private int age;
   private String emailAddress;
   public Student() {
       age = 0;
       emailAddress = "none";
   }
   public int getAge() {
       return age;
   }
   public void setAge(int age) {
       this.age = age;
   }
   public String getEmailAddress() {
       return emailAddress;
   }
   public void setEmailAddress(String emailAddress) {
       this.emailAddress = emailAddress;
   }
  
   public void printInfo(){
       System.out.println("Student Name:"+super.getName()+" ,Age:"+age+",Email Address:"+emailAddress);
   }
  
  
}
class Employee extends Person{
   private int salary;

   public Employee() {
       salary = 0;
   }

   public int getSalary() {
       return salary;
   }

   public void setSalary(int salary) {
       this.salary = salary;
   }
   public void printInfo(){
       System.out.println("Name:"+super.getName()+" Salary:"+salary);
   }
}
class Faculty extends Employee{
   private String dept;

   public Faculty() {
       dept = "none";
   }

   public String getDept() {
       return dept;
   }

   public void setDept(String dept) {
       this.dept = dept;
   }
   public void printInfo(){
       System.out.println("Faculty Name:"+super.getName()+",Salary:"+super.getSalary()+", Dept:"+dept);
   }
  
  
}
public class Test {
public static void main(String[] args) {
   Student s1 = new Student();
   s1.setName("unknown");
   s1.setAge(-1);
   s1.setEmailAddress("none");
   s1.printInfo();
  
   Student s2 = new Student();
   s2.setName("demo_s2");
   s2.setAge(23);
   s2.setEmailAddress("xyz @ email");
   s2.printInfo();
  
   Faculty f1 = new Faculty();
   f1.setName("demo_f1");
   f1.setSalary(-1);
   f1.setDept("none");
   f1.printInfo();
  
   Faculty f2 = new Faculty();
   f2.setName("demo_f2");
   f2.setSalary(10000);
   f2.setDept("CS");
   f2.printInfo();
  
}
}