Tracks
/
Crystal
Crystal
/
Exercises
/
Wellington's Weather Station
Wellington's Weather Station

Wellington's Weather Station

Learning Exercise

Introduction

Crystal has a type, Number, which is the base type for all numeric types. There are two main sub-types inside the Number type: Int and Float.

Int is short for integer and represents whole numbers. Int can be both negative and positive, with no decimal point.

1.class
# => Int32

-1.class
# => Int32

Float represents numbers with a decimal point. Floats can be both negative and positive.

10.5.class
# => Float64

Converting between Int and Float

Using the 'to_i' and' to_f' methods, you can convert between Int and Float. The number is rounded down when converting from Float to Int.

1.to_f
# => 1.0

1.0.to_i
# => 1

1.9.to_i
# => 1

Arithmetic operators

You can use the basic arithmetic operators on Int and Float. The operators are +, -, *, /, and %. When using these operators, you can mix and match Int and Float.

Addition & Subtraction & Multiplication

The + operator is used for addition, the - operator is used for subtraction, and the * operator is used for multiplication.

Operator Example
+ 4 + 6 => 10
- 15 - 10 => 5
* 2 * 3 => 6

Division

Division is used to divide numbers. The / operator is used for division. The result will always be a Float.

4 / 2
# => 2.0

4.0 / 2
# => 2.0
Caution

In some programming languages, when dividing by zero, the result will be an error.

In Crystal, when dividing by zero, the result is Infinity or -Infinity. The only exception is dividing zero by zero, resulting in NaN (Not a Number). Infinity and NaN are particular values in the Float type.

1 / 0
# => Infinity

-1 / 0
# => -Infinity

0 / 0
# => NaN

Modulus

Modulus is used to get the remainder of a division. The % operator is used for modulus.

5 % 2
# => 1

5.0 % 2
# => 1.0

5 % 3
# => 2

-7 % 5
# => 3
Caution

Dividing by zero when using modulo results in a DivisionByZeroError. This is different from normal division.

1 % 0
# Error: Unhandled exception: Division by 0 (DivisionByZeroError)

Note that unlike some other programming languages, % in Crystal calculates the modulus with floored division (for example, in JavaScript this would yield -1):

-6 % 5
# => 4

Rounding

The round method takes an optional argument: the number of decimal places to round to. The default number of decimal places is 0.

1.0.round
# => 1

1.5.round
# => 2

1.234.round(2)
# => 1.23

Priority and parentheses

Crystal allows parentheses(()), which can be used to group expressions. This is useful when you want to change the order of operations.

When using multiple arithmetic operators, the order of operations is the same as in mathematics, also known as PEMDAS. It follows the order of parentheses(()), exponents(**), multiplication(*) and division(/), and addition(+) and subtraction(-).

2 + 3 - 4 * 4
# => -11

(2 + 3 - 4) * 4
# => 4

Instructions

Wellington's weather station will replace its manual temperature recording system with a new digital one. In the old system, the calculations were performed manually. In the new system, they will be automated. The calculations include converting the temperature from Celsius to Fahrenheit and finding the average temperature. The new system has sensors to record temperature data in Celsius.

The Wellington weather station wants you to help them with the new system.

1. Convert the temperature from Celsius to Kelvin

The weather station has a section on its website that is specified for researchers. The researchers are interested in the temperature in Kelvin.

Kelvin is a unit used to measure temperature. It is on the same scale as Celsius but with a different starting point. Its starting point is absolute zero, which is 0K (Kelvin) or -273.15°C (Celsius).

Here is a table showing the conversion between Celsius and Kelvin:

Celsius(°C) Kelvin(K)
-273.15 0
-100 173.15
0 273.15
100 373.15

The weather station wants you to implement a method that converts a temperature from Celsius to Kelvin.

Implement the Temperature#to_kelvin method that takes an argument celsius and returns the temperature in Kelvin by adding 273.15 to the given celsius value.

Temperature.new.to_kelvin(0)
# => 273.15

2. Round the temperature

The weather station temperature sensor is exact. It can measure the temperature to the nearest 0.0001°C. The weather station only wants to display the temperature rounded to 1 decimal place since most users are not interested in that level of precision.

The weather station wants you to implement a method that rounds the temperature to 1 decimal place.

Implement the Temperature#round method that takes an argument celsius and returns the temperature rounded to 1 decimal place.

Temperature.new.round(10.1234)
# => 10.1

Temperature.new.round(10.1567)
# => 10.2

3. Convert the temperature from Celsius to Fahrenheit

The weather station has a section on its website that is specified for tourists. The tourists are interested in the temperature in Fahrenheit.

Fahrenheit is a unit of temperature measurement. It has a different scale than Celsius. 0°C is 32°F, and 1 degree Celsius equals 1.8 degrees Fahrenheit.

Here is a table showing the conversion between Celsius and Fahrenheit:

Celsius(°C) Fahrenheit(°F)
-273.15 -459.8
-100 -148
0 32
100 212

The weather station wants you to implement a method that converts temperatures from Celsius to Fahrenheit.

Implement the Temperature#to_fahrenheit method that takes an argument celsius and returns the temperature in Fahrenheit as an integer.

Temperature.new.to_fahrenheit(10)
# => 50

4. Get the number of missing sensors

During the installation of the temperature sensors, the maintenance workers realized some sensors were missing. Each temperature measurement unit requires four temperature sensors. The maintenance team counted how many sensors they had but needed to know how many were missing. They would like help counting how many sensors they need to buy to build another complete temperature measurement unit.

Implement the Temperature#number_missing_sensors method that takes an argument number_of_sensors and returns the number of missing sensors.

Temperature.new.number_missing_sensors(10)
# => 2
Edit via GitHub The link opens in a new window or tab
Crystal Exercism

Ready to start Wellington's Weather Station?

Sign up to Exercism to learn and master Crystal with 26 concepts142 exercises, and real human mentoring, all for free.