Hello! Do you want to write a program or toy for a mobile phone, and there is no desire or opportunity to learn the wisdom of Java? Then the ideal tool will be the software product MidletPascal. It is a kind of highly simplified Pascal, which, after compilation, is converted into java bytecode and is issued as two files - jar and jad. And the compilation requires nothing but MidletPascal itself, which again can not but rejoice. In this article, I will try to explain the very basics of this wonderful language. It is assumed that you are somewhat familiar with the Pascal language and have some experience programming in it.
Start MidLetPascal. Let's create our first program. Click "File - "New Project" and in the field "Project name" enter the name of the project (for example - "MyProject". Without quotation marks, of course).
After that, a text similar to the following automatically appears:
program MyProject; begin drawText('Hello world!', 0, 0); repaint; delay(2000); end.
If you are familiar with Pascal, then in principle everything is clear here. The first line is the name of the project. Next, between begin and end is the text of the program itself. The drawText procedure displays text. In this case, the text "Hello world!" is displayed on the screen with coordinates (0.0), i.e. in the upper left corner. But in reality, the text will still not be visible on the screen. The fact is that not one, but two "screens" are used, i.e. the so-called double buffering. First, the front buffer is displayed, and drawText outputs the text to the back buffer. Therefore, in order for our inscription to still be visible to the user, the following repaint command is used, which redraws the contents of the rear buffer to the visible screen. But there is one "but". The repaint procedure is slow and should be called as infrequently as possible. For example, the following code will work, but relatively slowly:
program MyProject; begin drawText('Строка 1', 0, 0); repaint; drawText('Строка 2', 0, 10); repaint; drawText('Строка 3', 0, 20); repaint; delay(2000); end.
This is not necessary, because here the screen is redrawn as many as three times, which is quite slow. It would be more correct to do this:
program MyProject; begin drawText('Строка 1', 0, 0); drawText('Строка 2', 0, 10); drawText('Строка 3', 0, 20); repaint; delay(2000); end.
Well, the last line - delay (2000) - pauses. The parameter (in parentheses) specifies the number of milliseconds. If you do not pause, then when the program reaches the end, it will end and the user simply will not have time to see the text on the screen.
Now let's write a program that does something useful :) For example, it will convert degrees of Fahrenheit to degrees Celsius. This puzzle is quite often given in programming. Let's see how to solve it on MidletPascal. Suppose we want to derive Fahrenheit values from 0 to 10 and similar degrees Celsius. Let's start with mathematics. All this will be calculated according to this formula:
C = (F - 32) * 5 / 9,
where F is degrees on the Faregate scale and C is celsius, respectively. Next, we proceed to write the program itself. Well, first we create a new project, as mentioned above and call it, for example, "Perevod".
Next, we need to enter the variables C and F. To do this, write "var" on the next line and then write our variables (the construction is completely similar to pascal):
var C, F: integer;
an integer is a type of variable (in this case, an integer). Since we have a lot of degrees, or rather the interval from 0 to 10, we organize a cycle:
for F:=0 to 10 do begin C:=trunc((F - 32) * 5 / 9); drawText(IntegerToString(F)+' -> '+IntegerToString(C), 0 , F*10); end;
That is, the F we will have will change from 0 to 10. When calculating C, trunc will discard the fractional part, which is done to simplify the process. Next, with the help of drawText, we bring out all this ugliness. By the way, in the last parameter - F*10 is made to automate the transition to the next line when output. And in the end, I will give the full text of the program:
program Perevod; var C, F: integer; begin for F:=0 to 10 do begin C:=trunc((F - 32) * 5 / 9); drawText(IntegerToString(F)+' -> '+IntegerToString(C), 0, F*10); end; repaint; delay(5000); end.
That's all for now. In the next article, we'll talk about displaying pictures and handling button clicks.