Programming (Ü): First Programming Assignment Months Temperature

advertisement
Programming (Ü): First Programming Assignment
1. A simple rule to estimate your ideal body weight is to allow 50 kilogram for the first 150 centimeters of
height and 2.3 kilograms for each additional 2.5 centimeters. Assume the person is at least 1.5 meters tall.
Based on these values, write a program that calculates and outputs the ideal body weight for a person that
has a given height. [2.5 points]
2. The Fibonacci numbers Fn are defined as follows: F0 is 1, F1 is 1, and F i +2 = F i + F i +1, i = 0, 1, 2, . . . .
In other words, each number is the sum of the previous two numbers. The first few Fibonacci numbers are
1, 1, 2, 3, 5, and 8. Write a program to compute the first N Fibonacci numbers, where N can be given as a
parameter. What is your observation as N grows larger and larger? [5 points]
3. The table contains the average temperatures in Ingolstadt for each month. Write a program that defines
an array with the given temperature values and computes their standard deviation. [5 points]
Months Temperature
January
-0.5°C
February
0.4°C
March
4.4°C
April
7.8°C
May
12.9°C
June
15.8°C
July
17.8°C
August
17.6°C
September
13.5°C
October
8.6°C
November
3.5°C
December
0.9°C
4. The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points
wins. Players take turns. On each turn, a player rolls a six-sided die:
• If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn.
• If the player rolls 2 through 6, then he or she can either:
o ROLL AGAIN or
o HOLD. At this point, the sum of all rolls is added to the player’s score and it becomes the
other player’s turn.
Write a program that plays the game of Pig, where one player is a human and the other is the computer.
When it is the human’s turn, the program should show the score of both players and the previous roll.
(Hint for simulating the roll of a dice: use import java.util.Random; and declare first a variable
Random randomGenerator = new Random(); Each time you need to roll the dice, generate a
random number with a value between 0 and 5 using the following statement:
int r = randomGenerator.nextInt(5); Then you can use the value r+1 to represent the result.)
Allow the human to input “r” to roll again or “h” to hold.
(Hint for human input: use the import java.util.Scanner; and declare first a variable Scanner
scannerObject = new Scanner(System.in); and then another variable
String input = scannerObject.nextLine(); You can compare the input string with “r” with a
statement like if (input.equalsIgnoreCase("r"))).
The computer program should play according to the following rule:
• Keep rolling when it is the computer’s turn until it has accumulated 20 or more points, then hold.
• If the computer wins or rolls a 1, then the turn ends immediately.
Allow the human to roll first. [10 points]
Please send your answers by email to alexandros.nanopoulos@ku.de as a single pdf file that contains the
Java source code and a short description (in English or German).
Download