Session 2 |
Tasks (to be submitted by Wen 20.02 12:55) 1 point
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)
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 argument is a double type value, the method returns its square which is rounded to integers;
- 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
The 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 Before1 and During1) - 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 present 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 harmonious mean of the teenagers' heights. The formal parameter of the method is an array of integers. Apply this method.
Next, choose one task (Task 3 or Task4) and solve it .
Task 3
- 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 some equal values. Print the initial values and short explanations on the screen.
Task 4
Create two arrays of integers - one 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 couples according to the following rules:
- if the number of boys and girls is equal, the children are coupled so that the highest boy dances with the highest girl and so on.
- if the number of boys is larger than the number of girls, the corresponding number of the highest 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 couples and uncoupled boys/girls (if there are children dancing alone).
An example of the program output:
Heights of boys: 180, 175, 200, 172, 169, 183, 188 Heights of girls: 165, 167, 172, 169, 162 Dancing couples: (169, 162) (172, 165) (175, 167) (180, 169) (183, 172) Uncoupled boys: 188, 200
Hint: an array can be sorted using method java.util.Arrays.sort(a)
where a
is an array of integers.
Session 2 |