Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2018

 

Examples | Java | Simple task

PrevNext


Solution of the simple task: Begin3

This section contains description of solving the following simple task in Java.

Begin3°. The sides a and b of a rectangle are given. Find the area S = ab and the perimeter P = 2(a + b) of the rectangle.

We shall use the NetBeans IDE 7.2 as programming environment, however the same results may be received in the NetBeans IDE 6.9, 7.3, 7.4 and Eclipse 4.

Creating a template and acquaintance with the task

To create a template of the required task one should use PT4Load tool.

Before creating the first Java template for NetBeans IDE the following information message will be output:

The created template is located in the PT_Java subdirectory of the working directory and consists of several files and subdirectories. But we need only MyTask.java file located in the src subdirectory. This file will be loaded into the code editor of NetBeans IDE:

// File: "Begin3"
public class MyTask extends PT
{
    public static void solve() throws Exception
    {
        task("Begin3");
    }
}

After loading the template project into the NetBeans IDE the project information will be output in the left-hand side of the IDE windows. If this information is not shown then you should execute the "File | Open project..." command and select the PT_Java project in the current directory. Because of importance of this action, it is described in the above-mentioned info window. This action should be performed just one time, the information about the next template projects will be output automatically. Note that in Eclipse no additional actions are required.

The MyTask.java file contains definition of the MyTask class; this class is a descendant of the PT class defined in the PT.java file. The MyTask class includes the solve function as its method. The solve function already contains the task function call that initializes the Begin3 task. The solution of the task should be entered in the solve method (of course any other methods may be used).

Note that the task function (just as get-functions and the put function) is a class method of the PT class (all these methods are declared with the "static" attribute). All these methods are called in the solve method of the MyTask class (that is a descendant of the PT class), therefore it is not necessary to qualify these methods with the PT prefix.

Also note that definition of main function is absent in the MyTask.java file. The main function is implemented as a static method of the PT class and is declared in the PT.java file. The PT.java file is not loaded into the code editor of NetBeanse IDE because this file requires no editing.

To run the program press [F6] key ([F11] key in Eclipse). When the program is launched you will see the Programming Task window with a task text and initial data.


This running is considered as acquaintance running because the program does not perform input-output operations. To close the Programming Taskbook window click the "Exit" button or press [Esc] or [F6]/[F11] key in Netbeans/Eclipse.

Initial data input

Before solving tasks you should input initial data in the program. In the Begin3 task the initial data are real numbers a and b that refer to sides of the rectangle.

The initial data should be input in the variable of the required type, otherwise Programming Taskbook will detect error. Let's model this situation in our program. For this purpose we shall organize initial data input to variables of the integer type using the getInt function:

public static void solve() throws Exception
{
    task("Begin3");
    int a = getInt(), b = getInt();
}

Run the program once again. You will see the new set of initial data. For each running of the program a new initial data are generated, so it is necessary to develop an algorithm that processes correctly all admissible sets of initial data.


In this case the running leads to the following information in the status bar: "Invalid type is used for an input data item." Note that each type of errors is marked by different color.

To input data correctly we should use the getDouble function that provides input data of double type. Change our program as follows:

public static void solve() throws Exception
{
    task("Begin3");
    double a = getDouble(), b = getDouble();
}

Now data input is performed correctly, but the program does not output results. In such situation we have the following message: "Correct data input: all required data are input, no data are output" (Programming Taskbook version 4.15 or higher) or "Some data are not output" (previous versions).

Calculation and output results

Let's perform the required calculations and output results using the put function:

public static void solve() throws Exception
{
    task("Begin3");
    double a = getDouble(), b = getDouble(), S, P;
    S = a * b;
    P = 2 * (a + b);
    put(P, S);
}

When the program is running you can see output values at the panel of results. Because we output data in inverse order, the status bar contains the error message "Wrong solution".


Correct solution and testing

To correct the last error it is enough to change order of arguments in the put method call:

    put(S, P);

When this program is running you will see the testing panel on screen:

This panel appears when the program processes successfully at least one set of input data. The progress bar shows the amount of tests which are already performed, the text above the bar allows to determine how much tests should be performed successfully. The program testing finishes in two cases: when all required tests are performed successfully or when some test is failed.

In our case the algorithm is correct, therefore the message "The task is solved!" will be shown.


To browse information about the task solving you can use PT4Results tool (you may run this tool by pressing the [F2] key or clicking the "Results" label in the upper-right corner of the Programming Taskbook window):

Begin3      j23/02 20:13 Acquaintance with the task.
Begin3      j04/02 15:07 Invalid type is used for an input data item.
Begin3      j04/02 15:09 Correct data input.
Begin3      j04/02 15:12 Wrong solution.
Begin3      j04/02 15:15 The task is solved!

The letter "j" denotes the programming language being used (Java).

Remark. It is not necessary to use additional variables S and P, because you can output expressions:

public static void solve() throws Exception
{
    task("Begin3");
    double a = getDouble(), b = getDouble();
    put(a * b, 2 * (a + b));
}

PrevNext

 

  Рейтинг@Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
06.05.2018