CS 161 Lab 4
Overview
This lab will cover inheritance, polymorphism, and abstract classes.
Lab Demo
Please download the following files:
It makes sense to make Vehicle an abstract class. What changes in SignThenDriveEvent.java will you need to make after you have done that?
Lab Assignment
The lab assignment will involve relatively little code writing. The main goal of this lab is to experiment with inheritance and understand how inherited methods fit into your code.
Specifically, we are simulating the day to day work environment in a software development company,
and is aimed at modeling employees and software projects.
Assume the company has the following types of employees: programmers, and testers (we will ignore managers since they don't contribute to the completion of projects). This will be represented by the following Java class hierarchy:
Employee
/ \
Programmer Tester
Employee is an abstract class that represents the notion of an employee.
It has an abstract method called work(), that the classes Programmer and Tester will provide an implementation for. A Programmer writes code, and the Tester is focused on testing it.
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 and ID number. You will need to implement a constructor, and getter/setter methods.
Programmer
The Programmer
class is a sub-class of Employee
.
A given programmer can write a certain number of lines of code per day, captured by an instance variable linesOfCodePerDay
.
A programmer's work method returns the number of lines of code he/she wrote on a given day, which is a random number between 50% and 150% of linesOfCodePerDay
(some days are not as good as others, and some tasks are harder/easier than others).
Tester
The Tester
class is a sub-class of Employee
.
A given Tester can test a given number of lines of code per day, captured by an instance variable linesOfCodeTestedPerDay
.
A tester's work method returns the number of lines of code he/she tested that day, which is a random number between 75% and 125% of linesOfCodeTestedPerDay
.
Project
The class Project
models a software development project. It should have the following attributes:
-
linesOfCode
: An estimate of the number of lines of code the project will require (that's never known ahead of time, but let's just assume that).
-
linesOfCodeWritten
: the number of lines of code that have been written by the programmers working on it.
-
linesOfCodeTested
: the number of lines of code that have been tested so far.
-
duration
: how many days managers have given for completion of the project.
-
employees
: an ArrayList that stores the employees that are associated with the project.
The project is complete once all the code is written and tested.
The class should have the following methods:
- Constructor: should take in the number of lines of code for the project and required duration.
void addEmployee
: adds an employee to the project.
int doProject()
: do the project. Each day, as long as the project is not complete, it should call each employee's work
method.
It needs to return the number of days the project took beyond the alotted number of days. Optional: If a project is running behind (or you estimate that it is running behind), put more testers or programmers on the job.
String toString()
: Output info about each of the project's primitive instance variables, and call the toString()
method for each employee. Each piece of information should be on a separate line.
The main method of this class should create a new instance of Project
, add a few programmers and testers, run the doProject()
method, and report how long it took and whether it was completed on time. It should also print the status of the project before and after completion using its toString()
method.
Tasks
- Complete the
Employee
class: write the constructor and getter/setter methods.
- Write the
Programmer
and Tester
classes, which need to implement the work()
method. Make sure the constructor of these classes use the constructor of the superclass; write toString() methods that use the toString() method of Employee
- Write the
Project
class.
Question: where have we used the Java concept of polymorphism?