Assignment #2 - Basic Calculations
Program #2
Your second program is a modification of Problem 10 on page 144 of our text. In that problem,
the user inputs a temperature value in degrees Celsius and the program then calculates the
equivalent temperature in Fahrenheit.
Let’s modify the problem in the following way. Rather than requiring the user to input just a
Celsius temperature and then calculating the Fahrenheit temperature from it, let’s calculate
equivalent temperatures in both directions. In addition, we’ll add to the report the equivalent
temperate in a third temperature scale, namely, degrees Kelvin.
We’ll first ask the user for a Celsius temperature and then print a report about both the
Fahrenheit and the Kelvin temperatures. The output should like the following:
Please enter a number in degrees Celsius: 45.2
The equivalent Fahrenheit temperature is: 113.36 degrees.
The equivalent Kelvin temperature is: 318.36 degrees.
Your program will then ask the user to input a number in degrees Fahrenheit and print the
equivalent Celsius and Kelvin degrees:
Please enter a number in degrees Fahrenheit: 45.2
The equivalent Celsius temperature is: 7.33 degrees.
The equivalent Kelvin temperature is: 280.49 degrees.
Note that all calculated temperatures are printed to two decimal places.
import java.util.Scanner;
import java.text.DecimalFormat;
public class TempApp {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
Scanner input = new Scanner(System.in);
double celsius;
System.out.print("Please enter a number in degrees Celsius: ");
celsius = input.nextDouble();
double fahrenheit = 1.8 * celsius + 32;
System.out.println("The equivalent Fahrenheit temperature is: "
+ (df.format(fahrenheit)));
double kelvin = celsius + 273.16;
System.out.println("The equivalent Kelvin temperature is: " + (df.format(kelvin)));
}
}