Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links

1. Loops

Write a program which calculates and outputs the sum of all the integers (inclusive) between 1 and 5. Solve the task using:

  • while loop
  • do ... while loop
  • for loop

Save each solution in a separate file.

2. Choose the loop

Choose an appropriate loop type for each task and write the program.

  1. Output integers from 1 to 5.
  2. Create 2 variables: sum and limit; then add value 2 to variable sum until sum becomes greater than limit.
  3. Output the following text using a loop: I like Java. The program has to print the message out at least once irrespectively of the loop conditions.

3. Input

Write a program that reads three edges for a triangle and determines whether the input is valid or not. (Hint: check the sum of the edges).

Example of the program output:

 Can edges 1, 2 and 10 form a triangle? False

Another example of the program output:

 Can edges 4, 5 and 6 form a triangle? True

4. Input with a loop

Write a program that reads an integer and checks whether it is even. The program must terminate if the user enters 0.

 Example of the program output:
 Is 25 an even number? False
 Is 100 an even number? True
 Is 0 an even number? I am done.

5. 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.

Upgrade the program so that the user is prompted for the range of the kilos (a starting value of the calculations, a step, an ending value of the calculation).

6. Sum of a series

Write a program that calculates and prints out the sum of the following series:

Upgrade the program so that the user is prompted for the number of the first terms to be summed.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment