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 | C# and VB.NET | Simple task

PrevNext


Solution of the simple task: Begin3

This section contains description of solving the following simple task in C# and VB.NET.

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 Visual Studio 2008 as programming environment, however the same results may be received in the Visual Studio 2010–2015 IDEs.

In this and the following sections we shall use the "procedure" notion not only for VB.NET subroutines but also for C# functions with the void return type.

Creating a template and acquaintance with the task

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

The template created for Visual Studio 2008 consists of several files. But we need only Begin.cs file (for C#) or Begin3.vb file (for VB.NET). This file will be loaded into the editor of Visual Studio:

[Begin3.cs]

using PT4;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PT4Tasks
{
    public class MyTask: PT
    {
        public static void Solve()
        {
            Task("Begin3");

        }
    }
}

[Begin3.vb]

Option Strict On
Option Infer On
Imports PT4.PT
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Module MyTask
    Sub Solve()
        Task("Begin3")

    End Sub
End Module

The Begin3.cs file contains definition of the MyTask class; this class is a descendant of the PT class defined in the PT4 namespace. The MyTask class includes the Solve procedure as its method. The Solve procedure already contains the Task procedure call that initializes the Begin3 task. The Begin3.vb file contains definition of the Solve procedure; this procedure already contains the Task procedure call. The solution of the task should be entered in the Solve procedure (of course any other procedures and functions may be used).

Note that the Task procedure (just as Get-functions and the Put procedure) is a class method of the PT class (all these methods are declared with the "static" attribute in C# and the "Shared" attribute in VB.NET). In C# 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. In VB.NET the PT prefix is not necessary due to the "Imports PT4.PT" directive.

Also note that definition of Main procedure is absent in the Begin3.cs and Begin3.vb files. The Main procedure is defined in the pt4main file that was created when the project was generated for this task (the pt4main file has .cs extension for C# and .vb extension for VB.NET). The pt4main file is not loaded into the editor of Visual Studio. But the project includes this file (see the Solution Explorer window located on the Visual Studio window).

  

The project also includes the pt4net.dll library. This library contains classes used by Programming Taskbook, in particular the PT class.

To run the program press [F5] key. When the program is launched you will see the Programming Task window with a task text and initial data. Here is the screenshot of the window for the C# language; the window for VB.NET has the "[Visual Basic.NET]" caption instead of "[C#]".


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 [F5] key.

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:

[C#]

public static void Solve()
{
    Task("Begin3");
    int a = GetInt(), b = GetInt();
}

[VB.NET]

Sub Solve()
    Task("Begin3")
    Dim a As Integer = GetInt(), b As Integer = GetInt()
End Sub

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:

[C#]

public static void Solve()
{
    Task("Begin3");
    double a = GetDouble(), b = GetDouble();
}

[VB.NET]

Sub Solve()
    Task("Begin3")
    Dim a As Double = GetDouble(), b As Double = GetDouble()
End Sub

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 procedure:

[C#]

public static void Solve()
{
    Task("Begin3");
    double a = GetDouble(), b = GetDouble(),
        S = a * b, P = 2 * (a + b);
    Put(P, S);
}

[VB.NET]

Sub Solve()
    Task("Begin3")
    Dim a As Double = GetDouble(), b As Double = GetDouble(), _
        S As Double = a * b, P As Double = 2 * (a + b)
    Put(P, S)
End Sub

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 procedure call:

[C#]

    Put(S, P);

[VB.NET]

    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. If the program is in infinite loop then you should close the testing panel by means of the [x] button in its upper right corner.

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      S23/02 20:13 Acquaintance with the task.
Begin3      S04/02 15:07 Invalid type is used for an input data item.
Begin3      S04/02 15:09 Correct data input.
Begin3      S04/02 15:12 Wrong solution.
Begin3      S04/02 15:15 The task is solved!

The letter "S" denotes the programming language being used (C Sharp).

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

[C#]

public static void Solve()
{
    Task("Begin3");
    double a = GetDouble(), b = GetDouble();
    Put(a * b, 2 * (a + b));
}

[VB.NET]

Sub Solve()
    Task("Begin3")
    Dim a As Double = GetDouble(), b As Double = GetDouble()
    Put(a * b, 2 * (a + b))
End Sub

PrevNext

 

  Рейтинг@Mail.ru

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

Last revised:
06.05.2018