Lecture 1
Lecture 1
2
Why programming?
• Programming is the core of Information
Technology and Computer Science
• Create websites, apps, games, robots…
• It is the art of logic
• Programmers are needed in the industry
3
What does a computers do?
• Performs calculations
– Billions of calculations per second!
• Remembers results
– Gigabytes, Terabytes of storage!
• Computers only do
– What you tell them
– They’re fast, but not creative
4
The Fetch-and-Execute cycle
program
instructions
program
data
5
Simple computer program
• Linear Equation Solver: you enter a, b and the
program shows you the solution for the equation:
ax + b = 0.
• You: start the program
• Program: “Please enter A: ”
• You: type 2 and press Enter
• Program: “Okay, I got it. Please enter B: ”
• You: type 1 and press Enter
(something happened inside the computer)
• Program: “The solution is X = -0.5”
6
Linear Equation Solver Program
8
Linear Equation Solver
Solve ax + b = 0
• Show text “Please enter number A: ”
• Wait for user to enter a number
• Store this number in variable A
• Show text: “Please enter number B: ”
• Wait for user to enter a number, Store in B
• Evaluate: A ≠ 0?
– If true:
• Calculate: X = -B/A
• Show text: “The solution is X = ”
• Show X’s value
– If false:
• Evaluate: B = 0?
– If true:
» Show text: “The equation has infinite number of solutions”
– If false:
» Show text: “The equation has no solution”
9
What is programming?
• Programming is
– Writing codes to do what would be manually done
otherwise.
– Telling a computer exactly what to do.
– Listing the little steps to achieve a goal.
• Programming is not
– Making something out of thin air.
– Making software to do what we don’t know how.
10
Cooking: Spring Rolls
• Step 1: Mince raw meat, onions, carrots, wood
ears and mix together.
• Step 2: Crack a few eggs and add to the
mixture.
• Step 3: Wrap the mixture in spring roll sheets.
• Step 4: Add vegetable oil to the frying pan.
• Step 5: Fry the spring rolls.
11
Cooking: Spring Rolls (messed-up)
• Step 1: Fry the spring rolls.
• Step 2: Add vegetable oil to a frying pan.
• Step 3: Mince raw meat, onions, carrots, wood
ears and mix together.
• Step 4: Wrap the mixture in spring roll sheets.
• Step 5: Crack a few eggs and add to the
mixture.
12
Algorithm
• An algorithm is the solution to a specific problem
• An algorithm consists of
– A set of simple steps
– A flow of control that specifies when each step is
executed
– A means of determining when to stop
13
What is Java?
• A programming language
• Appeared in 1995.
• Invented by James Gosling
(born 1955)
• Applications: Symbian apps,
Android apps, web servers,
websites, scientific apps, video
games…
• Write once, runs anywhere
14
Why Java?
• Huge community
– Lots of documents and tutorials.
• A neat, reasonably fast, practical language
– Logical, easy to learn
• Multi-purpose, cross-platform
• It’s a typical Object-Oriented Programming
language
• It is quite new
– JavaScript (1996), Python (1991)
– Perl (1987), C++ (1985), PHP (1995)
15
JDK, JRE, JVM
Class activity:
– What do JDK, JRE and JVM stand for?
– Describe JDK, JRE, JVM
• What kind of software is each of them?
• How do we use them?
– What is the latest Java version?
– Which Java versions are LTS?
16
Which JDK should I use?
• Oracle JDK (a.k.a Java SE, paid)
• OpenJDK
– Java SE’s community version
• Amazon Corretto
• Azul Zulu (paid support)
• AdoptOpenJDK
– HotSpot (recommended)
– OpenJ9
17
Which Java version to use?
• LTS versions (recommended)
– Java 8
– Java 11
– Java 17
• Latest version
– Java 18
– Java 19 (coming soon)
Java 8 Java 16
18
Prepare for Java programming
• Install IntelliJ IDEA Community Edition
• Create the first Java project in IntelliJ IDEA
• Download JDK for the project
• Add
<JDK_path/bin >
to the Path
environment
variable
19
How to test JDK?
• My Computer -> Properties -> Advanced
System Settings -> Environment Variables.
• Make sure JDK’s bin directory is in the user
variable or system variable named "Path“
• Open CMD, type javac
20
Hello World program
Source code of HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output:
Hello World
21
Program development cycle
binary
Compile executable
Text, Graphics,
Run Audio, Data…
22
From Java source code to program
23
What can go wrong?
• Syntax errors
– Codes do not follow language rules
– Cannot compile
• Semantic errors
– Codes follow language rules but don’t make sense
– Can compile but gives error when run
• Different meaning than what is intended
– Not following algorithm
– Program gives unexpected answer
24
Compile & Run with CMD
29
Structure of the first program
30
31
Live instructions: IntelliJ Idea
32
Live instructions: Eclipse
33
Demo exercise 1
• Write a program to calculate the
sum/product/quotient of two integers and
display the result.
34
Demo exercise 2
• Write a program to display the following shape:
*
* *
* * *
* * * *
* * * * *
35
Download Learning Materials
36
Reading Resources
• R. Sedgewick - Introduction to Programming in Java
– Online: https://introcs.cs.princeton.edu/java/home/
• Oracle’s Java Tutorials:
– https://docs.oracle.com/javase/tutorial/
• W3Schools Java Tutorial:
– https://www.w3schools.com/java/default.asp
37