The following problem applies the concepts of arrays and of polymorphism. A list of objects is required to store geometric figures and to calculate the perimeter and area of each figure when selected from the list. The geometric figures are circles, triangles, and rectangles.
The list is designed as an array of objects of class Gfigures. The solution to this problem is generic by using this abstract class, the base class for all the other classes. The type of the array is Gfigures, this class was defined in Sections 13.2.1 and Section 13.2.2. The capacity of the array is set with constant MAX_GEOM.
Each element of the array is an object reference of a concrete class, Circle, Triangle, or Rectangle. Figure 13.2 illustrates the basic structure of this heterogeneous array.
Figure 13.2: A heterogeneous array of objects.
An object is created for every object reference in the array. To process the array, the index value is used to access the needed element and invoke the methods to compute the perimeter and the area of the geometric figure. The execution of these functions is carried out via polymorphism. The implementation of the problem solution in KJP is not too different than the one explained in the first part of this chapter. The following statements declare (and create) an array of geometric figures and declare an object of class Circle.
object fig_list array [MAX_GEOM] of class Gfigures object circle_obj of class Circle
The type of array is the base class Gfigures, an abstract class. The types of the objects referred by the array elements are of the concrete classes that are subtypes of Gfigures. The following statements create an object of class Circle and assign it to an element of array fig_list.
// read value for radius display "enter value of radius for circle: " read r // create circle object create circle_obj of class Circle using r set fig_list [1] = circle_obj
Class Mgfigures implements the complete program for storing object references for objects of the subclasses Circle, Triangle, and Rectangle. The KJP code for the implementation of the Mgfigures class follows.
description This program uses a heterogeneous array to store different types of geometric figures. For each element, the corresponding object computes the area and perimeter of the fi- gure. */ class Mgfigures is public description This function gets the area and perimeter of a triangle object. */ function main is constants integer MAX_GEOM = 15 // array capacity variables integer i // index for array processing double x // first side double y // second side double z // third side double r // radius double area double perim objects object geom_figure of class Gfigures object triangle_obj of class Triangle object circle_obj of class Circle object rect_obj of class Rectangle object fig_list array [MAX_GEOM] of class Gfigures begin // Read data display "enter value first side of triangle: " read x display "enter value second side: " read y display "enter value third side: " read z // create triangle object create triangle_obj of class Triangle using x, y, z set fig_list[0] = triangle_obj // read data display "enter value radius of circle: " read r // create circle object create circle_obj of class Circle using r set fig_list[l] = circle_obj // read data display "Enter value first side of rectangle: " read x display "enter value of second side: " read y // create rectangle data create rect_obj of class Rectangle using x, y set fig_list[2] = rect_obj // invoke polymorphic functions for i = 0 to 2 do set area = call area of fig_list[i] display "Area of geometric fig is: ", area set perim = call perimeter of fig_list[i] display "Perimeter of geometric fig is: ", perim endfor endfun main endclass Mgfigures
Note that only three elements of the array are actually used, although the array has 15 elements. The Java code for class Mgfigures follows.
On the CD | The KJP code with the implementation of class Mgfigures is stored in the file Mgfigures.kpl, and the Java code implementation is stored in the file Mgfigures.java. |
// KJP v 1.1 File: Mgfigures.java, Thu Jan 23 19:42:37 2003 /** This program uses a heterogeneous array to store different types of geometric fi- gures. For each element, the corresponding object computes the area and perimeter of the figure. */ public class Mgfigures { /** This function gets the area and perimeter of a triangle object. */ public static void main(String[] args) { final int MAX_GEOM = 15; // array capacity int i; // index for array processing double x; // first side double y; // second side double z; // third side double r; // radius double area; double perim; Gfigures geom_figure; Triangle triangle_obj; Circle circle_obj; Rectangle rect_obj; // body of function starts here Gfigures fig_list[]= new Gfigures [MAX_GEOM]; // read data System.out.println( "enter value first side of triangle: "); x = Conio.input_Double(); System.out.println( "enter value second side: "); y = Conio.input_Double(); System.out.println("enter value third side: "); z = Conio.input_Double(); // create triangle object triangle_obj = new Triangle(x, y, z); fig_list [0] = triangle_obj; // read data System.out.println( "enter value radius of circle: "); r = Conio.input_Double(); // read circle object circle_obj = new Circle(r); fig_list [1] = circle_obj; // read data System.out.println( "Enter value first side of rectangle: "); x = Conio.input_Double(); System.out.println( "enter value second side: "); y = Conio.input_Double(); // create rectangle object rect_obj = new Rectangle(x, y); fig_list [2] = rect_obj; for (i = 0 ; i <= 2; i++) { area = fig_list [i].area(); System.out.println( "Area of geometric fig is: "+area); perim = fig_list [i].perimeter(); System.out.println( "Perimeter of geometric fig is: "+ perim); } // endfor } // end main } // end Mgfigures
The output for an execution run of this class is shown next. The input values are shown in the text file as shown. Note that the values of the sides of the triangle correspond to a well-defined triangle.
enter value of first side of triangle: 2 enter value of second side: 4 enter value of third side: 5 enter value radius of circle: 3.5 Enter value of first side of rectangle: 4.5 enter value of second side: 7.25 Area of geometric fig is: 3.799671038392666 Perimeter of geometric fig is: 11.0 Area of geometric fig is: 38.4844775 Perimeter of geometric fig is: 21.99113 Area of geometric fig is: 32.625 Perimeter of geometric fig is: 23.5