In: Computer Science
Question 4 - Methods (Unit Conversion)
Unit conversion is one of those perennial problems that has haunted humanity since prehistory. It's notable in that a solution has been widely proposed, studied, and adopted (standardization!) and yet our civilization STILL… well, there's no gentle way to describe our dysfunction here.
Let's take a moment to consider the implications of having (for instance) five different units of temperature: Degrees Kelvin, Degrees Celsius, Degrees Fahrenheit, Rankines, and Cones. In order to perform a direct conversion from, say, Kelvin to all other temperature units, we must determine, maintain, and implement four conversions. The same goes for each of the other units. If we chart these relationships, we might end up with the following representation, which is also called a complete graph.
The driver and function prototype are as follows:
public class Main { public static void main( String[] args ) { double thermometerTemp = 101.3; // The function call occurs here double degKelvin = degF_to_degK( thermometerTemp ); System.out.println( "My temp of (" + thermometerTemp + ") DegF" ); System.out.println( "Equals a temp of (" + degKelvin + ") DegK" ); } public static <TODO> degF_to_degK( <TODO> temp ) { double kelvinResult = <TODO>; return kelvinResult; } } |
We won't take the time to write other conversions, but you can hopefully see how one could move any arbitrary number of units both to Kelvin by making a set of '_to_degK' functions, then back out using a set of complementary 'degK_to_' functions.
Provide a screenshot demonstrating the Fahrenheit equivalent of:
0 degrees Kelvin // -459.67 273.15 degrees Kelvin // 32 373.2 degrees Kelvin // 212.09 |
This warm up brings us to the next exercise.
Please look at my code and in case of indentation issues check the screenshots.
-----------------Main.java----------------
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new
Scanner(System.in);
System.out.print("Enter temperature
in degree Kelvin: ");
double thermometerTemp =
sc.nextDouble();
//read temp in Kelvin from
user
// The function call occurs
here
double degFahrenheit =
degK_to_degF(thermometerTemp);
//convert the kelvin temperature to Fahrenheit
System.out.println("My temp of (" +
thermometerTemp + ") DegK");
System.out.printf("Equals a temp of
(%.2f) DegF\n", degFahrenheit); //print the result,
rounded to 2 decimal places
}
public static double degK_to_degF(double
kelvinTemp)
{
double fahrenheitResult = (9.0 / 5)
* (kelvinTemp - 273.15) + 32; //converts kelvinTemp to fahrenheit
temperature
return
fahrenheitResult;
}
}
--------------Screenshots-------------------
------------------Output-----------------
----------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou