Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class number6 {
- public static void main(String[] args) {
- /*6. Implement the following integer methods:
- *(a) Method Celsius returns the Celsius equivalent of a Fahrenheit temperature, using
- * Celsius = 5.0 / 9.0 * (Fahrenheit – 32);
- *(b) Method Fahrenheit returns the Fahrenheit equivalent of a Celsius temperature, using the calculation
- * Fahrenheit = 9.0 / 5.0 * Celsius + 32;
- *(c) Use the methods from parts (a) and (b) to write an application that enables the user either to enter a
- * Fahrenheit temperature and display the Celsius equivalent or to enter a Celsius temperature
- * and display the Fahrenheit equivalent.
- */
- Scanner input = new Scanner (System.in);
- double celsius, fahrenheit;
- String desired_temp;
- System.out.println("Celsius or Fahrenheit?");
- desired_temp = input.next();
- if(desired_temp.equals("celsius")) {
- System.out.println("What is the temperature?");
- celsius = input.nextDouble();
- System.out.println("Temperature: " +celsius + "C");
- System.out.println("Fahrenheit equivalent: " +Fahrenheit(celsius) + "F");
- }
- else
- if(desired_temp.equals("fahrenheit")) {
- System.out.println("What is the temperature?");
- fahrenheit = input.nextDouble();
- System.out.println("Temperature: " +fahrenheit + "F");
- System.out.println("Celsius equivalent: " +Celsius(fahrenheit) + "C");
- }
- }
- public static double Celsius (double fahrenheit) {
- double celsius;
- celsius = 5.0 / 9.0 * (fahrenheit - 32);
- return celsius;
- }
- public static double Fahrenheit (double celsius) {
- double fahrenheit;
- fahrenheit = 9.0 / 5.0 * (celsius + 32);
- return fahrenheit;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement