2

Chapter 2 Classes and Objects 4

Introduction 4

Lab Exercise 15

Programming Exercise 1 15

Programming Exercise 2 17

Post Lab Exercise 19

Assignment Exercise 19

Chapter 2 Classes and Objects

Introduction

These lab exercises are geared to help the student to begin real programming at an early stage. Students should attempt these exercises only after they have read the chapter, and at least do the end of section exercises. I suggest that the reader do the lab before they attempt a major programming assignment.

In doing the lab, do not skip any of the exercises, since they are arranged with incremented difficulty. The pre-lab exercises may be done prior to the closed lab session. All the exercises however, must be submitted to your proctor for grading.

Objective

·  To re-enforce the information gathered from the lesson.

·  To be knowledgeable with the fundamental construct of a Java class.

·  To know the basic syntax of Java.

·  To view a Java program as a whole/complete thought rather than by parts( method per method).

·  To understand how objects interact.

·  To be able to use the print() and println() methods to format output effectively.

Lab Exercise

Programming Exercise 1

The ABC Furniture Company wants a computerized payroll system. The program will print weekly payroll report for each of its customers. Each employee is subject to the following deductions: Federal with holding tax rate 15% of the gross pay; state withholding tax, 3.5% of the gross pay; and $5.50 flat amount for union dues.

The input for each customer is the name, the number of hours worked, and the employee’s hourly rate.

(a) Use Figure 1 to define the class Payroll.

Figure 1

After you have coded the class make sure that it compiles before attempting Part (b), the test class.

(b) Complete the class TestPayroll given below. Lines 7, 8, 24, 25 and 26 have been removed. You are required to supply the statements necessary to produce output similar to Figure 2.

Figure 2

Programming Exercise 2

The ABC Company wants a summary report in addition to the payroll report. The output should be similar to Figure 3.

Figure 3

Figure 3 shows the report for two individuals – James Lyons and Mary Harris; and the summary report for the week.

To fulfill this requirement do the following:

1.  Make a new folder/directory and copy both files from Programming Exercise 2 – Payroll and TestPayroll into this new folder.

2.  Extend Figure 1 to include the following variables:

·  total_FedTax

·  total_stateTax

·  total_dues

·  total_deduction

·  total_netPay

·  gross

These variables must be used to accumulate the various amounts – gross and deductions. By now you should realize that these variables must be class variables (static variables).

3.  Modify the calculate method to do the accumulations. For example, to sum the total federal tax, you would write:

total_FedTax = total_FedTax + fedTax;

4.  Provide accessor methods to return these values. Remember that these methods must also be class methods (static methods). For example, to return the total federal tax you would write:

static double getTotalFedTax()

{

return total_FedTax;

}

5.  Lastly, add the following method to the TestPayroll class that will produce the summary report.

6.  Remember to call this method for the main method. It must be the last statement in the method.

Post Lab Exercise

Assignment Exercise

Post lab exercise should take the form of assignment. I suggest that your instructor use the program projects found at the end of Chapter 2.