This is a part of preparation for Paper 2 in IB Computer Science diploma. The lessons are
Java is a High level programming language. By high level we refer high level of abstraction. In this case the abstraction is that doesn't care about the machin is made to.
It's a compiled then run programming language.
It compiles into a "bitcode" program that is going to be run by a Virtual Machine (Java Virtual Machine, to be specific)
Virtual machine:
Is software that emulates a type of machine. That machine that it's emulated can also have their own OS (or maybe not). Examples of Virtual Machines are JVM but also old consoles emulators.
Also these VM can be accessed sometimes online. example of when I used VM for the TSB
The purpose of the JVM
The Java Virtual Machine is a software that interpret the bytecode compiled from a java IDE. This software is different for different OS and devices. But the idea is the same program would run exactly the same in both OS using this layer of the VM
Quick paint and mouse used here. Still better than some AIs
Their motto is "compile once run everywhere"
One of the main characteristics of Java is that is a highly typed programming language (unlike python or javascript)
This means that every time that we declare a variable we need to define also its type.
When we declare a variable the word that it's just before it is the type.
Also statements ends with a semicolon (";")
The entry point of any java app is the Public static void Main function (method). Whatever is inside of it will be executed.
So the most basic program in java would be this one:
So a simple program would be like this:
In java comments are written with //. If you want to do several lines comment you can do /*
and finish with */
We're not going to deal with all the details but for outputing we're going to use the console with System.out.println(stringToBeOutput);
In Java the structure of an if is similar to pseudocode and C++. You put the condition in parethesis and what happens is between curly braces.
We also have if/else and also if/ else if
While loop is while is similar to all the whiles in other programming languages and pseudocode. Evaluates a certain condition and while this condition is met it repeats the block of code inside of it.
Exercise:
With this information use a while loop to add all numbers from 1 to 100 and output it:
Credit: C.R.
The result should be 5050
In Java the for loop has 3 statements inside
The first statement is the iteration variable declaration (usually int i = 0). This is a local variable whose scope is going to be just the for loop.
The second statement is the staying condition. It's a condition that needs to be met in order to continue in the loop. (usually i < than certain threshold but varies)
The third statement is the step. This is executed at the end of each iteration (each loop) and usually is dedicated to modify the iteration variable. The most common is add one to the variable or substract
x++ is the same as writing x +=1 and it's the same as writing x = x +1
Exercise:
With this information use a for loop to add all numbers from 1 to 100 and output it:
solution in the spoiler:
Credit P.H.F.
The result should be (still) 5050
Reverse count
I want a program whose output is a reverse count from 20 to 0. After that it's going to say "LIFT OFF!"
Solutions in the spoiler:
Credit D.Z.
I want a program that outputs all odd numbers that are multiples of 7 from 0 to 200.
Modulus operator
Here you can use the modulus operator to specify if a number has a certain remainder. To do that you use %. For example if we write
Solution in the spoiler
Output the 20 first Fibonacci numbers.
The first one is 1, the second is 1, then rest are the sum of the 2 previous.
HL you can use an iterative way or you may try to use a recursive function to do it.
Solution in the spoiler
(a bit complex)
Print all numbers from 0 to 1000 whose figures add 12.
Hint Use modulus 10 for separate figures.
Solution in the spoiler
Credit P.H.F.
We need to state the type of the variable.
https://www.w3schools.com/java/default.asp
Once you have grasped the concepts of java we can jump to The concept of Object in OOP