CSE 271 Spring 2017 Thursday Labs Due Monday 11:59pm Friday Labs Due Tuesday 11:
ID: 3799745 • Letter: C
Question
CSE 271 Spring 2017 Thursday Labs Due Monday 11:59pm Friday Labs Due Tuesday 11:59pm Lab 05- 40 points For today's lab you will be implementing two of your own inheritance hierarchies. It is a smaller/simpler lab, with an earlier deadline. The expectation being that you will also use this time to start your Project #1. Part 1 (20 Points) Implement a superclass Person. Make two classes, student and Instructor that inherit from Person. A person has a name and a year of birth (2 points) A student has a major (2 points) An instructor has a salary (2 points) Write The class declarations (1 point each x 3 3 points) The appropriate constructors (2 points each for sub x 1, 1 point for super 5 points) o Make one for each class that initializes all the data and utilizes the "Super constructor appropriately. That is, the student and instructor constructors will call the super constructor with the appropriate data to initialize the person's instance variables. For example, the student and instructor constructors will need the information necessary parameters) that includes person information The (appropriate and overridden) tostring methods for all classes. (3 points) o For each class' implementation of tostring, use (extend) behavior from each super class' toString's method in order to avoid duplicate code (Negative points for duplicate code)! Getters and Setters (Negative points if done wrong) Supply a single test program that tests these classes and methods, including the constructors. (2 points)Explanation / Answer
Part1
package part1;
public class Person {
private String name;
private int yearOfBirth;
public Person(String name, int yearOfBirth) {
super();
this.name = name;
this.yearOfBirth = yearOfBirth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYearOfBirth() {
return yearOfBirth;
}
public void setYearOfBirth(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
@Override
public String toString() {
return "I am " + name + ", was born in " + yearOfBirth + ".";
}
}
--------------------------------------------
package part1;
public class Student extends Person {
private int major;
public Student(String name, int yearOfBirth, int major) {
super(name, yearOfBirth);
this.setMajor(major);
}
public int getMajor() {
return major;
}
public void setMajor(int major) {
this.major = major;
}
@Override
public String toString() {
return super.toString()+"I am a Student majoring "+major+".";
}
}
----------------------------------------------------
package part1;
public class Instructor extends Person {
private double salary;
public Instructor(String name, int yearOfBirth, double salary) {
super(name, yearOfBirth);
this.setSalary(salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return super.toString()+"I am working as an Instructor for salary=" + salary + ".";
}
}
--------------------------------------------------------
package part1;
public class Test {
public static void main(String args[]) {
Student student1=new Student("AAA", 1994, 9);
Instructor inst1=new Instructor("XYZ", 1985, 1000);
System.out.println(student1);
System.out.println(inst1);
}
}
---------------------------------------------------------------------
Part2
----------------------------------------------------------------------
package part2;
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "I am " + name + " and my salary is" + salary + ".";
}
}
--------------------------------------------------------------
package part2;
public class Manager extends Employee {
private String department;
public Manager(String name, double salary, String department) {
super(name, salary);
this.department = department;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return super.toString()+" department " + department + ".";
}
}
--------------------------------------------------------------
package part2;
public class Executive extends Manager{
private String offficelocation;
public String getOffficelocation() {
return offficelocation;
}
public void setOffficelocation(String offficelocation) {
this.offficelocation = offficelocation;
}
public Executive(String name, double salary, String department, String offficelocation) {
super(name, salary, department);
this.offficelocation = offficelocation;
}
@Override
public String toString() {
return super.toString()+"Executive offficelocation=" + offficelocation + ".";
}
}
------------------------------------------------------------------------
package part2;
public class Test {
public static void main(String args[]) {
Manager mangr=new Manager("AAA", 2000, "sales");
Executive exe=new Executive("XYZ", 5000, "admin", "SYD");
System.out.println(mangr);
System.out.println(exe);
}
}
---------------------------------------------------------------------