undefined

Comparison with Pascal

Java Syntax
Level 1 , Lesson 8
Available
Comparison with Pascal - 1

"Hi. My name is Laga Bilaabo. I'm an extraterrestrial and this ship's physician. I hope we'll be good friends."

"Me too."

"On my home planet, we use the advanced Pascal programming language instead of the backward Java language. Look at this side-by-side comparison of Java and Pascal code:"

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.

"This is the same program written in two different languages. As you can see, it has fewer lines in Pascal, which is evidence that Pascal is superior to Java."

"I thought this example might help you better understand Java if you've ever seen Pascal before."

"No, I haven't. Still, it's interesting to compare two programming languages."

"Fair enough. I'll continue then."

"In Pascal, we place code in the program body, procedures or functions. In Java, this process is simplified: the program body, procedures, and functions are all replaced by functions called methods."

Java Pascal
Main method
public static void main(String[] args)
{
   System.out.println("Ho-ho-ho!");
}
Program body
Begin
   WriteLn("Ho-ho-ho!");
End.
Function/method
double sqr(double a)
{
   return a * a;
}
Function
Function Sqr(a: Real): Real
Begin
   Sqr := a * a;
End;
Function with void return type
void doubleWrite(String s)
{
   System.out.println(s);
   System.out.println(s);
}
Procedure
Procedure DoubleWrite(s: String);
Begin
   WriteLn(s);
   WriteLn(s);
End;

"In the Pascal column, I see the words 'program body', 'function', and 'procedure', but in Java they are all called methods. That's a little weird."

"Yes, we extraterrestrials find it very weird. But humans like to unify everything."

"In Java, all code is part of a method, so you don't even need to write the word Function, like in Pascal, to declare a method."

"It's all very simple. If a line of code looks like Type + Name, it's a declaration of either a method or a variable. If the name is followed by parentheses, then it's a declaration a new method. If there are no parentheses, then it's a declaration of a variable."

"Declarations of variables and methods in Java are very similar. See for yourself:"

Code Description
String name;
Variable called name that is a String.
String getName()
{
}
Method called getName that returns a String.

"But that's not all. In Java, methods cannot exist in isolation. They must be inside a class. Thus, when humans need to write one small program in Java, they must first create a class, declare a main method in it, and only then can they write their code in the method. These earthlings are so weird!"

"Diego dropped by earlier today and asked me to give you these tasks. I hope you like them."

undefined
1
Task
Java Syntax, level 1, lesson 8
Locked
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.
undefined
1
Task
Java Syntax, level 1, lesson 8
Locked
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.
undefined
5
Task
Java Syntax, level 1, lesson 8
Locked
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.
undefined
5
Task
Java Syntax, level 1, lesson 8
Locked
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.
undefined
1
Task
Java Syntax, level 1, lesson 8
Locked
Choose healthy food! Choose fruit!
Display the variables whose values are names of fruit. Display each variable on a new line.
undefined
1
Task
Java Syntax, level 1, lesson 8
Locked
Favorite poem
Today's programmers are rarely full-fledged engineers obsessed with technology and gadgets and nothing else. Some of them are also passionate about poetry. If you're a fellow poetry-lover, then you'll find it easy to perform the following task: display a quatrain (or more) from your favorite poem.
Comments (186)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Shadow Level 2 Denver United States
26 March 2021
The square root one threw me off at first lol, I was trying to declare the variable a, however a variable is already declared through the method just had to give it a value! Thanks codegym! I've had a hard time understanding that concept before but now through this exercise it finally "clicked"!
Aliya Sagar Dalvi Level 2 Pune India
13 March 2021
I cant unlock the task because I am join free i am very keen to learn java but i still doing my best to learn thank you codegym System.out.println("I like your approach to lessons");
Isa Musa Level 3 Kano Nigeria
2 January 2021
Can someone please help me on this sqr method? I am stuck please..
1 Level 2 Issy-les-Moulineaux France
13 December 2020
there is an n missing in "I thought this example might help you better understand Java if you've ever seen Pascal before." it should be "I thought this example might help you better understand Java if you've never seen Pascal before."
abdu saeed Level 1 Riyadh Saudi Arabia
3 November 2020
I cant unlock the task because I am join free i am very keen to learn java but i still doing my best to learn thank you codegym System.out.println("I like your approach to lessons");
Mark Level 2 Madrid Spain
30 September 2020
I don't get why in the "square of a number" exercise, we don't call a variable int a = 5 and instead we use the screen output. How does it know sqr(5) is what goes inmediately below? Am I to understand sqr is an int and a is an int, so it just substitutes both below?
Mihai Bone Level 8 Bucharest Romania
22 September 2020
The fruit one was tricky.
Yuliya Samsonava Level 3 United States
18 September 2020
How can I see where I am in the whole structure of lessons? How far I am from level 2?
jordan Level 6 Houston United States
8 September 2020
how can i reset the text in the problem?
Ngeno Gilbert Level 1
25 August 2020
Code gym