# Java exercises
:::info
All this programs are one class only called Main so they can be executed in w3schools tryit editor:
https://www.w3schools.com/java/tryjava.asp?filename=demo_helloworld
:::
The base of these programs is this:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J2_Basics.html
## Exercise about dataflow (if/else)
Write the output of this code:
```java=
double temperature = 80.1;
if (temperature > 80) {
System.out.println("Too Hot!");
} else {
System.out.println("Too Cold!");
}
System.out.println("yummy!");
```
Solution
:::spoiler
Too Hot!
yummy!
:::
Write the output of this code:
```java=
int markers = 30;
if (markers < 30) {
System.out.println("We have to buy a lot more markers!");
markers += 20;
} else if (markers <= 40) {
System.out.println("We have to buy some more markers!");
markers += 5;
}
else {
System.out.println("I have enough markers");
}
System.out.println("I have these markers:" + markers);
```
Solution
:::spoiler
We have to buy some more markers!
I have these markers:35
:::
## iterations (for and while loops)
One other concept that we should have clear is loops
### Variations of for loop
LOOP 1
Write the output of this code:
```java=
int total = 0
for (int x = 0; x<10; x++) {
total++;
}
System.out.println(total);
```
Solution
:::spoiler
10
(number of loops done)
:::
LOOP 2
Write the output of this code:
```java=
int total = 0
for (int x = 0; x<6; x++) {
total=total+x;
}
System.out.println(total);
```
Solution
:::spoiler
15
(0+1+2+3+4+5)
:::
LOOP 3
Write the output of this code:
```java=
int total = 0
for (int x = 1; x<10; x= x+2) {
total=total+x;
}
System.out.println(total);
```
Solution
:::spoiler
25
(1+3+5+7+9)
:::
LOOP 4
Write the output of this code:
```java=
int total = 0
for (int x = 1; x<10; x= x+2) {
total++;
}
System.out.println(total);
```
Solution
:::spoiler
5
(five times the loop has been executed)
:::
LOOP 5
Write the output of this code:
```java=
int total = 0
for (int x = 1; x<8; x= x+2) {
System.out.println(total);
}
```
Solution
:::spoiler
0
0
0
0
(four times the loop has been executed)
:::
LOOP 6
Now
Construct a code that sums all the numbers from 1 to 1000.
Construct a code that outputs a count from 10 to 0 and then prints "lift off!"
Construct a code that outputs the sum of all numbers divisible by 23 from 0 to 10000
## Other example codes
Here we have a program that adds all the numbers from 1 to 1000 that are divisible by 3, 5 and 7
```java=
public class Main {
public static void main(String[] args) {
final int lowerLimit = 1;
final int upperLimit = 1000;
int sum = 0; // For accumulating odd numbers, init to 0
int number = lowerLimit;
while (number <= upperLimit) {
// divisible by 3, 5 or 7, but NOT by 15, 21, 35 or 105.
if ( (number % 3 == 0) || number % 5 == 0 || number % 7 == 0 ) {
if ( number % 15 != 0 && number % 21 != 0 && number % 35 != 0 && number % 105 != 0)
{
sum += number;
}
}
number++; // Next number
}
// Print the result
System.out.println("The sum of these numbers from " + lowerLimit + " to " + upperLimit + " is " + sum);
}
}
```
```java
public class Main {
public static void main(String[] args) {
final int limiteInferior = 999;
final int limiteSuperior = 2010;
int counter = 0;
int number = limiteInferior;
while (number <= limiteSuperior) {
if ( number % 4 == 0)
{
counter++;
System.out.println(number + "AD");
}
number++; // Next number
}
// Print the result
System.out.println("Total number of leap years " + counter);
}
}
```