Question

Program must be in JAVA. Write a program that converts temperature from Celsius to Fahrenheit or...

Program must be in JAVA.

Write a program that converts temperature from Celsius to Fahrenheit or vice versa, depending on user's input.

The formula for converting temperature in Celsius to Fahrenheit is,

Fahrenheit = (9/5) * Celsius + 32

The formula for converting temperature in Fahrenheit to Celsius is,

Celsius = (5/9) * (Fahrenheit – 32)

Results should be rounded to two decimal points, e.g., 92.56

Homework Answers

Answer #1

import java.util.Scanner;
public class temp {

       public static double celsius_To_Fahrenheit(double degree_C)
       {
       double fahrenheit_Result = (9*(degree_C) / 5) + 32;
       return fahrenheit_Result;
       }

       public static double fahrenheit_To_Celsius(double degree_F)
       {
       double celsius_Result = 5*(degree_F - 32)/9;
       return celsius_Result;
       }

       public static void init(){
       Scanner scan = new Scanner(System.in);
       double result;
       char cont= 'y';
       char type;


       while (cont!='q' && cont!='Q')
       {
       System.out.println("Please enter the degree:");
       double degree = scan.nextDouble();

       System.out.println("Now enter the type of conversion (For Celsius, type 'C'. For Fahrenheit, type 'F')");
       type = scan.next().charAt(0);

       if(type!='c' && type!='C' && type!='f' && type!='F')
       {
       System.out.println("Error! You've entered an invalid conversion type!");
       System.out.println("Please enter a valid selection (Type C for celsius and F for fahrenheit");
       type = scan.next().charAt(0);
       }
       if (type == 'F' || type =='f')
       {
       result = fahrenheit_To_Celsius(degree);
       System.out.println("The conversion of "+ degree+"°F "+ "to Celsius is: " + Math.round(result * 100.0) / 100.0 + "°C");

       }
       if (type == 'C' || type =='c')
       {
       result = celsius_To_Fahrenheit(degree);
       System.out.println("The conversion of "+ degree+"°C "+ "to Fahrenheit is: " + Math.round(result * 100.0) / 100.0 + "°F");
       }


       System.out.println("Would you like to quit? Type 'q' to quit or type any other letter to continue:");
       cont = scan.next().charAt(0);
       }
       scan.close();
       }

       public static void main(String[] args)
       {
       init();
       }
}

Code Image:

Output Image:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in java that generates a two-column table showing Fahrenheit temperatures from -40F to...
Write a program in java that generates a two-column table showing Fahrenheit temperatures from -40F to 120F and their equivalent Celsius temperatures. Each line in the table should be 5 degrees F more than the previous one. Both the Fahrenheit and Celsius temperatures should be accurate to 1 decimal place. I am not able to use: import java.text.DecimalFormat; Any feedback would be helpful.
step by step in python please The program will prompt the user as to whether you...
step by step in python please The program will prompt the user as to whether you want to convert from Celsius to Fahrenheit or from Fahrenheit to Celsius Write it so each conversion is contained within its own function (i.e., one function to do the math in one direction, a second to do the math in the other direction) These two functions should just have the input temperature as a parameter and return the output temperature in the other units....
Write a program in java that: 1. takes any number in any base between 2 and...
Write a program in java that: 1. takes any number in any base between 2 and 16 and converts it to base 10. 2. converts base 10 numbers to any given base between 2 & 16. The program should have a gui with an input box and two buttons, one button to convert any base between 2 and 16 to base 10; and one button to convert base 10 to any base between 2 and 16. **Please DO NOT upload...
Write a Java program segment (not an entire program) that will summarize the season results for...
Write a Java program segment (not an entire program) that will summarize the season results for 5 players on one of TRU’s basketball teams. Each player played 10 games. Your program segment should ask the user for a player’s name, followed by the points that player scored in each of the 10 games (a total of 11 pieces of input). Your program will then produce the summary line shown below for that player.   Your program should get the data for...
Download the attached .java file. Run it, become familiar with its processes. Your task is to...
Download the attached .java file. Run it, become familiar with its processes. Your task is to turn TemperatureConversion into GUI based program. it should, at the least, perform similar functions as their text output versions. The key factor to remember is that the workings should remain the same (some tweaks may be necessary) between text and GUI programs, while the means pf visual presentation and user interaction changes. You must properly document, comment, indent, space, and structure both programs. import...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose input is the number of steps, and whose output is the miles walked. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); Ex: If the input is: 5345 the output is: 2.67 Your program must define and call a method: public static double stepsToMiles(int userSteps)
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Write a program in Java that: 1. will prompt user with a menu that contains options...
Write a program in Java that: 1. will prompt user with a menu that contains options to: a. Add a To-Do Item to a todo list. A To-Do Item contains: i. An arbitrary reference number (4; 44,004; etc.) ii. A text description of the item ("Pick up dry cleaning", etc.) iii. A priority level (1, 2, 3, 4, or 5) b. View an item in the list (based on its reference number) c. Delete an item from the list (based...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
Write a program that takes two numbers from the Java console representing, respectively, an investment and...
Write a program that takes two numbers from the Java console representing, respectively, an investment and an interest rate (you will expect the user to enter a number such as .065 for the interest rate, representing a 6.5% interest rate). Your program should calculate and output (in $ notation) the future value of the investment in 5, 10, and 20 years using the following formula: future value = investment * (1 + interest rate)year We will assume that the interest...