0% found this document useful (0 votes)
26 views51 pages

M1 Basic Concepts

Uploaded by

Justin Reynolds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views51 pages

M1 Basic Concepts

Uploaded by

Justin Reynolds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

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

• Machine Language (Binary Code)


0010101101101110110000101110111011000010110111011000010110111
0110000101101110110000101101110110000101101110110000101101110
11000010110… 1st generation: com uters use
• Assembly language processor s ecific inar code
MOV AX, 47104
2n eneration: more reada e
MOV DS, AX
POP [3998], 36 processor-s ecific co e
INT 32
3r eneration (3G ): processor
• 3rd Generation Languages (3GL)
in e en ent co e
Scanner scanner = new Scanner(System.in);
boolean niceWeather = scanner.nextBoolean();
if (niceWeather) {
System.out.println("Leave your umbrella at home!");
} else {
System.out.println("Don't forget your umbrella!");
}

-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

•No added benefit apparently, so why Java?


Characteristic C++ Java
Platform No YES!
independent

-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

Binary code Binary code


file Intel file ARM
01011101 11101100
11101001 01101011
Develop
(development platform)
Execute
(target platform)

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

The a a A (Ap ication Pro ramming


Interface: ja a ibraries) an Ja a Virtua
Machine hi e the un erl in lat orm: Java
pro rams are atform in epen ent.

- 12 -
Java applications
•Desktop applications
– Examples: LibreOffice, Minecraft, …
•Server applications

We’ll ocus on des to a plications for now. In t e years


to co e we’ll shift our ocus to ser er a lications…

- 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

•You can find all exercise instructions on the


Canvas Digital Learning Platform

- 15 -
IntelliJ: instant feedback...

li t u b: pro oses and o tional y


ap ies en ance ents

- 16 -
Quiz
•What do following terms mean?
•3GL
•compiling
•bytecode
•JVM
•JRE
•JDK
•API

- 17 -
Deege U Java lesson 002

Your first Java


program
Your first Java program: Steps
1. Write source code Source code
(MyProgram.java)
2. Compile
3. Run
Java
Compiler

Bytecode
(MyProgram.class)

Intel
Interpreter

Run on
Intel

- 19 -
1. Write source code
•File extension '.java' Source code
(MyProgram.java)

•Any text editor will do


– Suggestion: an IDE will assist you Java
when you write code Compiler

▪e.g. IntelliJ, Eclipse, Visual Studio


Code Bytecode
(MyProgram.class)

Intel
Interpreter

Run on
Intel

- 20 -
Code walkthrough 2.2

Comment: text bet een /* an */ or


precede y // is i nore y t e com iler
an can e used to ocument our co e
/* This Java application shows
the text 'Hello World!' on the screen. */
A package grou s sourcefiles
package hello;
(in su director hel o)
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Show the text
}
} The main met od: startin point of a
Java a lication

class definition. Classes are ey o ect


co e to be execute : this statement dis la s
oriented conce ts an wil be ex laine later
Hello World

- 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)

•So > javac hello/HelloWorldApp.java


> dir hello
HelloworldApp.java Intel
Interpreter
HelloworldApp.class

•The generated bytecode file has a Run on


'.class' extension Intel

- 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

command prompt (from the


parent folder of ‘hello’!) Bytecode
(MyProgram.class)
java hello.HelloWorldApp

•It should display “Hello world!” Intel


Interpreter
> java hello.HelloWorldApp
Hello world!

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

> run button Right click menu,


choose run - 24 -
Exercises
•Ex 01.02, your first program
– task 1
▪Create a Java project and add source code
– task 2
▪Compile and Run HelloWorldApp

- 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 pro uces output

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

•What is the algorithm for the following


program?

Enter a number: 15 user input


Enter another number: 35
Sum is: 50

Model the algorithm (draw)

- 31 -
Sum: algorithm

in ut an output (I/O)

Interme iate resu ts are


stored in aria es

Com ute usin varia les

- 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)

System.out.print("Enter a number: ");

first = keyboard.nextInt();

rea input rom the e boar

Assi nment (=): assi n nu ber entere


(ri t of =) to ariable first (left of =)

- 35 -
Sum: transform algorithm to Java (3)

System.out.print("Enter a number: ");

first = keyboard.nextInt();

System.out.print(
"Enter another number: ");
second = keyboard.nextInt();

sum = first + second;

System.out.print("The sum is: "


+ sum);
You can use + to concatenate
text too - 36 -
Sum: transform algorithm to Java (4)
int sum;
int first; Declare varia les and t e t e
int second; of ata the can ol efore use
System.out.print("Enter a number: ");
first = keyboard.nextInt();

System.out.print(
"Enter another number: ");
second = keyboard.nextInt();

sum = first + second;

System.out.print("The sum is: "


+ sum);
- 37 -
Sum: transform algorithm to Java (5)
int sum; Declare varia le keyboard.
int first; Assi n a e boar (System.in)
int second; rea er to it.
Scanner keyboard;
keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
first = keyboard.nextInt();
System.out.print(
"Enter another number: ");
second = keyboard.nextInt();

sum = first + second;

System.out.print("The sum is: "


+ sum);
- 38 -
Sum: complete source code (6)
package calculate;
Scanner is not art of the Ja a core.
import java.util.Scanner; It nee s to e im orte
/* Program that reads two numbers and displays the sum
*/
public class SumApp {
public static void main(String[] args) {
int sum; Put co e in t e main etho
int first;
int second;
of a c ass to execute
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
first = keyboard.nextInt();
System.out.print("Enter another number: ");
second = keyboard.nextInt();
sum = first + second;
System.out.print("The sum is: " + sum);
} // main()
} // class

- 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

test com arin varia les


- 42 -
How to transform the algorithm to Java
•Each step becomes a Java statement terminated
by a;
•display output: System.out.print("text");
•keyboard input: keyboard.nextInt();
•test: if (test) {
// if true statements
}
•loop while (test) {
// repeating statements
}

You’ll earn ore a out tests an loo s in


mo u e 3!

- 43 -
How to transform the algorithm to Java (1)
secret = 56;
System.out.print("Enter a number: ");
guess = keyboard.nextInt();
if (guess < secret) {

Com arin 2 values

Assi nment (=): value 56 (ri t of =) is


assi ne to varia le secret (left of =).

- 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;

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!");

Almost t ere… Just a itt e more or to set u the aria es.


- 46 -
How to transform the algorithm to Java (4)

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

•Ex 01.09 – Ex 01.12

- 50 -
Review
1. Programming Languages
2. The Java Platform
3. Your first Java program
4. Programming Algorithms

- 51 -

You might also like