0% found this document useful (0 votes)
176 views13 pages

Inheritance Assignment

1. The document describes creating subclasses of an Employee class, including Manager, Engineer, and Administrative Assistant subclasses. It also describes creating a Director subclass of Manager. 2. The tasks involve applying encapsulation to the Employee class, creating the subclasses, and modifying a test class to create instances of each subclass and print their details. 3. Additional optional tasks include formatting output, adding validation, overriding toString methods, and creating a class to grant different numbers of stock options based on the employee type.

Uploaded by

mehmet göçer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
176 views13 pages

Inheritance Assignment

1. The document describes creating subclasses of an Employee class, including Manager, Engineer, and Administrative Assistant subclasses. It also describes creating a Director subclass of Manager. 2. The tasks involve applying encapsulation to the Employee class, creating the subclasses, and modifying a test class to create instances of each subclass and print their details. 3. Additional optional tasks include formatting output, adding validation, overriding toString methods, and creating a class to grant different numbers of stock options based on the employee type.

Uploaded by

mehmet göçer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

Practice 2-1: Creating Java Classes

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.

Practices for Lesson 2: Java Syntax and Class Review


Chapter 2 - Page 1
6. Write code in the EmployeeTest class to test your Employee class.
a. Construct an instance of Employee.
b. Use the setter methods to assign the following values to the instance:
Field Value
Employee id 101
Employee name Jane Smith
Employee Social Security Number 012-34-5678
Employee salary 120_345.27

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.

Practices for Lesson 2: Java Syntax and Class Review


Chapter 2 - Page 2
Practice 3-1: Creating Subclasses
Overview
In this practice, you will create subclasses of Employee, including Manager, Engineer, and
Administrative assistant (Admin). You will create a subclass of Manager called Director, and
create a test class with a main method to test your new classes.

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.

Practices for Lesson 3: Encapsulation and Subclassing


Chapter 3 - Page 3
2. Create a subclass of Employee called Manager in the same package.
a. Add a private String field to store the department name in a field called deptName.
b. Create a constructor that includes all the parameters needed for Employee and
deptName.
c. Add a getter method for deptName.
3. Create subclasses of Employee: Engineer and Admin in the com.example.domain
package. These do not need fields or methods at this time.
4. Create a subclass of Manager called Director in the com.example.domain package.
a. Add a private field to store a double value budget.
b. Create a constructor for Director that includes the parameters needed for Manager and
the budget parameter.
c. Create a getter method for this field.
5. Save all the classes.
6. Test your subclasses by modifying the EmployeeTest class. Have your code do the
following:
a. Remove the code that creates an instance of the “Jane Smith” Employee.
b. Create an instance of an Engineer with the following information:
Field Choices or Values
ID 101
Name Jane Smith
SSN 012-34-5678
Salary 120_345.27
c. Create an instance of a Manager with the following information:
Field Choices or Values
ID 207
Name Barbara Johnson
SSN 054-12-2367
Salary 109_501.36
Department US Marketing

d. Create an instance of an Admin with the following information:


Field Choices or Values
ID 304
Name Bill Munroe
SSN 108-23-6509
Salary 75_002.34

Practices for Lesson 3: Encapsulation and Subclassing


Chapter 3 - Page 4
e. Create an instance of a Director:
Field Choices or Values
ID 12
Name Susan Wheeler
SSN 099-45-2340
Salary 120_567.36
Department Global Marketing
Budget 1_000_000.00
f. Use the printEmployee method to print out information about each of your Employee
objects.
g. (Optional) Use the raiseSalary and setName methods on some of your objects to make
sure that those methods work.
h. Save the EmployeeTest class and test your work.
7. (Optional) Improve the look of the salary print output using the NumberFormat class.
a. In the printEmployee() method of Employee.java, use the following code to get
an instance of a static java.text.NumberFormat class that you can use to format
the salary to look like a standard US dollar currency:
NumberFormat.getCurrencyInstance().format((double)
getSalary()));
8. (Optional) Add additional business logic (data validation) to your Employee class.
a. Prevent a negative value for the raiseSalary method.
b. Prevent a null or empty value for the setName method.

Practices for Lesson 3: Encapsulation and Subclassing


Chapter 3 - Page 5
Practice 4-1: Overriding and Overloading Methods
Overview
In this practice, you will use a static method, override the toString method of the Object
class in the Employee class and in the Manager class. You will create an
EmployeeStockPlan class with a grantStock method that uses the instanceof operator
to determine how much stock to grant based on the employee type.

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.

