**Calculation of the area of circle and volume of a sphere**

In this article will be described how to create simplify applications, that will used in Windows environment.

**Task**

Develop the application that will calculate of the area of circle and volume of a sphere for given radius R. The application must be developed as Windows-application.

Before developing we need to implement a mathematical formulation of the problem. It means that we need to determine the formules and data that are used in calculation and also input/output results.

**Mathematical statement of the problem**

The area of а circle is given by:

*S* = *π *· *R²*

The input value is a radius R. The result is the area of a circle S.

The volume of a sphere is given by:

*V = 4/3 · π · R³*

The radius is the input valut here, again. The volume of a sphere is the result.

In both formulas is defined a *π* constant, that equals 3,14159.

So, the sequense of solution of problem is the next (fig. 1).

**Instuctions**

### 1. Creating the application of “VCL Forms Application” type

Run the Embarcadero RAD Studio Delphi 2010 and creating the Windows-application.

The initial view of the form window is shown in Fig. 2.

Fig. 2. View of the form window

**2. The Tab “Standard” of the tool panel of “Tool Palette”.**

In this application we need to use several components (controls), that are given below:

- the component of TLabel type, that is text string on the form view;
- the component of TButton type, that is a button on the form;
- the component of TEdit type, that is input box on the form.

All these components are placed on the “Tool Palette” tool panel at the “Standart” tab (see fig. 3).

Fig. 3. The “Standart” tab of components palette

##### 3. The TLabel component (control)

**3.1. Placing the TLabel component on the form**

First of all, we need to click (left mouse button) at the TLabel (fig. 4).

After that we need to click at left-top corner of the form’s window as shown in figure 5.

Fig. 4. The TLabel component at the tool palette

Fig. 5. The TLabel component at the main form

**3.2. Setting a text into TLabel component**

To perform any action with the component TLabel, first of all, we need select (using the “mouse” or selection panel “Object Inspector”) it. Then we need to set the Caption property of TLabel component to the value “R =” (Fig. 6).

Fig. 6. The Caption property

As a result, the text “Label1” on the form will be changed into “R =”.

In Object Inspector we can view many different properties this component. In our case we are interested in the “Name” property, that contains the name of the variable value. By default this value is equaled “Label1”. This means that when we are writing code we can appeal to the component’s properties with the prefix “Label”.

For example, to change into the program the “Caption” property we need type the following line:

`Label1.Caption := 'R = ';`

Similarly, we place the components with names Label2 and Label3 under the previous component and set the Caption values according to the “S =” and “V =”.

Main Form has view as shown in fig. 7.

Fig. 7. The application form after placement Label1, Label2 and Label3 components

Transferring and handling all other components from the palette Tool Palette is similar.

##### 4. The TEdit component (control)

We add component TEdit from tab “Standard” of “Tool Palette” component panel, that is a simply input string. With the help of using this component we will get a radius value inputted by user from keyboard. After adding the component on the form, Delphi system creates component-variable with the name “Edit1” (property Name).

Clear the property “Text” of the component.

##### 5. The TButton component (control)

Add TButton component from tab “Standard” of “Tool Palette” component panel, that is a simply buttom. After pressing on the button the application will calculate the area of circle and volume of sphere. The Delphi system automatically add component-variable with the name of “Button1”.

Change the value of Caption component into “Calculate”.

In design mode the application form will have view as shown in fig. 8.

Fig. 8. Application form after adding TEdit and TButton components

##### 6. The programming of event of clicking at the “Calculate” button

The next step in our application is the programming of event in Delphi, that is generated when you click on “Button1” button. The event of mouse click is named as “OnClick”.

Delphi 2010 automatically creates code snippet, where we need to write our own code. The code that were created has the following view:

**procedure** TForm1.Button1Click(Sender: TObject);
**begin**
**end;**

The first priority is determining the input data, results or intermediate variables to be used in the program.

According to the problem condition in our program we will describe three variables with the appropriate denotes:

- R – radius of circle;
- S – area of circle;
- V – volume of sphere.

All variables must be floating point type.

Also in application used one constant – the Pi number (3.1415). Let this number is called Pi. It is noted that Delphi system has internal function with the Pi name, but in our program this function will not used.

So, the description of variables and constants before the “begin” instruction will be following:

**const**
Pi = 3.1415; // the Pi number
**var**
R:real; // radius of circle
S:real; // area of circle
V:real; // volume of sphere

Between the keywords “begin” and “end” we inscribe the next strings of main program code:

```
// 1. Reading the value of the radius
// of the circle component Edit1.Text
R := StrToFloat(Edit1.Text);
// 2. Calculating the circle area
S := Pi * R * R;
// 3. Calculating the volume of sphere
V := 4/3 * Pi * R * R * R;
// 4. The output of results with precision
// of 3 decimal places
Label2.Caption:='S='+FloatToStrF(S,ffFixed,8,3);
Label3.Caption:='V='+FloatToStrF(V,ffFixed,8,3);
```

Let us explain some of the functions used in the code. StrToFloat function converts the Edit1.Text values into floating point number.

For example, after executing this code

`x := StrToFloat('-3.675');`

the value of x will be -3.675.

Paragraphs 2 and 3 describe conventional calculating the area of a circle and the volume of a sphere using the arithmetic of Pascal.

Paragraph 4 describes outputting results on screen.

Since the program is implemented as a Windows-based application, for results output we need just fill the value of Caption property into Label2 (area) and Label3 (volume) components.

The FloatToStrF function performs the inverse transformation to the function StrToFloat, ie converts the actual number into a string.

For example, if we need convert the 2.87 number to a string with precision 4 digits after the decimal point we should write:

```
v := 2.87;
str := FloatToStrF(v, ffFixed, 8, 4);
```

where

- v – variable of floating point type;
- str – variable of string type;
- ffFixed – format of conversion. The constant 8 means that used width of output size with 8 characters. The constant 4 means precision after the decimal point.

The total listing of procedure of OnClick event processing of Button1 component is given below:

**procedure** TForm1.Button1Click(Sender: TObject);
**const**
Pi = 3.1415; // the Pi number
**var**
R:real; // radius of circle
S:real; // area of circle
V:real; // volume of sphere
**begin**
// 1. Reading the value of the radius
// of the circle component Edit1.Text
R := StrToFloat(Edit1.Text);
// 2. Calculating the circle area
S := Pi * R * R;
// 3. Calculating the volume of sphere
V := 4/3 * Pi * R * R * R;
// 4. The output of results with precision of
// 3 decimal places
Label2.Caption:='S='+FloatToStrF(S,ffFixed,8,3);
Label3.Caption:='V='+FloatToStrF(V,ffFixed,8,3);
**end;**

##### 7. Changing the application name

To change the application title, that corresponds of the Form1 main form, we need to change a Caption property of a form. Set this property in value “Calculating of the circle area and the sphere volume”.

##### 8. Program results

After application running will show a window that contains a query to input a radius R of circle.

Input the 2,5 value. The window of results is displayed on fig. 9.

Fig. 9. Program results

##### The conclusions

In this article were developed desktop application that calculates a circle area and a sphere volume. The application uses several components of the following types:

- TLabel – a component of a “label”-type, that is simply text string;
- TButton – a component, which represents a usually button at form.
- TEdit – is an input string, intended for getting information that was entered from keyboard by the user.

For the design of program interface the Tool palette and Object Inspector were used.

**Related topics**

**Borland C++ Builder – An example of application development of calculating of cylinder volume.****C# – An example of creating of Web-application in MS Visual Studio.**