During session 1
Tasks for 1 point
Once you have submitted the assignment, please proceed with the following tasks.
These tasks help you practise and master your Java skills. If you want your solutions to be checked, please show them to the instructor during the session or send to the instructor by email.
1. Loops
Write a program which calculates and outputs the sum of all the integers between 1 and 5 (inclusive). Solve the task using:
- while loop
- do ... while loop
- for loop
If the task is solved correctly, you will have three separate files.
2. Input
Write a program that gets three numbers as main method arguments.The program has to check if these numbers can be the edges of a triangle. (Hint: check the sum of the edges).
Example of the program output:
Numbers 10, 15 and 30 cannot be edges of a triangle.
Example of the program output:
Numbers 10, 15 and 10 can be edges of a triangle.
3. Input with a loop
Write a program that constantly asks the user for an integer (use Scanner) and checks whether it is even. The program has to terminate if the user enters 0.
Example of the program output: Enter a number: 25 Odd Enter a number: 100 Even Enter a number: 0 I am done.
4. Kilos and Pounds
Write a program that displays the following table (note: 1 kg = 2.2 pounds; if you want to be more precise 1 kg = 2.20462262185 pounds). The output of the program should look like this:
Kg Pounds 1 2.2 3 6.6 ... 197 433.4 199 437.8
To output the result as a table, format the output. To create columns for the table, use tab symbol \t
. For example, System.out.println("abc"+"\t"+"def")
; in such case string def will be allocated into a new column. To format floating point numbers, use System.out.printf
. For example, System.out.printf("%.1f",1234.45)
will display double numbers with one digit after the comma.
Modify the program so that the user is prompted for the range of the kilos (a starting value of the calculations, a step, a stopping value of the calculations).
5. Sum of a series
Write a program that calculates and prints out the sum of the following series:
Modify the program so that the user is prompted for the number of the first terms to be summed.