Advertisement
thotfrnk

methods6.java

Dec 7th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class number6 {
  3.     public static void main(String[] args) {
  4.  
  5.         /*6. Implement the following integer methods:
  6.         *(a) Method Celsius returns the Celsius equivalent of a Fahrenheit temperature, using
  7.         * Celsius = 5.0 / 9.0 * (Fahrenheit – 32);
  8.         *(b) Method Fahrenheit returns the Fahrenheit equivalent of a Celsius temperature, using the calculation
  9.         * Fahrenheit = 9.0 / 5.0 * Celsius + 32;
  10.         *(c) Use the methods from parts (a) and (b) to write an application that enables the user either to enter a
  11.         * Fahrenheit temperature and display the Celsius equivalent or to enter a Celsius temperature
  12.         * and display the Fahrenheit equivalent.
  13.          */
  14.  
  15.         Scanner input = new Scanner (System.in);
  16.  
  17.         double celsius, fahrenheit;
  18.         String desired_temp;
  19.  
  20.         System.out.println("Celsius or Fahrenheit?");
  21.         desired_temp = input.next();
  22.  
  23.         if(desired_temp.equals("celsius")) {
  24.             System.out.println("What is the temperature?");
  25.             celsius = input.nextDouble();
  26.  
  27.             System.out.println("Temperature: " +celsius + "C");
  28.             System.out.println("Fahrenheit equivalent: " +Fahrenheit(celsius) + "F");
  29.         }
  30.         else
  31.             if(desired_temp.equals("fahrenheit")) {
  32.                 System.out.println("What is the temperature?");
  33.                 fahrenheit = input.nextDouble();
  34.  
  35.                 System.out.println("Temperature: " +fahrenheit + "F");
  36.                 System.out.println("Celsius equivalent: " +Celsius(fahrenheit) + "C");
  37.             }
  38.     }
  39.  
  40.     public static double Celsius (double fahrenheit) {
  41.         double celsius;
  42.  
  43.         celsius = 5.0 / 9.0 * (fahrenheit - 32);
  44.  
  45.         return celsius;
  46.     }
  47.  
  48.     public static double Fahrenheit (double celsius) {
  49.         double fahrenheit;
  50.  
  51.         fahrenheit = 9.0 / 5.0 * (celsius + 32);
  52.  
  53.         return fahrenheit;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement