Inheritance Assignment
Inheritance Assignment
Overview
In this practice, using the Eclipse IDE, you will create an Employee class, create a class witha
main method to test the Employee class, compile and run your application, and print the
results to the command line output.
Tasks
1. Start the Eclipse IDE by using the icon from Desktop.
2. Create a new project Employee with an EmployeeTest main class in the com.example package.
3. Create another package called com.example.domain.
4. Add a Java Class called Employee in the com.example.domain package.
5. Code the Employee class.
a. Add the following data fields to the Employee class—use your judgment as to what
you want to call these fields in the class. Refer to the lesson materials for ideas on the
field names and the syntax if you are not sure. Use public as the access modifier.
Field use Recommended field type
Employee id int
Employee name String
Employee Social Security Number String
Employee salary double
b.Create a no-arg constructor for the Employee class.
c. Add accessor/mutator methods for each of the fields.
c. In the body of the main method, use the System.out.println method to write the
value of the employee fields to the console output.
d. Resolve any missing import statements.
e. Save the EmployeeTest class.
7. Run the Employee project.
8. (Optional) Add some additional employee instances to your test class.
Assumptions
Use this Java class diagram to help guide this practice.
Tasks
1. Apply encapsulation to the Employee class.
a. Make the fields of the Employee class private.
b. Replace the no-arg constructor in Employee with a constructor that takes empId,
name, ssn, and salary.
c. Remove all the setter methods except setName.
d. Add a method named raiseSalary with a parameter of type double called
increase to increment the salary.
e. Add a method named printEmployee to print the Employee object details.
f. Save Employee.java.
Assumptions
Tasks
1. Edit the Employee class:
a. Delete the instance method printEmployee().
b. Override the toString() method from the Object class. Object’s toString method
returns a String.
I. Add a return statement that returns a string that includes the employee ID,
name, Social Security number, and a salary as a formatted string, with each line
separated with a newline character ("\n").
II. To format the double salary, use the following:
i. NumberFormat.getCurrencyInstance().format(getSalary())
III. Fix any missing import statements.
IV. Save the class.
2. Override the toString() method in the Manager class to include the deptName field
value. Separate this string from the Employee string with a newline character.
3. (Optional) Override the toString() method in the Director class as well, to display all
the fields of a Director and the available budget.
System.out.println(emp);
}
c. Above the printEmployee method calls in the main method, create an instance of
the EmployeeStockPlan and pass that instance to each of the printEmployee
methods:
EmployeeStockPlan esp = new EmployeeStockPlan();
printEmployee(eng, esp);
d. Modify the remaining printEmployee invocations.
printEmployee(adm, esp);
printEmployee(mgr, esp);
printEmployee(dir, esp);
6. Save the EmployeeTest class and run the application. You should see output for each
employee that includes the number of Stock Options, such as:
Employee id: 101
Employee name: Jane Smith
Employee SSN: 012-34-5678
Employee salary: $120,345.27
Stock Options: 10
7. It would be nice to know what type of employee each employee is. Add the following to your
original printEmployee method above the print statement that prints the employee data
fields:
System.out.println("Employee type: " +
emp.getClass().getSimpleName());
This will print out the simple name of the class (Manager, Engineer, and so on). The
output of the first employee record should now look like this:
Employee type: Engineer
Employee id: 101
Employee name: Jane Smith
Employee SSN: 012-34-5678
Employee salary: $120,345.27
Stock Options: 10
Assumptions
You have reviewed the interface section of this lesson.
Summary
You have been given a project that contains an abstract class named Animal. You create a
hierarchy of animals that is rooted in the Animal class. Several of the animal classes
implement an interface named Pet, which you will create.
Tasks