Print out the multiplication table for 1 to 10

degree of difficulty: 3

Implement a Java-main-method that prints out the multiplication table for all numbers from 1 to 10. Use the tabulator character '\t' to align the values. The output of your method should be as follows:

1	2	3	4	5	6	7	8	9	10	
2	4	6	8	10	12	14	16	18	20	
3	6	9	12	15	18	21	24	27	30	
4	8	12	16	20	24	28	32	36	40	
5	10	15	20	25	30	35	40	45	50	
6	12	18	24	30	36	42	48	54	60	
7	14	21	28	35	42	49	56	63	70	
8	16	24	32	40	48	56	64	72	80	
9	18	27	36	45	54	63	72	81	90	
10	20	30	40	50	60	70	80	90	100	

Solution

Sort three numbers

degree of difficulty: 3

Implement a Java-method with three local int variables a, b, and c that sorts these three values in ascending order by comparing and exchanging their values. At the end of the program a <= b <= c must hold.

You can solve this exercises with three if in minimum. Test your method with all six permutation of three different numbers.

Solution

Calculate (german) grades for an examination

degree of difficulty: 3

The maximum points you can get in the written exam to my lecture is 120. At 60 points you have passed the exam (grade 4.0). For every additional 5 points the next better grade. Because of juridictional problems, the grade 4.3 does not exist (it is 4,7, too, in that case). It is possible to get 0.5 points for your solutions in the exam. The following table shows the mapping between points and their grades:

gradepoints
5.00 - 49.5
4.750 - 59.5
4.060 - 64.5
3.765 - 69.5
3.370 - 74.5
3.075 - 79.5
2.780 - 84.5
2.385 - 89.5
2.090 - 94.5
1.795 - 99.5
1.3100 - 104.5
1.0105 - 120

Implement a Java-function that returns the grade for the points reached in the exam and prints out the grade for all points from 0 to 120 (in steps of 0.5).

Solution

Find the day of the week of a given date

degree of difficulty: 2

Implement a Java-method that prints out the day of the week for a given day (1..31), month (1..12) and year.

The day of the week of dates between March 1900 and February 2100 can be calculated as follows:

First, you have to calculate the total number of days from 1900/1/1 to the given date (see below, for details). Secondly, you divide this number by 7 with integer remainder: this now is the day of the week, with 0 as sunday, 1 as monday, etc.

To calculate the total number of days you have to implement the following steps:

Here some dates with the day of the week for testing your Java programm:

Solution

Winning strategy for Roulette

degree of difficulty 2

A foolproof winning strategy for roulette is to always bet on the same color: If you loose your wager, then you simply double the bet to compensate the loss. The probability for red (or black) is 50% (we ignore the existence of zero).

Implement a Java method, that calculates how much money you approximatly need for playing 1 000 000 times with 1 Euro as the first bet.

Use the expression Math.random() < 0.5 for checking the probability.

How much money do you approximatly need, without loosing it during gambling.

Extend your Java method, such that it calculates from a given seed money the number of rounds you need to play to loose all your money.

Solution

Convert a given number of bytes into a representation with metric units

degree of difficulty 3

A number of bytes given as a int value should be printed out with at most three digits before the decimal comma. The ouput for four different values:

123 Byte are 123.0 Byte
15323 Byte are 15.323 KByte
15323000 Byte are 15.323 MByte
1532300001 Byte are 1.532300001 GByte

Do not use iterations (only if-else).

Solution