CS 161 Recitation 10(a)


This lab will cover inheritance, polymorphism, and instanceof.


Please download the following files:

These files demonstrate inheritance.


The lab assignment will involve a little code writing. The main goal of this lab is to experiment with inheritance and understand how inherited methods fit into your code.

This lab is built around a simple discrete event simulator. Specifically, we are simulating the day to day work environment of a software development company. Suppose this company has three types of employees: executives, software engineers, and software managers (i.e. the managers of the engineers). We can represent this organization structure with the following Java class hierarchy:

You will need the following starter files:

The Employee class

The Employee class contains functionality common to types of employees in the company. For example, all employees have a name, ID number, a managerID (the ID of their manger), and the capacity to do work. This work() method is only a stub and doesn't do much.

The backdrop code, CompanySimulator, is a simple discrete event simulator. At time t, the simulator runs through an ArrayList that represents the company and tells everyone to do work by invoking Employee.work().


The Engineer class inherits from Employee as shows in the vehicle example. Software engineers implement the work method in the following way: engineers happily program, but cause a crisis 10% of the time. For our purposes, "programming" is defined as a simple "I am programming" print statement that identifies the engineer (using toString). You can generate a crisis by returning false. Otherwise, return true. Use the following code snippet to determine if a crisis should be generated:

  Random crisisGen = new Random();
  if (crisisGen.nextInt(10) == 0) {
      //it's a crisis!
  } else {
      //everything is OK!
nextInt generates a random integer in the range [0, n-1]. In the above example, n=10. Crises are detected by the simulation code and triggers a call to CompanySimulator.manageCrisis(Employee emp) that you will implement. We will discuss the method further shortly.



The Manager is a specialization of Engineer. Because the manager inherits from the engineer class, it is also an Employee. Note that in this example, our organization hierachy (managers are in charge of engineers), is the opposite of the class hiearchy. This is because the manager can perform engineering tasks (programming) and distinct management tasks. The manager has the additional ability to handle a crisis, which will be modeled using the handleCrisis() method. Also, let's assume that a Manger can't cause a crisis.


Handling a Crisis

Recall that the work() method will return false if an employee has created a crisis. If a software engineer causes a crisis, the crisis must be dealt with by their respective manager. The manager who needs to handle the crisis is determined by an employee's managerID instance variable. Note: Only the work method may throw a Crisis (as noted in Employee).