Primitive types challenge

We now propose some (very easy) exercises without solutions.

Try executing them in Netbeans, and JavaTutor to get familiar with all environments.

Exercise - cycling

✪ Write a program that given three variables with numebers a,b,c, cycles the values, that is, puts the value of a in b, the value of b in c, and the value of c in a .

So if you begin like this:

int a = 4;
int b = 7;
int c = 9;

After the code that you will write, by running this:


You should see:


There are various ways to do it, try to use only one temporary variable and be careful not to lose values !

HINT: to help yourself, try to write down in comments the state of the memory, and think which command to do

// a b c t    which command do I need?
// 4 7 9
// 4 7 9 7    t = b

Exercise - deadline 1

✪ You are given a very important deadline in:

Write some code that prints the total minutes. By executing it, it should result:

In total there are 6592 minutes left.

Exercise - deadline 2

✪ For another super important deadline there are left:

int totMinutes = 5000;

Write some code that prints:

There are left:
   3 days
   11 hours
   20 minutes

QUESTION: given two numbers a and b, which of the following expressions are equivalent?

1. Math.max(a,b)
2. Math.max(Math.min(a,b),b)
3. -Math.min(-a,-b)
4. -Math.max(-a,-b)


Exercise - armchairs

✪✪ The tycoon De Industrionis owns two factories of armchairs, one in Belluno city and one in Rovigo. To make an armchair three main components are needed: a mattress, a seatback and a cover. Each factory produces all required components, taking a certain time to produce each component:

Belluno takes 23h to produce a mattress, 54h the seatcback and 12h the cover. Rovigo, respectively, takes 13, 37 and 24 hours. When the 3 components are ready, assembling them in the finished armchair requires one hour.

Sometimes peculiar requests are made by filthy rich nobles, that pretends to be shipped in a few hours armchairs with extravagant like seatback in solid platinum and other nonsense.

If the two factories start producting the components at the same time, De Industrionis wants to know in how much time the first armchair will be produced. Write some code to calculate that number.

Example 1 - given:

int b_mat=23, b_bac=54, b_cov=12, r_mat=13, r_bac=37, r_cov=24;

your code must print:

The first armchair will be produced in 38 hours.

Example 2 - given:

int b_mat=81, b_bac=37, b_cov=32, r_mat=54, r_bac=36, r_cov=91;

your code must print:

The first armchair will be produced in 82 hours.

Exercise - circle

✪ Calculate the area of a circle at the center of a soccer ball (radius = 9.1m), remember that $area= pi*r^2$

Your code should print as result 263.02199094102605

Exercise - fractioning

✪ Write some code to calculate the value of the following formula for x = 0.000003, you should obtain 2.753278226511882

$$\Large -\frac{\sqrt{x+3}}{\frac{(x + 2)^3}{\log{x}}}$$

Exercise - summation

