Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


136


gives 5.16 as its result. Here is the algorithmic solution:

The method to return the name just calls the Namemethod that provides the name in
the necessary format.

Because the pay was computed and stored when the employee object was instanti-
ated, we return the field value.

The total is updated by each instantiation, so again we can just return the field value.

That’s all there is to the employee class responsibility algorithms. As you can see,
most of the methods are trivial in their design. This simplicity is a common characteris-
tic of object-oriented problem solutions. To hide the implementation details in a
consistent manner, we turn accesses to internal fields into value-returning methods.
Software engineers refer to such methods as observers, and we will have more to say
about their role in a later chapter. Now, let’s look at the code for the Employeeclass,
which we store in a file called Employee.java.

totalPay value-returning class method
returntotal

pay value-returning instance method
returnwages

name value-returning instance method
returnmyName in last, first, MI format

Employee Constructor
Parameters: first, last, middle names, all String; payrate, double
Declare a BufferedReader in
Create a new Name, myName, using first, last, middle
Copy payrate parameter into rate field
Instantiate in using System.in
Prompt for hours worked, using myName firstLast format
Get hours worked from in, and convert to double, storing in hours field
Compute wages and round to cents
Add wages to total

Class Fields
Name myName
double rate
double hours
double wages
static double total
Free download pdf