M1 Basic Concepts
M1 Basic Concepts
Programming 1
Contents
1. Programming Languages
2. The Java Platform
3. Your first Java program
4. Programming Algorithms
-2-
Programming
languages
Programming languages over time 1.4
-4-
Examples 3GL
•Java – C – C++ – C# – Objective C - Visual
Basic – Scala – Kotlin – Groovy – Rust – Go –
Delphi – Perl – Python – SQL – Javascript –
Cobol – Ruby – Prolog – Lisp – bash –
Smalltalk – PHP – ...
-5-
Why is Java so popular?
•1st release, January 1996 by Sun Microsystems
•In the ’90s, C++ was then very popular (still is)
•Comparing some characteristics of Java and C++
Characteristic C++ Java
Syntax Based on C Based on C++
Object Oriented Yes Yes
Suitable for Yes No
system
programming
-6-
Compiled language: 3GL text to binary (compile)
Source code file (text)
for (int i = 0; i < 10; i++) { Different transformations
}
System. out.println(i);
for ifferent processors
Compiler Compiler
Intel ARM
Run on Run on
Intel ARM
-7-
Compiled language: execute binary code (run)
Source code file (text) •Example:
for (int i = 0; i < 10; i++) { – C, C++
System. out.println(i);
}
•Pro:
– Compile-time checks
– Portable source code
– Execution: fast.
Compiler Compiler Compiler generates binary
Intel ARM
code which can be
executed directly
– Secure: binary code not
readable at target platform
Binary code Binary code
file Intel file ARM
01011101 11101100 •Contra:
11101001 01101011
Develop – Platform dependent:
(development platform) different compiler per
Execute target platform
(target platform)
– Changing code (e.g. for
Run on Run on testing) requires
Intel ARM
recompilation
-8-
Interpreted language: interpret + execute
•Example:
– JavaScript
Source code file (text) •Pro:
Develop
(development platform)
– Portable source code
– Runnable source code
Execute
(target platform)
– Platform independent:
Interpreter Interpreter
runs on any platform
Intel ARM (if interpreter installed)
•Contra:
Binary code
– No compile-time checks
Binary code
Intel ARM – You need an interpreter on
each system where the
program runs
– Execution: slow.
Run on Run on
Transformation from text
Intel ARM to binary is needed every
run
– source code readable on
target platform
-9-
Compiled/Interpreted language 1.9
•Language
Source code file (text)
(MyProgram.java)
– Java
•Pro:
Compiler
Bytecode for the – Compile-time checks
Java Virtual – Portable source code
Java
– Platform independent:
Machine (JVM): a runs on any platform
software processor (if Java platform installed)
Bytecode
Develop – Execution: faster than
(MyProgram.class)
(development platform) interpreter
Execute
Interpreter
Intel
Interpreter
ARM
(target platform) •Contra:
– Extra compile step just
before execution (JIT)
– Slower than compiled code
Run on Run on (= native code)
Intel ARM – You need a virtual machine
to run the code
(virtual machine: part of
the Java platform) - 10 -
Deege U Java lesson 001
The Java
platform
The Java platform
- 12 -
Java applications
•Desktop applications
– Examples: LibreOffice, Minecraft, …
•Server applications
- 13 -
JRE / JDK / IDE
•Java Runtime Environment (JRE):
– JVM + Java API
– needed to run java programs
•Java Development Kit (JDK):
– Tools: compile, debug, …
– Contains JRE
– Optional: Java API reference documentation
– JDK = JRE + some extras (tools, docs)
•Integrated Development Environment (IDE)
– Workbench application for developers
– Often usable for development in multiple
programming languages
- 14 -
Exercise
•Ex 01.01: install software
– Install IDE: IntelliJ
▪other popular IDEs: Eclipse, Visual Studio Code
– Install the JDK
- 15 -
IntelliJ: instant feedback...
- 16 -
Quiz
•What do following terms mean?
•3GL
•compiling
•bytecode
•JVM
•JRE
•JDK
•API
- 17 -
Deege U Java lesson 002
Bytecode
(MyProgram.class)
Intel
Interpreter
Run on
Intel
- 19 -
1. Write source code
•File extension '.java' Source code
(MyProgram.java)
Intel
Interpreter
Run on
Intel
- 20 -
Code walkthrough 2.2
- 21 -
2. Compile (command prompt)
•javac is the command line Source code
(MyProgram.java)
compiler, bundled with the JDK
•Execute the following on the
Java
command prompt (from the parent Compiler
folder of ‘hello’!)
javac hello/HelloWorldApp.java Bytecode
(MyProgram.class)
- 22 -
3. Run (command prompt)
•java is the JDK or JRE command Source code
(MyProgram.java)
that starts the JVM and executes
bytecode
Java
•Execute the following on the Compiler
Run on
Intel
- 23 -
3. Run (IntelliJ)
•IntelliJ “Run” will both compile and run your code
▪from right click menu, run button or SHIFT+F10:
▪IntellJ by default puts
» source files in the src folder
» .class files in the out folder
▪ result is shown in the bottom run window pane
- 25 -
Programming
algorithms
Programming algorithm
•Algorithm: steps to perform a task
•Programming is
– designing an algorithm to perform a task
– encode the algorithm in a programming
language to let a computer do the task
- 27 -
Pizza order Robot
•Algorithm to order pizza?
Ela orate steps to the evel
un erstoo b the ro ot. Ste 1
start (get enu): see ne t s ide...
ro ot processes in ut
ro ot nee s to e a le to
com ute t e amount
en
- 28 -
loo : re eatin a
se uence of ste s se eral
times
test: different actions are
ta en de endin on t e
resu t
- 29 -
Pizza Order Robot conclusions
•Algorithm:
– Sequence of actions
– Typical actions:
▪Ask Input
▪Produce Output
▪Test
▪Repeat
▪Compute
– Elaborate actions to the level understood by
the robot
- 30 -
A simpler example: Sum 2.5
- 31 -
Sum: algorithm
in ut an output (I/O)
- 32 -
Sum: transform algorithm to Java
•Each algorithm step becomes a Java statement
terminated by a semicolon ;
•Output to display:
System.out.print("text");
•Input from keyboard:
keyboard.nextInt();
- 33 -
Sum: transform algorithm to Java (1)
Text is quoted
System.out.print("Enter a number: ");
- 34 -
Sum: transform algorithm to Java (2)
first = keyboard.nextInt();
- 35 -
Sum: transform algorithm to Java (3)
first = keyboard.nextInt();
System.out.print(
"Enter another number: ");
second = keyboard.nextInt();
System.out.print(
"Enter another number: ");
second = keyboard.nextInt();
- 39 -
Exercises
•Ex 01.03 – Ex 01.07
- 40 -
More complex example: Higher/Lower
•Design the algorithm for this game:
Enter a number: 100
Lower! Enter a number: 50
Higher! Enter a number: 60
Lower! Enter a number: 55
Higher! Enter a number: 56
Congratulations, your guess is correct.
- 41 -
Algorithm Higher/Lower
loo
- 43 -
How to transform the algorithm to Java (1)
secret = 56;
System.out.print("Enter a number: ");
guess = keyboard.nextInt();
if (guess < secret) {
- 44 -
How to transform the algorithm to Java (2)
secret = 56;
System.out.print("Enter a number: ");
< true i sma ler guess = keyboard.nextInt();
if (guess < secret) {
System.out.print("Higher! ");
}
if (guess > secret) {
System.out.print("Lower! ");
}
> true i greater
Wha ut loo ?
- 45 -
How to transform the algorithm to Java (3)
guess = 0;
secret = 56;
int guess = 0;
You can eclare a varia le an assi n an
initia value in one statement
int secret = 56;
Scanner keyboard = new Scanner(System.in);
while (guess != secret) { Create outsi e the loo !!
System.out.print("Enter a number: ");
guess = keyboard.nextInt();
if (guess < secret) {
System.out.print("Higher! ");
}
if (guess > secret) {
System.out.print("Lower! ");
} // if
} // while
System.out.print("Congratulations,
your guess is correct!");
keyboard.close();
Release system resources
- 47 -
HigherLower: complete source code (5)
import java.util.Scanner; Sca r ot t he J or .
public class HigherLowerApp { It e s e m te .
public static void main(String[] args) {
int guess = 0; Put in main etho of
int secret = 56; class to ut .
Scanner keyboard = new Scanner(System.in);
while (guess != secret) {
System.out.print("Enter a number: ");
guess = keyboard.nextInt();
if (guess < secret) {
System.out.print("Higher! ");
}
if (guess > secret) {
System.out.print("Lower! ");
} // if
} // while
System.out.print("Congratulations, your guess is correct");
keyboard.close();
} // main
} // class
- 48 -
while (true) : bad practice!
while (true) {
System.out.print("Enter a number: ");
guess = keyboard.nextInt();
if (guess == secret) {
System.out.print("Congratulations,
your guess is correct!");
break;
}
if (guess < secret) {
System.out.print("Higher! ");
}
if (guess > secret) {
System.out.print("Lower! ");
} // if
} // while
- 49 -
Exercises
•Ex 01.08: HigherLower
– Put the if test in the while
condition
– Extend the Higher/lower
algorithm to show the
number of guesses used
- 50 -
Review
1. Programming Languages
2. The Java Platform
3. Your first Java program
4. Programming Algorithms
- 51 -