User:Kelti/sandbox/Java

'''A temporary page for a Java course. Send your questions to: juergen@purtz.de'''

Program Structure; Generate Output
1. Create a class  which prints the text:

2. Create the output: ++ ++

3. Create the output: 1 2 3 ... 8 9

Conditions
Use this program as the basis for the following exercises.

1. Change the value of x. 2. Change the condition to check x: equal (==), greater (>), smaller-or-equal (<=), not-equal (!=) 3. Test the value of x and y with the operator and (&&): 4. Test the value of x and y with the operator or (||) 5. Test the value of x and y with the operator not (!=) 6. Create an exercise which combines 3, 4, and 5.

More examples:

Loops
1. Create the output: 1 2 3 ... 98 99

2. Create the output: 1, 3, 5, 7, ..., 97, 99 3. Create the output: 10, 9, 8, ..., 3, 2, 1

4. Create the output: 15, 10, 5, 0, -5, -10, -15

5. Compute the sum from 1 to 5 (= 15). 6. Compute the sum from 1 to 100 (= 5050).

Where is the error?
{{ExerciseSolution|width=100%|text= The class misses the entry point: public static void main(String[] args) { This entry point is (nearly) always necessary! }}

{{ExerciseSolution|width=100%|text= The class misses one closing 'curly bracket': }

}}

Nested Loops
The following exercises use two for-loops, which are nested in each other: a loop within another loop.

1. Create the output (both loops run from 1 to 3):

1 / 1 1 / 2 1 / 3 2 / 1 2 / 2 2 / 3 3 / 1 3 / 2 3 / 3

2. Create the output: 0 1 2 3 4 5 6 7 8 9 10 11 12 ... 17 18 19 20 21 22 ... 27 28 29 ...             ... ...              ... 90 91 92 ... 97 98 99

3. Create the output (3 nested loops, one per column): 0 0  0 0  0  1 0  1  0 0  1  1 1  0  0 1  0  1 1  1  0 1  1  1 4. Create the output: x xx xxx xxxx xxxxx

5. Create the output: x x   x      x    x  x x

6. Create the output (three for-loops): x x   x      x    x  x x x  x    x      x    x  x x x  x    x      x    x  x x

It's Quiz Time I
Which value has the variable  at the end of the code snippet?

Which value has the variable  at the end of the code snippet?

Which value has the variable  at the end of the code snippet?

Loops plus Conditions
1. Create a chessboard: It's a 8 x 8 square with black and white positions like this: xx xx  xx  xx xx  xx  xx  xx  xx  xx  xx  xx xx  xx  xx  xx  xx  xx  xx  xx xx  xx  xx  xx  xx  xx  xx  xx xx  xx  xx  xx

2. Modify the board to a 12 x 12 square. In an optimal situation this program differs only in 1 position from the previous program: the value for the size changes from 8 to 12 - all other statements keep unchanged.

3. Create a triangle like this. There are many different possiblities: two loops from 1 to 9 each, one loop from 11 to 99 plus some conditions, ... . 11 21 22 31 32 33 ... 91 92 93 94 95 96 97 98 99

4. Create diagonal-lines like this (A): o o          o           o           o           o

or this (B) o        o            o          o        o      o

or this (C) o    o      o   o           o o            o          o o       o   o   o     o

or this (D) o o      o  o      o  o      o  o      o  o      o  o      o  o      o  o         o         o         o

5. What will the following code snippet print out?

Arrays
Create a class with 5 variables to hold some student names. For each of the students print the output:


 * Hello.
 * How are you today?
 * What have you done during the weekend?
 * , I wish you much success.

Class:
 * 1) Create an array which holds 5   values. Fill the array with some values, e.g. the age of students.
 * 2) Show the elements of the array - one line per element.
 * 3) Compute the average age of the students.
 * 4) Show the age of the jungest student.

Class:


 * 1) Create an array to store values of data type.
 * 2) Create random numbers with the command   and store them in the array.
 * 3) Show all created random numbers - they will be in the range from 0 to 1.
 * 4) Show only those random numbers, which are between 0.5 and 0.6.

Class:


 * 1) Create an array to store values of data type.
 * 2) Create random numbers with the command   and store them in the array.
 * 3) Show all created random numbers - they will be in the range from 0 to 1.
 * 4) Compute the average of the random numbers. Which value do you expect (statistics: expected value)?
 * 5) Run the program several times.
 * 6) Use a greater array (more random numbers, e.g.: 5, 50, 500, 5.000, 5 million). The average shall be closer to the expected value.