Object interaction was explained in Chapter 2. When one object sends a message to another object, the first object invokes or calls a function of the second object. In the class definition of the second object, this function must have been defined as public; otherwise, the function is not accessible to other objects.
Function main is special; execution of the entire application starts in this function, and terminates here. In a typical program (or application), function main carries out the following general sequence of tasks:
Declares constants, simple variables, and object variables, as necessary
Creates one or more objects of the previously defined classes
Invokes one or more methods of the various objects. This delegates the tasks and subtasks to the objects for carrying the complete solution of the application.
Consider a definition of class Ball, similar to the one presented in Chapter 3. The attributes of the class are:
size of type real; represents the diameter of the object in inches
color of type character (not string); the possible values are 'B' for blue, 'R' for red, 'Y' for yellow, 'W' for white, and 'O' for orange
move_status of type character; represents the state of the ball object. 'M' is the value of this attribute if the ball is moving. 'S' is the value of this attribute if the ball object is not moving and/or has been stopped.
The methods (functions) of this class are:
move, which starts movement of the ball object
stop, which stops the movement of the ball object
show_status, which displays the values of the attributes color and move_status
get_color, which reads the value of attribute color from the console
get_size, which reads the value of attribute size from the console
The program has two classes: Ball and Mball. The second class (Mball) is the class with function main. This function is defined to carry out the following sequence of activities:
Declare two object variables, obj_1 and obj_2, of class Ball.
Create the two objects.
Invoke functions get_color and get_size for each object.
Invoke function show_status for each object.
Invoke functions move and then show_status for each object.
Terminate execution of the entire program.
On the CD | The complete definition of class Ball in KJP follows and is stored in the file Ball.kpl on the CD-ROM that accompanies this book. |
description This is a simple class. The attributes are color, size, and move_status. Nov 2002, J Garrido */ class Ball is private // attributes variables character color character move_status real size public // public methods description This method reads the value of attribute color from the console */ function get_color is begin display "type single-character color value " read color endfun get_color description This method reads the value of attribute color from the console. */ function get_size is begin display "type value of size " read size endfun get_size description This function displays the color, status, and size of the object. */ function show_status is begin display "Color of ball object: ", color display "Size of ball object: ", size display "Status of ball object: ", move_status endfun show_status description This function changes the move_status of the object. */ function move is begin set move_status = 'M' endfun move description This function changes the move_status of the object */ function stop is begin set move_status = 'S' endfun stop endclass Ball
On the CD | The implementation in KJP of class Mball is stored in the file Mball.kpl. The two Java classes produced by the KJP translator are stored in the files Ball.java and Mball.java. The complete definition of class Mball follows. |
description This program illustrates the general structure of a KJP program. It creates two objects of class Ball, and invokes some of their methods. */ class Mball is public description This function controls the program. */ function main is objects object obj_1 of class Ball object obj_2 of class Ball begin display "Creating object 1" create obj_1 of class Ball display "Creating object 2" create obj_2 of class Ball display "Invoking methods of object 1" call get_color of obj_1 call get_size of obj_1 display "Invoking methods of object 2" call get_color of obj_2 call get_size of obj_2 // to starting moving the ball objects call move of obj_1 call move of obj_2 call show_status of obj_1 call show_status of obj_2 // now stop moving the objects call stop of obj_1 call stop of obj_2 call show_status of obj_1 call show_status of obj_2 endfun main endclass Mball
The details for some of the KJP statements used in this example are explained in Chapters 5 and 6. Appendix A contains detailed explanations on compiling and executing KJP and Java programs with jGRASP. Figure 4.5 shows the main jGRASP screen with class Mball.
Figure 4.5: jGRASP with class Mball on the main window.
Figures 4.6 and 4.7 show the console input and output produced during execution of the program.
Figure 4.6: Console input data.
Figure 4.7: Console output data.
This section presents a complete and extremely simple KJP program with only one class and only one function. This is an extreme case of a simple program. No objects are necessary in this program because the entire task is carried out in function main.
Because this program does not involve objects, it is not a real object-oriented program. The program consists of a single-class called Salary1.
On the CD | The KJP code for class Salary1 follows and is stored in the file Salary1.kpl. |
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%. */ class Salaryl is public description This function computes the salary increase and updates the salary of an employee. */ function main is constants real percentl = 0.045 // percent increase real percent2 = 0.05 variables real salary real increase // body of function starts begin display "enter salary: " read salary if salary > 45000 then set increase = salary * percentl else set increase = salary * percent2 endif add increase to salary print "increase: ", increase, " salary: ", salary endfun main endclass Salaryl