8.3 The Salary Problem with Repetition


8.3 The Salary Problem with Repetition

This version of the salary problem includes repetition so that the same calculations for salary increase are applied to a given number of employees of a company. Assume that the sequence of instructions for calculating the salary increase is the same as the salary problem already discussed. These operations are grouped in Block1 and are to be repeated for every employee.

In designing the algorithm for this problem, the sequence of instructions in Block1 is placed in a loop. The while statement contains the condition that compares the loop counter with the given number of employees.

To simplify the problem, assume that the algorithm first reads a variable from the input list. This variable is an integer that represents the number of employees to compute the salary increase and update their salaries. The name of this variable is Num_emp. The value of this variable determines for how many employees to calculate the salary increase, and updates the salary. Figure 8.2 shows the portion of the flowchart for the algorithm for the salary problem with repetition.


Figure 8.2: Salary problem with repetition.

Variable Num_emp denotes how many times to repeat the block of instructions in Block1. For example, if the value of Num_emp is 10, this means that there are 10 employees, and the block of instructions will be repeated 10 times, once for each employee.

The condition for the repetition (loop) is: counter less or equal to Num_emp. The last instruction in the loop increments the value of variable counter.

The flowchart in Figure 8.2 shows only the repeat structure of the algorithm and Block1 contain all the instructions that are repeated. This is a simple way to modularize a program, in other words, group instructions into small modules. This also helps reduce the size of the flowchart.

The corresponding code in KJP for the algorithm is:

       integer Num_emp    // number of employees to process       integer counter       . . .       read Num_emp       // read number of employees       set counter = 1       while counter <= Num_emp do          // instructions in Block1          increment counter       endwhile       . . . 

A complete implementation of the algorithm for the salary problem is implemented in classes Employee_m and Salarym. The operations for calculating the salary increase and updating the salary for every employee are implemented in class Employee_m, which is the same as discussed previously.

On the CD

The code for the KJP implementation of class Salarym follows; it is stored in the file Salarym.kpl.

       description         This program computes the salary increase for a         number of employees. If the employee's salary is         greater than $45,000, the salary increase is 4.5%;         otherwise, the salary increase is 5%. The program         also calculates the number of employees with a         salary greater than $45,000 and the total amount         of salary increase.   */       class Salarym is         public           //         description         This function creates objects to compute the         salary increase and update the salary. This         calculates the number of employees with salary         greater than $45,000 and the total salary         increase.         */      function main is        variables          integer num_emp   // number of employees          integer loop_counter          real salary          string name          integer age          real increase          real total_increase = 0.0          integer num_high_sal = 0  // number employees                      // with salary greater than 45000        objects          object emp_obj of class Employee_m        begin                       // body of function         display "enter number employees to process: "         read num_emp         set loop_counter = 1          // initial value         while loop_counter <= num_emp do            display "enter employee name: "            read name            display "enter salary: "            read salary            display "enter age: "            read age            create emp_obj of class Employee_m using                    name, salary, age            if salary > 45000 then               increment num_high_sal            endif            call sal_increase of emp_obj            set increase = call get_increase of emp_obj               // updated salary               set salary = call get_salary of emp_obj               display "Employee: ", name, " increase: ",                      increase, " salary: ", salary               increment loop_counter               add increase to total_increase // accumulate            endwhile            print "Number employees with salary > 45000: ",                      num_high_sal            print "Total amount of salary increase: ",                      total_increase         endfun main       endclass Salarym 

On the CD

The code for Java implementation of class Salarym follows and is stored in the file Salarym.java. This file was generated by the KJP translator.

       // KJP v 1.1 File: Salarym.java, Fri Dec 06                 11:28:28 2002       /**         This program computes the salary increase for a         number of employees. If the employee's salary         is greater than $45,000, the salary increase is         4.5%; otherwise, the salary increase is 5%. The         program also calculates the number of employees         with a salary greater than $45,000 and the         total amount of salary increase.      */       public  class Salarym {       /**            This function creates objects to compute the            salary increase and update the salary. This            calculates the number of employees with            salary greater than $45,000 and the total            salary increase.   */        public static void main(String[] args) {           int  num_emp;  // number of employees to process           int  loop_counter;           float  salary;           String  name;           int  age;           float  increase;           float  total_increase = 0.0F;  // accumulator           int  num_high_sal = 0; // number employees with                              // salary greater than 45000           Employee_m emp_obj;           // body of function starts here           System.out.println(                 "enter number of employees to process: ");           num_emp = Conio.input_Int();           loop_counter =  1;      // initial value           while ( loop_counter <= num_emp ) {               System.out.println("enter employee name: ");               name = Conio.input_String();               System.out.println("enter salary: ");               salary = Conio.input_Float();               System.out.println("enter age: ");               age = Conio.input_Int();               emp_obj = new Employee_m(name, salary, age);               if ( salary > 45000) {                   num_high_sal++;               } // endif               emp_obj.sal_increase();               increase =  emp_obj.get_increase();               // get updated salary               salary =  emp_obj.get_salary();               System.out.println("Employee: "+ name+                " increase: "+increase+" salary: "+ salary);               loop_counter++;               total_increase += increase;    // accumulate           } // endwhile           System.out.println(                "Number of employees with salary > 45000: "+                    num_high_sal);           System.out.println(                    "Total amount of salary increase: "+                    total_increase);        }  // end main       }  // end Salarym 




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