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

Write a Java program to continue creating your own user-defined methods and intr

ID: 3815026 • Letter: W

Question

Write a Java program to continue creating your own user-defined methods and introduce some do-whileloops.

Write two value-returning methods called farToCel() and celToFar(). These two methods will convert temperatures from Fahrenheit to Celsius and Celsius to Fahrenheit respectively. They will each take a single int parameter and return the converted value as an int.

Write an additional value-returning method called displayMenu() to display a three item menu and read input from the user which is their selection and return that as a char. Consider using the Character class method toUpperCase() to narrow the number of choices from six {F, f, C, c, Q, q} to three {F, C, Q}.

Note that displayMenu() has no parameters. The displayMenu() method MUST only return valid values.

Start by calling displayMenu() within a do-while loop in the main() method which will capture the return value of displayMenu() into a char variable. This value will be used to determine what type of conversion is to be done and the loop will terminate when the user enters ‘Q’.

The other do-while loop is in the displayMenu() method. We would stay in the displayMenu() method until the user chooses a valid selection. By doing so, this method can never return bad selections to the main() method.

Once you have mastered your displayMenu() method, you can proceed to the other two. Also, note that displayMenu() is not responsible for reading the temperatures; the main() method will do this based on the choice made by the user.

The calculation for Fahrenheit to Celsius is:

The calculation for Celsius to Fahrenheit is:

Your output should resemble:

Explanation / Answer

TempConverterTest.java

import java.util.Scanner;


public class TempConverterTest {

   static Scanner scan = new Scanner(System.in);
  
   public static void main(String[] args) {
       char ch='';
       do{
           ch = displayMenu();
           if(ch!='q' && ch!='Q') {
               if(ch=='c' || ch=='C'){
                   System.out.println("Enter the Celsius temperature: ");
                   int c = scan.nextInt();
                   System.out.println("The Temperature "+c+" Fahrenheit is "+celToFar(c)+" Celsius.");
               }
               else{
                   System.out.println("Enter the Fahrenheit temperature: ");
                   int f = scan.nextInt();
                   System.out.println("The Temperature "+f+" Celsius is "+farToCel(f)+" Celsius.");
                  
               }
           }
       }while(ch!='q' && ch!='Q');

   }
   public static char displayMenu() {
      
       System.out.println("Please select one of the following: F - To convert Fahrenheit to Celsius C - To convert Celsius to Fahrenheit Q - To Quit. Choice: ");
       char ch = scan.next().charAt(0);
       while(ch!='c' && ch!='C' && ch!='f'&&ch!='F'&&ch!='q'&&ch!='Q') {
           System.out.println("Please select one of the following: F - To convert Fahrenheit to Celsius C - To convert Celsius to Fahrenheit Q - To Quit. Choice: ");
           ch = scan.next().charAt(0);
              
       }
       return ch;
   }
   public static int celToFar(int temp){
       return (int)((temp * 9)/5 + 32);
   }
   public static int farToCel(int temp){
       return (int)((temp - 32) * 5)/9;
   }
  

}

Output:

Please select one of the following:
F - To convert Fahrenheit to Celsius
C - To convert Celsius to Fahrenheit
Q - To Quit.
Choice:
f
Enter the Fahrenheit temperature:
32
The Temperature 32 Celsius is 0 Celsius.
Please select one of the following:
F - To convert Fahrenheit to Celsius
C - To convert Celsius to Fahrenheit
Q - To Quit.
Choice:
c
Enter the Celsius temperature:
-40
The Temperature -40 Fahrenheit is -40 Celsius.
Please select one of the following:
F - To convert Fahrenheit to Celsius
C - To convert Celsius to Fahrenheit
Q - To Quit.
Choice:
q

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote