Pascal ile Karşılaştırma - 1

"Merhaba. Benim adım Laga Bilaabo. Ben bir dünya dışıyım ve bu geminin doktoruyum. Umarım iyi arkadaş oluruz."

"Ben de."

"Benim gezegenimde, geri kalmış Java dili yerine gelişmiş Pascal programlama dilini kullanırız. Java ve Pascal kodunun şu yan yana karşılaştırmasına bakın:"

java Pascal
public class MyFirstClass
{
   public static void main(String[] args)
   {
      int a, b, c;
      String s1, s2;
      System.out.println("Enter two numbers");
      a = new Scanner(System.in).nextInt();
      b = new Scanner(System.in).nextInt();
      c = a + b;
      System.out.println("The sum is " + c);
   }
}
Program MyFirstProgram;
Var
   a, b, c: Integer;
   s1, s2: String;
Begin
   WriteLn("Enter two numbers");
   ReadLn(a);
   ReadLn(b);
   c := a + b;
   WriteLn("The sum is ", c);
End.

"Bu, iki farklı dilde yazılmış aynı program. Gördüğünüz gibi Pascal'da daha az satır var, bu da Pascal'ın Java'dan üstün olduğunun kanıtı."

"Pascal'ı daha önce gördüyseniz, bu örneğin Java'yı daha iyi anlamanıza yardımcı olabileceğini düşündüm."

"Hayır, yapmadım. Yine de iki programlama dilini karşılaştırmak ilginç."

"Yeterince adil. O zaman devam edeceğim."

"Pascal'da kodu program gövdesine, prosedürlere veya işlevlere yerleştiririz. Java'da bu işlem basitleştirilmiştir: program gövdesi, prosedürler ve işlevlerin tümü, yöntem adı verilen işlevlerle değiştirilir."

java Pascal
Ana yöntem
public static void main(String[] args)
{
   System.out.println("Ho-ho-ho!");
}
Program gövdesi
Begin
   WriteLn("Ho-ho-ho!");
End.
işlev/yöntem
double sqr(double a)
{
   return a * a;
}
İşlev
Function Sqr(a: Real): Real
Begin
   Sqr := a * a;
End;
geçersiz dönüş tipine sahip işlev
void doubleWrite(String s)
{
   System.out.println(s);
   System.out.println(s);
}
prosedür
Procedure DoubleWrite(s: String);
Begin
   WriteLn(s);
   WriteLn(s);
End;

"Pascal sütununda 'program gövdesi', 'işlev' ve 'prosedür' sözcüklerini görüyorum ama Java'da bunların hepsine yöntem deniyor. Bu biraz tuhaf."

"Evet, biz uzaylılar bunu çok tuhaf buluyoruz. Ama insanlar her şeyi birleştirmeyi sever."

"Java'da tüm kodlar bir yöntemin parçasıdır, dolayısıyla bir yöntemi bildirmek için Pascal'da olduğu gibi Function kelimesini yazmanıza bile gerek yoktur. "

"Her şey çok basit. Bir kod satırı Type + Name gibi görünüyorsa , bu bir yöntemin veya değişkenin bildirimidir. Adın ardından parantez geliyorsa, bu yeni bir yöntemin bildirimidir. Parantez yoksa, o zaman bu bir değişken bildirimidir. "

"Java'daki değişken bildirimleri ve yöntemler birbirine çok benzer. Kendiniz görün:"

kod Tanım
String name;
Adı verilen değişken namea String.
String getName()
{
}
getNamea döndüren çağrılan yöntem String.

"Ama hepsi bu kadar değil. Java'da yöntemler tek başlarına var olamazlar. Bir sınıfın içinde olmaları gerekir. Bu nedenle, insanlar Java'da küçük bir program yazmaları gerektiğinde, önce bir sınıf oluşturmalı, içinde bir ana yöntem bildirmeli ve ancak o zaman yöntemde kodlarını yazabilirler . Bu dünyalılar çok tuhaf!"

"Diego bugün erkenden uğradı ve sana bu görevleri vermemi istedi. Umarım beğenirsin."

1
Görev
Java Syntax,  seviyeders
Kilitli
The great purge
Whoever wrote this program obviously did it in a hurry. Actually, that's not true: this program was written for educational purposes, and the author deliberately crammed in superfluous variables and simultaneously failed to declare necessary variables. We're going to correct this: Comment out the unnecessary variables, and declare the missing variables. Then the program will achieve universal harmony.
1
Görev
Java Syntax,  seviyeders
Kilitli
Don't feel like it? Do it anyway.
Laziness afflicts even the best programmers. And not only programmers. Nevertheless, people have managed to become professionals by teaching themselves. So, we suggest not being lazy. Instead, display this slogan on the screen: "If you feel like it, do the task. If you don't feel like it, do it anyway". And to really remember this, display it 16 times.
5
Görev
Java Syntax,  seviyeders
Kilitli
Square of a number
There are several ways to square a number. For example, some people write a number and then draw a square around it. This method is used by people who haven't studied anywhere. Everybody else has to multiply, remember the table of squares, ... or use a program. Your program should display the square of 5.
5
Görev
Java Syntax,  seviyeders
Kilitli
As simple as 2+2
In a Java application, all the actions are performed by functions. Or more accurately, methods. In our program, a kind mentor has already implemented a method (that is, written the method's code) that can calculate the sum of two numbers. All you need to do is call this method with the arguments 2 and 2. You need to do this in the main method.
1
Görev
Java Syntax,  seviyeders
Kilitli
Choose healthy food! Choose fruit!
Display the variables whose values are names of fruit. Display each variable on a new line.