Arvutiteaduse instituut
  1. Kursused
  2. 2019/20 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2019/20 kevad

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

Week 2 session exercises

1. Magic

Write a program that prompts the user for an integer and then performs the following operations with it:

  • adds to it the next largest integer, i.e. an integer that is larger than it by one;
  • adds 9 to the result;
  • divides the result by 2;
  • subtracts the original integer from the result.

Then the program outputs the number obtained.

Test your program with different numbers.

Do you notice anything special? How do you explain it?

2. Pizza price

Write a program that prompts the user for the diameter and price of the pizza. The diameter is in centimeters and the price is in euros. The program calculates the price of a square centimeter of the pizza in eurocents and prints the result.

 What is the diameter of your pizza? 20
 How many euros does it cost? 5
 One square cm of your pizza costs 1.5915494309189533 cents.

Hint. See Java API for class Math.

3. Speeding fines

If the driver exceeds the maximum speed limit, then a cautionary fine is imposed. According to the Traffic Act, the size of the fine in euros is calculated by multiplying the number of km/h-s above the speed limit by 3 (​see link). Maximum size of the cautionary fine is 190 euros.

Write a program that:

  1. prompts the user for name, speed limit and actual speed;
  2. computes the size of the fine, using the rules above;
  3. prints the name and the size of the fine on the screen.

Example

 Enter name: Anver
 Enter speed limit (km/h): ​60
 Enter actual speed (km/h): ​80
 Anver, your fine for exceeding the speed limit is 60 euros.

Explanation: actual speed was 80 − 60 = 20 km/h above the speed limit. Therefore, the size of the fine is 3 · 20 = 60 euros.

Second example

 Enter name: Sal-Saller
 Enter speed limit (km/h): ​50
 Enter actual speed (km/h): ​172
 Sal-Saller, your fine for exceeding the speed limit is 190 euros.

Explanation: the speed was 172 − 50 = 122 km/h above the speed limit. Preliminary calculation gives the size of the fine as 3 · 122 = 366 euros. Since this exceeds the maximum size of the fine, the fine will be 190 euros.

Hint: to take into account the maximum size of the fine, you can use the function Math.min. For example, if the variable res has a value larger than 190, then the value of Math.min(190, res) is 190.

4. Countries and currencies

To solve this task, you may want to look at the methods in the classes java.util.Locale and java.util.Currency.

a) Write a program that outputs the values of country, language and currency for the current instance of the Java Virtual Machine (i.e. you own computer).

 Country: Eesti
 Language: eesti
 Currency: EUR

b) Write a program that for the given language name and outputs all countries that use that language and the currencies that these countries use.

 Enter language: German
 Austria EUR
 Germany EUR
 Switzerland CHF
 Luxembourg EUR
 . . .

c) Write a program that for the given currency abbreviation finds all countries that use that currency.

 Enter currency: ALL
 Albania

5. Opinion of Java

Create a text file and write what are your first impressions of Java. What are the largest differences compared to Python? What of the material covered so far should be reviewed or explained again?

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused