Chapter 2 |
Tasks (to be submitted by Mon 24.02)
Task 1
Rewrite the following Python program in Java:
grade = int(input('Enter course points: ')) if grade < 50: print('F') elif grade < 60: print('E') elif grade < 70: print('D') elif grade < 80: print('C') elif grade < 90: print('B') else: print('A') for i in range(100, -1, -5): print(i)
Then add to the program three static methods with the same name but different signatures. All methods must return something. For example,
- if the arguments are two integers, the method returns their sum;
- if the only argument is a double type value, the method returns its square rounded to an integer;
- if the arguments are string and int type values, the method does not return anything, but prints out the string as many times as it is stated in the integer type argument (in such case a loop is needed);
Invoke the methods in the main method. Add signatures of the methods into the program comments.
Task 2
A water park has certain restrictions. The smallest children (60-100 cm) can go only to small children's attractions. The medium children (101-140 cm) can go to children's attractions. The teenagers (141-200 cm) can go to adult attractions.
- Create a method with two parameters: the lowest height and the highest height. The method has to return a randomly generated integer from that range (
Math.random()
). (Hint: check last week's material). - Create a method with three parameters: the number of children, the lowest height and the highest height. In the method, create an array of integers that contains children's height generated by the previous method. The return type of the method should be
int[]
. - Invoke the last method in the main method to create a 10-element array for smallest children, a 15-element array for medium height children, and a 20-element array for teenagers.
- In the main method, output all the elements of each array using loops.
- Use two loops (for and for-each) and if statement to output (twice) the elements from the first array if the element value is greater than 80 cm.
- Create a method that calculates the average of the teenagers' heights. The formal parameter of the method is an array of integers. Apply this method.
Next, choose one task (either Task 3 or Task 4) and solve it.
Task 3
First, create two methods.
- Merge two arrays.
- Invert an array
Create the main method to test both methods mentioned above. Test the first method using arrays of different sizes and with some equal values. Print the initial values and short explanations on the screen.
Task 4
Create two arrays of integers - an array of boys' heights and an array of girls' heights (the heights are in cm). Add elements into both arrays (check an example at the end of the task). The arrays do not have to be sorted.
Write a program which puts the boys and girls into dancing pairs according to the following rules:
- If the number of boys and girls is equal, then they are paired so that the tallest boy dances with the tallest girl, the second tallest boy with the second tallest girl and so on.
- If the number of boys is larger than the number of girls, the corresponding number of the tallest boys have to dance alone; the same rule is for girls if the number of girls is larger than the number of boys.
Output the initial data (the heights of the boys and the girls in the initial order), heights of the dancing pairs and boys/girls who were left alone (if they exist).
Example of program output:
Heights of boys: 180, 175, 200, 172, 169, 183, 188 Heights of girls: 165, 167, 172, 169, 162 Dancing pairs: (169, 162) (172, 165) (175, 167) (180, 169) (183, 172) Unpaired boys: 188, 200
Hint: an array can be sorted using the method java.util.Arrays.sort(a)
where a
is an array of integers.
Chapter 2 |