Session 2 |
Tasks (to be submitted by Sun 25.02 23:55) 2 points
Task 1
- Create a new project called Before2 in IntelliJ (if needed, look for help here).
- Create variables for integers:
a
,b
,c
,d
,b
,e
. - Assign variable
a
value2147483647
. Assign the rest of the variable any integers. - Sum, subtract, multiply, divide variables and find the reminder.
- What is the result if variable
a
is added value 1? Explain the answer in the program comment. - What is the result of the statement
System.out.println(1-0.9);
? Explain the answer in the program comment.
Hint: you can output the result of calculations using the following statement:
System.out.println("Sum of numbers " + b + " and " + c + " is " + (b + c));
What will change if the parenthesis are removed?
Task 2.
Write a program that contains 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 return 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 3.
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 the 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 the smallest children, a 15-element array for the medium height children, and a 20-element array for the 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 4 or Task5) and solve it .
Task 4.
- 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 5.
Create two arrays of integers - an array for boys' heights and an array of girls' heights (the heights are given in cm). 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.
In the main method, output the initial data (the heights of boys and 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 |