Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this assignment, we will be writing static methods that convert temperature f

ID: 3828662 • Letter: I

Question

In this assignment, we will be writing static methods that convert temperature from Celsius to Fahrenheit and from Fahrenheit to Celsius. We will also be using a ‘switch’ statement to determine which conversion to use and validate input. We will also be using a while or do-while to run conversion of more than 1 temperature until the sentinel value is reached.   We will be creating 2 java class files.

One will be the Temperature class file which will have 2 public static methods, celsiusToFahrenheit() – there will be one input for Celsius temperature. The output will be Fahrenheit temperature. What is the logical primitive datatype for temperature?

The formula to convert is the following:

        fahrenheit = (celsius * 9 / 5) + 32;

fahrenheitToCelsius() - there will be one input for Fahrenheit temperature. The output will be Celsius temperature. What is the logical primitive datatype for temperature?

The formula to convert is the following:

        celsius = (fahrenheit - 32) * 5 / 9;

The 2nd java class file will be the driver program called TempDemo.    This is the program that will have your main() method. The program will:

prompt the user if they want to convert from F to C or from C to F. User can input a 1, 2, or Q (can accept q also). This prompt will be displayed repeatedly until the user types in a Q to quit the program. This is how to display the prompt to user:

Please select the converter direction.

1. From Celsius to Fahrenheit.

2. From Fahrenheit to Celsius.

Q. To Quit.

Enter Q, 1 or 2 ===>

Read the input as a String. Notice inputs are numbers (1 or 2) and characters (Q)!

Get the first character of the String! Remember that method from way back in the String chapter 2.10. See page 80. Need to get the first char of the String to be used on the switch statement in next step (p243 example).

Use a ‘switch’ statement (Chp 4.10) on the first character of the input String to:

case 1 - then prompt user for temperature in C. Read input.

case 2 - then prompt user for temperature in F. Read input.

case Q – Quit program.

otherwise - invalid input, then display an error message:

Invalid converter selected. Enter 1,2 or Q

if input is 1 or 2, execute the correct static method you created in Temperature to convert the temp.

When valid input, display the converted temperature:

100.00 degrees Celsius is equal to 212.00 degrees Fahrenheit.

OR

32.00 degrees Fahrenheit is equal to 0.00 degrees Celsius.

if input is Q or q, set sentinel value to quit. Display “Goodbye”.

Explanation / Answer

/**
* The java program TempDemo that prompts user
* to enter choice for conversion of temp. Then the user prompt
* to enter temperature value. Then converts to corresponding
* temperature and print the result to console
* and reprompt to enter temp or Q or q to exit.
* */
//TempDemo.java
import java.util.Scanner;
public class TempDemo
{
   public static void main(String[] args)
   {
       //Scanner class instance
       Scanner keyboard=new Scanner(System.in);
       //declare variables
       String input;
       //set repaet to true
       boolean repeat=true;
       double temp;
       //contintue until repeat is true
       while(repeat)
       {
           System.out.println("Please select the converter direction.");
           System.out.println("1. From Celsius to Fahrenheit.");
           System.out.println("2. From Fahrenheit to Celsius.");
           System.out.println("Q. To Quit.");
           System.out.println("Enter Q, 1 or 2 ===>");
           //prompt for user input
           input=keyboard.nextLine();
          
           switch (input)
           {
          
           //for case 1
           case "1":
               System.out.println("Enter temperature in Celsius");
               temp=keyboard.nextDouble();
               System.out.printf("%.2f degrees Celsius is equal to %.2f degrees Fahrenheit. ",
                       temp,Temperature.celsiusToFahrenheit(temp));
               break;
               //for case 2
           case "2":
               System.out.println("Enter temperature in Fahrenheit ");
               temp=keyboard.nextDouble();
               System.out.printf("%.2f degrees Fahrenheit is equal to %.2f degrees Celsius. ",
                       temp,Temperature.fahrenheitToCelsius(temp));
               break;
               //for case Q or q
           case "Q":                              
           case "q":
               //set repeat to false
               repeat=false;
               System.out.println("Good Bye");
               break;
           default:
               //print for invalid input
               System.out.println("Invalid converter selected. Enter 1,2 or Q");
               break;
           }
       }

   }
}

------------------------------------------------------------------------------------------------------------------------

//Temperature.java
public class Temperature
{
   /*
   * The static celsiusToFahrenheit that takes celsius temperature
   * and returns the temperature in fahrenheit
   * */
   public static double celsiusToFahrenheit(double celsius)
   {
       double fahrenheit = (celsius * (9.0 / 5.0)) + 32.0;
       return fahrenheit;
   }
  
   /*
   * The static fahrenheitToCelsius that takes fahrenheit temperature
   * and returns the temperature in celsius
   * */
   public static double fahrenheitToCelsius(double fahrenheit)
   {
       double celsius = (fahrenheit - 32.0) *( 5.0 / 9.0);
       return celsius;
   }
}//end of Temperature

------------------------------------------------------------------------------------------------------------------------

Sample run:

Please select the converter direction.
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
Q. To Quit.
Enter Q, 1 or 2 ===>
1
Enter temperature in Celsius
100
100.00 degrees Celsius is equal to 212.00 degrees Fahrenheit.
Please select the converter direction.
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
Q. To Quit.
Enter Q, 1 or 2 ===>
Invalid converter selected. Enter 1,2 or Q
Please select the converter direction.
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
Q. To Quit.
Enter Q, 1 or 2 ===>
2
Enter temperature in Fahrenheit
32
32.00 degrees Fahrenheit is equal to 0.00 degrees Celsius.
Please select the converter direction.
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
Q. To Quit.
Enter Q, 1 or 2 ===>
Invalid converter selected. Enter 1,2 or Q
Please select the converter direction.
1. From Celsius to Fahrenheit.
2. From Fahrenheit to Celsius.
Q. To Quit.
Enter Q, 1 or 2 ===>
q
Good Bye