CodeGym /Java Course /Java Syntax /Comparison with Pascal

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."

1
Task
New Java Syntax, level 1, lesson 8
Locked
Bugs and features
Write a program that displays: It's not a bug - it's a feature.
1
Опрос
Commands and your first program,  1 уровень,  8 лекция
недоступен
Commands and your first program
Commands and your first program
Comments (185)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Shadow Level 3, 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..
Asheem Chhetri Level 4, Golden Valley, United States
7 January 2021
Which part you got confused with? I am guessing calling of the sqr() method. In java, when you write a method it can accept any number of parameters i.e the variables in () like this:

public int hey_I_am_a_method(int parameterA, int parameterB)
{
     // Inside the method body, you can do anything with the parameters, or define your own variables.
     int variableC = 10;
     // Every method has to return something or return nothing
     // Lets say this method returns an INT
     return (parameterA + parameterB + variableC);
}
** So now when you call this method in your MAIN() you have to provide the value for those 2 parameters like this:

hey_I_am_a_method(20, 5)
which will hold the value 35, because we are using a variable in the above method as well.
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."
Deepak Mehta Level 2, Ankleshwar, India
17 December 2020
No, the audience of that statement is people who are familiar with Pascal (ever seen Pascal). That's why Amigo replies in next line : No, I haven't
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?
Gaelle Gilles Level 15, New York City, United States
21 October 2020
When you create a method, you are basically creating a blueprint for anybody to use. This allows any user to use any number they wish without having to write the same method over and over again. The reason why sqr is an int is because that is the datatype of the method. Methods are written in the general format of data_type method_name(parameters){ //actions}. Also, take note that Java is a strong datatype language. Datatypes play a heavy part when writing in the java language. Hope this helps.
Mihai Bone Level 8, Bucharest, Romania
22 September 2020
The fruit one was tricky.
Dinesh Level 7, Delhi, India
3 January 2021
What was the trick in that can you elaborate plz
Mihai Bone Level 8, Bucharest, Romania
5 January 2021
They ask you to display only the fruits and they include sausages or things that are not fruits. If you don't read carefully you get an fail check.
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