Write some code to calculate the value of the following expression (don't use cycles, write down all calculations), you should obtain 20.53333333333333

$$\normalsize \sum_{j=1}^{3}{\frac{j^4}{j + 2}}$$

Reals - conversion

If we want to convert a real to an integer, several ways are available:

Function Description Mathematical symbol Result
Math.floor(x) round x to inferior double $$\lfloor{8.7}\rfloor$$ 8.0
(int) x round x to inferior integer $$\lfloor{8.7}\rfloor$$ 8
Math.ceil(x) round x to superior double $$\lceil{5.3}\rceil$$ 6.0
Math.round(x) round x to closest integer $$\lfloor{2.4}\rceil$$ 2
$$\lfloor{2.51}\rceil$$ 3

QUESTION: Look at the following expressions, and for each of them try to guess which result it produces (or if it gives an error).

  1. Math.floor(2.3)
  2. Math.floor(-2.3)
  3. Math.round(3.49)
  4. Math.round(3.51)
  5. Math.round(-3.49)
  6. Math.round(-3.51)
  7. Math.math.ceil(8.1)
  8. Math.math.ceil(-8.1)

QUESTION: Given a float x, the following formula is:

(int) Math.floor(Math.ceil(x)) == (int) Math.ceil(Math.floor(x))
  1. always true
  2. always false
  3. sometimes true and sometimes false (give examples)


QUESTION: Given a float x, the following formula is:

(int) Math.floor(x) == (int) -Math.ceil(-x)
  1. always true
  2. always false
  3. sometimes true and sometimes false (give examples)


Exercise - Invigorate

✪ Excessive studies lead you search on internet recipes of energetic drinks. Luckily, a guru of nutrition just posted on her Instagram channel @HealthyDrink this recipe of a miracle drink:

Pour in a mixer 2 decilitres of kiwi juice, 4 decilitres of soy sauce, and 3 decilitres of shampoo of karitè bio. Mix vigorously and then pour half drink into a glass. Fill the glass until the superior deciliter. Swallow in one shot.

You run shopping the ingredients, and get ready for mixing them. You have a measuring cup with which you transfer the precious fluids, one by one. While transfering, you always pour a little bit more than necessary (but never more than 1 decilitre), and for each ingredient you then remove the excess.

Example - given:

double kiwi = 2.4;
double soy = 4.8;
double shampoo = 3.1;
double measuring_cup = 0.0;
double mixer = 0.0;
double glass = 0.0;

Your code must print:

I pour into the measuring cup 2.4 dl of kiwi juice, then I remove excess until keeping 2 dl
I transfer into the mixer, now it contains 2.0 dl
I pour into the measuring cup 4.8 dl of soia, then I remove excess until keeping 4 dl
I transfer into the mixer, now it contains 6.0 dl
I pour into the measuring cup 3.1 dl of shampoo, then I remove excess until keeping 3 dl
I transfer into the mixer, now it contains 9.0 dl
I pour half of the mix ( 4.5 dl ) into the glass
I fill the glass until superior deciliter, now it contains: 5 dl

Exercise - Triangle area

Compute the area of a triangle having base 120 units (b) and height 33 (h). Assign the result to a variable named area and print it. Your code should show Triangle area is: 120.0

Exercise - square area

Compute the area of a square having side (s) equal to 145 units. Assign the result to a variable named area and print it, it should show Square area is: 21025

Exercise - area from input

Modify the program at previous point to acquire the side s from the user at runtime.

HINT: use the Scanner object and remember to convert the acquired value into an int with Integer.parseInt)).

Try also to put the two previous scripts in two separate files (e.g. and and execute them from the terminal)

Exercise - trapezoid

Write a small script ( that computes the area of a trapezoid having major base (mj) equal to 30 units, minor base (mn) equal to 12 and height (h) equal to 17. Print the resulting area. Try executing the script from a text editor like Netbeans and from the terminal.

It should print Trapezoid area is: 357.0

Exercise - first n numbers

It should show Sum of first 1200 integers: 720600

Exercise - hypotenuse

Write a small script to compute the length of the hypotenuse (c) of a right triangle having sides a=133 and b=72 units (see picture below). It should print Hypotenuse: 151.23822268196622

triangle 9u349y43

Exercise - airport

You finally decide to take a vacation and you go to the airport, but you already know you will need to go through various queues. Luckily, you only have carry-on bag, so you directly go to security checks, where you can choose among three rows of people sec1, sec2, sec3. Each person an average takes 4 minutes to be examinated, you included, and obviously you choose the shortest queue. Afterwards you go to the gate, where you find two queues of ga1 and ga2 people, and you know that each person you included an average takes 20 seconds to pass: again you choose the shortes queue. Luckily the aircraft is next to the gate so you can directly choose whether to board at the queue at the head of the aircraft with bo1 people or at the queue at the tail of the plane with bo2 people. Each passenger you included takes an average 30 seconds, and you choose the shortest queue.

Write some code to calculate how much time you take in total to enter the plane, showing it in minutes and seconds.

Example - given:

int sec1=4,sec2=5,sec3=8,ga1=5,ga2=2,bo1=7,bo2=6;

your code must print:

24 minutes and 30 seconds