CS 1401 Homework #3

Date Assigned: Tuesday, February 1, 2005

Due Date: Thursday, February 3, 2005, before the beginning of your lecture section.

Goal: the goal of this assignment is to learn how to deal with numbers in Java.

Assignment:
Write a piece of code that asks the user for a temperature in Celsius and returns the corresponding temperature in Fahrenheit. The corresponding formula is Fahrenheit = (9/5) Celsius + 32. Take into account that the temperature is not always an integer, e.g., in Celsius, the prefect body temperature is 36.6.

Trace your code on an example of 100 degrees Celsius, which should result in 212 degrees F.

Explain what happens if you literally write (9/5) in the corresponding Java code.

Explain what happens if you literally write (9/5) Celsius in the corresponding Java code.

Answer: 

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the temperature in Celsius"); 
double celsius = Double.parseDouble(in.readline()); 
double fahrenheit = (9.0/5.0) * celsius + 32.0; 
System.out.println("In Fahrenheit, this temperature is equivalent to " +
  fahrenheit + " degrees."); 

For celsius = 100.0, we will compute 9.0/5.0 = 1.8, then 1.8 * 100.0 = 
180.0, then 180.0 + 32.0 = 212.0 will be assigned to the variable
fahrenheit. 

If we literally write (9/5), the system will view it as integer
division, when 9/5=1.

If we literally write (9/5) celsius, without the multiplication
symbol, then Java compiler will return an error message.