How to resolve the algorithm Temperature conversion step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Temperature conversion step by step in the Scala programming language

Table of Contents

Problem Statement

There are quite a number of temperature scales. For this task we will concentrate on four of the perhaps best-known ones: Kelvin, Celsius, Fahrenheit, and Rankine. The Celsius and Kelvin scales have the same magnitude, but different null points. The Fahrenheit and Rankine scales also have the same magnitude, but different null points. The Celsius/Kelvin and Fahrenheit/Rankine scales have a ratio of 5 : 9.

Write code that accepts a value of kelvin, converts it to values of the three other scales, and prints the result.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Temperature conversion step by step in the Scala programming language

Source code in the scala programming language

object TemperatureConversion extends App {

  def kelvinToCelsius(k: Double) = k + 273.15

  def kelvinToFahrenheit(k: Double) = k * 1.8 - 459.67

  def kelvinToRankine(k: Double) = k * 1.8

  if (args.length == 1) {
    try {
      val kelvin = args(0).toDouble
      if (kelvin >= 0) {
        println(f"K  $kelvin%2.2f")
        println(f"C  ${kelvinToCelsius(kelvin)}%2.2f")
        println(f"F  ${kelvinToFahrenheit(kelvin)}%2.2f")
        println(f"R  ${kelvinToRankine(kelvin)}%2.2f")
      } else println("%2.2f K is below absolute zero", kelvin)

    } catch {
      case e: NumberFormatException => System.out.println(e)
      case e: Throwable => {
        println("Some other exception type:")
        e.printStackTrace()
      }
    }
  } else println("Temperature not given.")
}


  

You may also check:How to resolve the algorithm Inconsummate numbers in base 10 step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the PHP programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Tau function step by step in the Java programming language
You may also check:How to resolve the algorithm Two bullet roulette step by step in the C++ programming language