Practices for Lesson 4: Overriding Methods and Applying Polymorphism


Chapter 4 - Page 6
4. Create a new class called EmployeeStockPlan in the package
com.example.business. This class will include a single method, grantStock, which
takes an Employee object as a parameter and returns an integer number of stock options
based on the employee type:
Employee Type Number of Stock Options
Director 1000
Manager 100
All other Employees 10
a. Add a grantStock method that takes an Employee object reference as a parameter
and returns an integer
b. In the method body, determine what employee type was passed in using the
instanceof keyword and return the appropriate number of stock options based on
that type.
c. Resolve any missing import statements.
d. Save the EmployeeStockPlan class.
5. Modify the EmployeeTest class:
a. Add a static printEmployee method that invokes the toString method of the Employee
class.
public static void printEmployee(Employee emp) {

System.out.println(emp);
}

b. Overload the printEmployee method to take a second parameter,


EmployeeStockPlan, and print out the number of stock options that this employee will
receive.
i. The new printEmployee method should call the first printEmployee method
and the number of stocks granted to this employee:
printEmployee (emp);
System.out.println("Stock Options: " + esp.grantStock(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);

Practices for Lesson 4: Overriding Methods and Applying Polymorphism


Chapter 4 - Page 7
e. Modify the code used to display the Managers stock plan after invoking the
raiseSalary method to
printEmployee(mgr, 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

Practices for Lesson 4: Overriding Methods and Applying Polymorphism


Chapter 4 - Page 8
Practice 6-1: Implementing an Interface
Overview
In this practice, you will create an interface and implement that interface.

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

Practices for Lesson 6: Interfaces


Chapter 6 - Page 9
1. Run the project. You should see text displayed in the output window.

2. Review the Animal and Spider classes.


a. Open the Animal.java file (under the com.example package).
b. Review the abstract Animal class. You will extend this class.
c. Open the Spider.java file (under the com.example package).
d. The Spider class is an example of extending the Animal class.
3. Create a new Java interface: Pet in the com.example package.
4. Code the Pet interface. This interface should include three method signatures:
▪ public String getName();
▪ public void setName(String name);
▪ public void play();
5. Create a new Java class: Fish in the com.example package.
6. Code the Fish class.
a. This class should:
▪ Extend the Animal class
▪ Implement the Pet interface
b. Complete this class by creating:
▪ A String field called name
▪ Getter and setter methods for the name field
▪ A no-argument constructor that passes a value of 0 to the parent constructor
▪ A play() method that prints out "Just keep swimming."
▪ An eat() method that prints out "Fish eat pond scum."
▪ A walk() method that overrides the Animal class walk method. It should first call
the super class walk method, and then print "Fish, of course, can't walk;
they swim."
7. Create a new Java class: Cat in the com.example package.
8. Code the Cat class.
a. This class should:
▪ Extend the Animal class
▪ Implement the Pet interface
b. Complete this class by creating:
▪ A String field called name
▪ Getter and setter methods for the name field
▪ A constructor that receives a name String and passes a value of 4 to the parent
constructor

Practices for Lesson 6: Interfaces


Chapter 6 - Page 10
▪ A no-argument constructor that passes a value of "Fluffy" to the other constructor
in this class
▪ A play() method that prints out name + " likes to play with string."
▪ An eat() method that prints out "Cats like to eat spiders and fish."
9. Modify the PetMain class.
a. Open the PetMain.java file (under the com.example package).
b. Review the main method. You should see the following lines of code:
Animal a;
//test a spider with a spider reference
Spider s = new Spider();
s.eat();
s.walk();
//test a spider with an animal reference
a = new Spider();
a.eat();
a.walk();
c. Add additional lines of code to test the Fish and Cat classes that you created.
▪ Try using every constructor.
▪ Experiment with using every reference type possible and determine which methods
can be called with each type of reference. Use a Pet reference while testing the
Fish and Cat classes.
d. Implement and test the playWithAnimal(Animal a) method.
▪ Determine whether the argument implements the Pet interface. If so, cast the
reference to a Pet and invoke the play method. If not, print a message of "Danger!
Wild Animal".
▪ Call the playWithAnimal(Animal a) method from within main, passing in each
type of animal.

Practices for Lesson 6: Interfaces


Chapter 6 - Page 11
10. Run the project. You should see text displayed in the output window.

Practices for Lesson 6: Interfaces


Chapter 6 - Page 12
.
Chapter 19 - Page 13

You might also like