7.3 The Salary Problem with Selection


7.3 The Salary Problem with Selection

This is an extension of the salary problem presented earlier. A company is to give salary increases to its employees. The salary increase is 4.5% of the salary—but only for those employees with a salary greater than $45,000.00; otherwise, the salary increase is 5%. The solution should calculate the salary increase for the employees and the updated salary.

7.3.1 Preliminary Design

To describe the transformation on the data, a selection structure is needed in the algorithm. Figure 7.3 shows the selection structure applied to this problem.

click to expand
Figure 7.3: Application of the selection structure.

The flow of control in the sequence of instructions takes alternate routes depending on the condition: salary greater than 45,000. The left path is followed when the condition is true (salary is greater than 45,000), the right path is followed when the condition is not true.

The first level of detail for the algorithm description in informal pseudocode is:

       get the salary for an employee       if salary is greater than $45,000.00       then                calculate increase with 4.5%       else                calculate increase with 5%       endif       update salary       show increase and updated salary for         this employee 

Note that the flowchart shown for the salary problem is not complete, the start and the end symbols are not shown. The flowchart is only slightly more complicated than the one for the previous version of the salary problem.

7.3.2 Final Design and Implementation

The following class definition implements part of the solution for the salary problem. The most relevant part of the solution for this problem is found in function sal_increase of class Employee_m.

       description         This program computes the salary increase for an         employee. If his/her salary is greater than $45,000,         the salary increase is 4.5%; otherwise, the salary         increase is 5%. This is the class for employees.         The main attributes are salary, age, and name.         */       class Employee_m is         private         variables           // variable data declarations             real salary             integer age             string obj_name             real increase  // salary increase         public           //         description           This is the constructor, it initializes an object           on creation.   */         function initializer parameters string iname,                           real isalary, integer iage is         begin           set salary = isalary           set age = iage           set obj_name = iname         endfun initializer         //         description           This function gets the salary of the employee           object.  */         function get_salary of type real is           begin             return salary         endfun get_salary         //         description           This function returns the name of the           employee object.   */         function get_name of type string is           begin             return obj_name         endfun get_name         //         description            This function changes the salary of the            Employee object by adding the change to            current salary.            */         function change_sal parameters real change is         begin            add change to salary         endfun change_sal         //         description             This function computes the salary increase             and updates the salary of an employee.             */         function sal_increase is           constants             real percent1 = 0.045 // percent             real percent2 = 0.05           begin                   // body starts here             if salary > 45000 then                set increase = salary * percent1             else                set increase = salary * percent2             endif             add increase to salary    // update salary         endfun sal_increase         //         description            This function returns the salary increase            for the object.  */         function get_increase of type real is           begin            return increase         endfun get_increase       endclass Employee_m 

On the CD

The code of the KJP implementation for this class is stored in the file Employee_m.kpl.

The following class, Comp_salary_m includes the function main, which creates and manipulates objects of class Employee_m.

       description         This program computes the salary increase for         an employee. If his/her salary is greater than         $45,000, the salary increase is 4.5%; otherwise,         the salary increase is 5%. This class creates         and manipulates the objects of class         Employee_m.  */       class Comp_salary_m is         public         description             This is the main function of the appli-             cation.      */         function main is           variables             real increase             real salary             integer age             string oname           objects             object emp_obj of class Employee_m           begin             display "Enter salary: "             read salary             display "Enter age: "             read age             display "Enter name: "             read oname             create emp_obj of class Employee_m using                     salary, age, oname             set increase = call sal_increase of emp_obj             // get updated salary             set salary = get_salary() of emp_obj             print "Employee name: ", oname             print "increase: ", increase,                   " new salary: ", salary          endfun main       endclass Comp_salary_m 

On the CD

The code for the KJP implementation is stored in the file Comp_salary_m.kpl.

The following is the code for the Java implementation of class Employee_m.

       // KJP v 1.1 File: Employee_m.java, Thu Dec 12                          20:09:35 2002       /**         This program computes the salary increase for         an employee. If his/her salary is greater than         $45,000, the salary increase is 4.5%; otherwise,         the salary increase is 5%. This is the class         for employees. The main attributes are salary,         age, and name.         */       public  class Employee_m  {         // variable data declarations         private float  salary;         private int  age;         private String  obj_name;         private // salary increase         float  increase;         /**         This is the initializer function (constructor),         it initializes an object on creation.         */       public Employee_m(String  iname, float  isalary,                 int  iage) {         salary =  isalary;         age =  iage;         obj_name =  iname;       } // end constructor       /**         This function gets the salary of the employee         object.   */       public float  get_salary() {         return salary;       }  // end get_salary       /**         This function returns the name of the employee         object.         */       public String  get_name() {         return obj_name;       }  // end get_name       /**           This function computes the salary increase           and updates the salary of an employee.           */       public void  sal_increase() {         // constant data declarations         final float  percent1 = 0.045F;   // increase         final float  percent2 = 0.05F;         // body of function starts here         if ( salary > 45000) {            increase =  (salary) * (percent1);         }         else {            increase =  (salary) * (percent2);         } // endif           salary += increase;    // update salary         }  // end sal_increase         /**            This function returns the salary increase for            the object.   */         public float  get_increase() {            return increase;         }  // end get_increase       }  // end Employee_m 

On the CD

The implementation of class Employee_m is stored in the file Employee_m.java.

The output of the execution of this program is shown in Figure 7.4. This program uses class Comp_salary_m as the main class that includes the two classes. Two runs are shown, the first with a salary less than $45,000.00, and the second with a salary higher than $45,000.00.

click to expand
Figure 7.4: Execution of class Comp_salary_m for the salary problem.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net