0% found this document useful (0 votes)
4 views4 pages

Unit 9 Summative Review I

The document outlines a programming assignment focused on inheritance in Java, specifically creating an Employee class that extends a Person class. It includes requirements for constructors, methods, and an inheritance hierarchy chart. Additionally, it provides code examples for methods like toString() and equals(), as well as a main method to demonstrate polymorphism with an array of Person objects.

Uploaded by

dcoolcal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Unit 9 Summative Review I

The document outlines a programming assignment focused on inheritance in Java, specifically creating an Employee class that extends a Person class. It includes requirements for constructors, methods, and an inheritance hierarchy chart. Additionally, it provides code examples for methods like toString() and equals(), as well as a main method to demonstrate polymorphism with an array of Person objects.

Uploaded by

dcoolcal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit 9: Inheritance​ ​ ​ ​ ​ ​ ​ ​ Name: ______________________

Unit 9 Summative Review I

Use the following class to answer the questions that follow.

public class Person


{
private int myAge;

public Person(){
myAge = 0;
}
public Person(int x){
myAge = x;
}

public void setAge(int age){


myAge = age;
}

public int getAge(){


return myAge;
}

public void haveBirthday(){


myAge++;
}

public void saySomething(){


System.out.println("I am a person");
}
}

1) Write an Employee class that is-a Person.

Employee should have the following:


●​ two instance variables: salary and vacationDays
●​ A default constructor with an explicit call to the Person’s default constructor.
●​ A two-parameter constructor with an explicit call to Person’s default constructor.
●​ A three-parameter constructor with an explicit call to Person’s parameter constructor.
●​ A polymorphic method called saySomething()
public class Employee extends Person {
private double salary;
private int vacationDays;

public Employee() {
super();
salary = 0.0;
vacationDays = 0;
}

public Employee(double salary, int vacationDays) {


super();
this.salary = salary;
this.vacationDays = vacationDays;
}

public Employee(int age, double salary, int vacationDays) {


super(age);
this.salary = salary;
this.vacationDays = vacationDays;
}

public void saySomething() {


System.out.println("I am an employee");
}
}

2) Draw an inheritance hierarchy chart for Person, Student, Employee, and APStudent in the space to
the left. Then, circle Legal or Illegal next to each line of code, identifying if the upcasting or aliasing is allowed.

Person Person z = new Employee(); Legal / Illegal


/ \
Student Employee APStudent y = new Employee(); Legal / Illegal
|
Student x = new Employee(); Legal / Illegal
APStudent
Student w = new APStudent(); Legal / Illegal
Person v = new Student();
Person u = new Employee();
v = u; Legal / Illegal
Person t = new Person();
Employee s = new Employee();
t = s; Legal / Illegal
Person r = new Student();
Student q = new Student();
q = r; Legal / Illegal

3) Write the toString() and equals() method for an Employee. Format the String nicely, but it can be
done however you would like.

public class Employee extends Person {


private double salary;
private int vacationDays;

public String toString() {


return "Employee: Age = " + getAge() + ", Salary = " + salary + ", Vacation Days = " + vacationDays;
}

public boolean equals(Object obj) {


if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Employee other = (Employee) obj;
return this.getAge() == other.getAge() &&
this.salary == other.salary &&
this.vacationDays == other.vacationDays;
}
}
4) Consider the following code:

Person A = new Person();


Person B = new Employee();
Person C = new Student();
Person D = new APStudent();

Write a code segment that holds all 4 people in a single array, and then uses a loop to print off the
saySomething method for each of the people.

public class Main {


public static void main(String[] args) {
Person[] people = {
new Person(),
new Employee(),
new Student(),
new APStudent()
};

for (Person person : people) {


person.saySomething();
}
}
}

You might also like