Two simple examples are presented in this section. The first one consists of two classes and computes the salary increase of employees. The second example has only one class with function main to compute the area of a triangle. This class calls a function of the library class, Math. Because this is a static function, the name of the class is used instead of an object reference.
The problem must compute a 4.5% salary increase for employees, and update their salary. The solution to this problem must first declare the necessary variables: salary of type real, and increase of type real.
The algorithm describes the following sequence of operations for an employee, in an informal pseudo-code notation.
Get the value of the employee's salary.
Compute the 4.5% salary increase of the current salary.
Add the value of this salary increase to the current salary to get the new (updated) salary.
Print the values of the salary increase and the updated salary.
The corresponding flowchart is shown in Figure 6.9. Because this example is very simple, it needs only two of the design structures, sequence and input/output. Note that the pseudo-code is much more compact than the flowchart in Figure 6.9; however, both are equally complete.
Figure 6.9: Flowchart that shows the transformations for the simple salary problem.
The solution is decomposed into two classes, Employee and Memployee. Class Employee defines the complete algorithm in one or more of its operations. Class Memployee contains function main, which declares and creates an object of class Employee and invokes operation compute_increase of that object.
The data description for the attributes of class Employee consists of two variables, salary and name. These attributes have access mode private, which is always enforced in KJP.
The algorithm is implemented in function compute_increase. This function computes the salary increase and updates the salary of the object.
On the CD | The complete KJP code for class Employee follows and is stored in the file Employee.kpl. |
description This program computes the salary increase for an employee at the rate of 4.5%. This is the class for employees. The main attributes are salary and name. */ class Employee is private variables // variable data declarations real salary string name public // description This is the constructor, it initializes an object on creation. */ function initializer parameters real isalary, string iname is begin set salary = isalary set 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 name endfun get_name // description This function computes the salary increase and updates the salary of an employee. It returns the increase. */ function compute_increase of type real is constants real percent = 0.045 // % increase variables real increase begin // body of function set increase = salary * percent add increase to salary // update salary return increase endfun compute_increase endclass Employee
Class Memployee contains function main. The name and salary of the employee are read from the console, and then an object of class Employee is created with these initial values for the attributes. Function main invokes function compute_increase of the object.
On the CD | The complete KJP implementation of class Memployee follows. The code for this class is stored in the file Memployee.kpl. |
description This program computes the salary increase for an employee. This class creates and manipulates the employee objects. */ class Memployee is public description This is the main function of the application. */ function main is variables real increase real obj_salary string obj_name objects object emp_obj of class Employee begin display "Enter salary: " read obj_salary display "Enter name: " read obj_name create emp_obj of class Employee using obj_salary, obj_name set increase = call compute_increase of emp_obj set obj_salary = get_salary() of emp_obj display "Employee name: ", obj_name display "increase: ", increase, " new salary: ", obj_salary endfun main endclass Memployee
On the CD | The Java code generated by the KJP translator for class Employee follows and is stored in the file Employee.java. |
// KJP v 1.1 File: Employee.java, Fri Dec 06 11:22:18 2002 /** This program computes the salary increase for an employee to be 4.5%. This is the class for employees. The main attributes are salary and name. */ public class Employee { // variable data declarations private float salary; private String name; /** This is the constructor, it initializes an object on creation. */ public Employee(float isalary, String iname) { salary = isalary; 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 name; } // end get_name /** This function computes the salary increase and updates the salary of an employee. It returns the increase. */ public float compute_increase() { // constant data declarations final float percent = 0.045F; // increase float increase; // body of function starts here increase = (salary) * (percent); salary += increase; // update salary return increase; } // end compute_increase } // end Employee
Figure 6.10 shows the results of executing the salary program for the indicated input data.
Figure 6.10: Results on the console for salary program.
As stated previously, this problem has the following description: Given the three sides of a triangle, calculate its area. The algorithm description in informal pseudo-code notation is:
Read the value of side x from the input device (keyboard).
Read the value of side y from the input device (keyboard).
Read the value of side z from the input device (keyboard).
Compute s = 0.5(x + y + z), as an intermediate result.
Compute area = .
Print the value of area to the output device (video screen).
The data description and the algorithm for calculating the area of a triangle in KJP notation is included in function main:
function main is variables // data descriptions real x // first side of triangle real y // second side real z // third side real s // intermediate result double area // begin // instructions starts here display "enter value of first side: " read x display "enter value of second side: " read y display "enter value of third side: " read z set s = 0.5 * (x + y + z) set area = Math.sqrt(s * (s - x)*(s - y)*(s - z)) print "Area of triangle is: ", area endfun main