Java for
Beginners
University
Greenwich
Computing At
School
DASCO
Chris
Coetzee
What do you learn last time?
Wordle.org
Levels of Java coding
• 1: Syntax, laws, variables, output
• 2: Input, calculations, String manipulation
• 3: Selection (IF-ELSE)
• 4: Iteration/Loops (FOR/WHILE)
• 5: Complex algorithms
• 6: Arrays
• 7: File management
• 8: Methods
• 9: Objects and classes
• 10: Graphical user interface elements
if(condition)
Condition is a logic check
something OPERATOR something
Example:
if(num == 3)
Logic operators in Java
Operator Function Example
== equals if(num==3)
(int, double, char, boolean)
.equals() equals if(name.equals(“Chris”) )
(String)
> greater than if(num>20)
< less than if(num<15)
>= greater than or equal to if(age>=18)
<= less than or equal to if(age<=12)
!= not equal to if(married!=true)
Warning! = does not mean ==
AND/OR
• AND in Java: &&
• OR in Java: ||
• Used between conditions
if(num>3&&<12)
if(num>3)&&(num<12)
Types of iteration (loops)
Iteration
Unspecified number of
Fixed number of times
times
FOR loop WHILE loop
Can you predict the output?
A logic condition (like in IF) that has
to be true for the loop to continue.
Something Operator Something
A simple variable (usually an int) that
(usually involving the counter
allows the loop to ‘step through’
variable)
Normally called “i”
for(counter; condition; change)
What should happen to the counter
variable value at the end of the loop
Usually i++ or i--
Typical example (FOR loop)
Create int called i At end of for loop, increase i by
Continue while i is less than 3
Set i to 0 1
for(int i = 0; i<3; i++)
{
System.out.println(“X”);
}
X
Output X
X
Another example (FOR loop)
Create int called i At end of for loop, increase i by
Continue while i is less than 5
Set i to 0 1
for(int i = 0; i<5; i++)
{
System.out.println(i);
}
0
1
Output
2
3
Predict the outcome (FOR loop)
What is the counter and starting
What is the condition? What change happens?
value?
for(int j = 2; j<10; j=j+2)
{
System.out.println(j);
}
2
4
Output
6
8
Students struggle with…
EVERYTHING ABOUT LOOPS
Loops are an abstract construct. Lower
ability students will need a LOT of simple
practice.
Most common mistake:
for(int i = 0; i<5; i++);
FOR example
Can you spot the
mistake?
Enter your favourite word >
Cheese
Here is Cheese 3 times!
Cheese
Output
Cheese
Cheese
Cheese
FOR with .charAt(x)
c
Output
?
t
m
Detour: random numbers
It can be very useful to make a random
number in program.
Java has a many ways to do this.
Most common way is Math.random()
Note: This is NOT an examinable bit, but
it does teach logical thinking which IS
examinable.
Making random numbers
Math.random() generates a random
double number between 0 and 1.
e.g. 0.34212… or 0.93813
To make random int numbers between
say 1 and 10 (both included), we need to
use a bit of maths.
Useful formula:
Min+(int)(Math.random()*((Max - Min) + 1))
Write down the minimum number and
maximum number you need, and use the
formula!
So let’s make numbers between 1 and 10:
1 +(int)(Math.random()*((10 - 1) + 1))
FOR with random ints (A)
17
17
Output
17
FOR with random ints (B)
19
7
Output
12
Example task (advanced)
Write a java program that will generate a
random letter of the alphabet 5 times.
Example output: g u l x e