Sunteți pe pagina 1din 6

/** * *A program that reads a temperature in degrees Fahrenheit and prints the temperature in degrees Celsius.

* * @author Dale Miguel * @version 1.00 2009/7/5 * */ import java.util.Scanner;

public class TemperatureConverterA { static Scanner kbd = new Scanner(System.in);

public static void main(String[] args){ char choice='a'; while ( choice != 'c'){ showMenu(); choice = readChoice("abc"); convert(choice); System.out.print("\n\nPlease press enter to continue."); kbd.nextLine(); } // end of while System.out.println("Again, thank you."); System.exit(0); } // end of main method

/** *Displays a menu on the screen **/ public static void showMenu(){ System.out.println("This program helps you convert temperature measures."); System.out.println("----------------Menu----------------------"); System.out.println("a: Convert from Farenheit to Celsius"); System.out.println("b: Convert from celsius to Fahrenheit"); System.out.println("c: Quit"); System.out.println("------------------------------------------"); } // end of showMenu method

/** *Reads and returns a character that is found *in the string validChoices from the keyboard **/ public static char readChoice(String validChoices){ char choice = 'x'; do { System.out.print("Enter your choice<a/b/c>: "); choice = kbd.nextLine().charAt(0); if (validChoices.indexOf(choice) == -1 ) System.out.println("Invalid Choice! Please enter only characters found in the string "+ validChoices); } while (validChoices.indexOf(choice)==-1 );

return choice; } // end of readChoice method

/** *Returns the temperature in Celsius that is equal to a given temperature * in Fahrenheit **/ public static double convertToCelsius(double fahrenheit){ double celsius=0; celsius = 5.0/9.0*(fahrenheit-32); return celsius; }

/** *Returns the temperature in Fahrenheit that is equal to a given temperature * in Celsius **/ public static double convertToFahrenheit(double celsius){ return 9.0/5.0*celsius+32; }

/** *performs a conversion based on a choice **/ public static void convert(char choice){

double fahrenheit=0, celsius=0; switch (choice) { case 'a': System.out.print("Enter the temperature in Fahrenheit: "); fahrenheit = Double.parseDouble(kbd.nextLine()); //Compute then display the equivalent temperature in Celsius celsius = convertToCelsius(fahrenheit); System.out.println(fahrenheit + " degrees Fahrenhiet equals " + celsius + " degrees Celsius "); break; case 'b': System.out.print("Enter the temperature in celsius: "); celsius = Double.parseDouble(kbd.nextLine()); //Compute then display the equivalent temperature in Celsius fahrenheit = convertToFahrenheit(celsius); System.out.println(celsius + " degrees celsius equals " + fahrenheit + " degrees farenheit."); break; case 'c': System.out.println("Thank you!"); } } // end of convert method

} // end of class

import javax.swing.*;

import java.util.*; public class TemperatureConverterB {

public static void main(String[] args) { Scanner kbd=new Scanner(System.in); int choice=0; do { choice = readChoice(1,3); switch (choice){ case 1: convertCelsiusToFahrenheit(readTemperature()); break; case 2: convertFahrenheitToCelsius(readTemperature()); break; case 3: JOptionPane.showMessageDialog(null, "Thank you for using this program."); } } while (choice !=3);

System.exit(0); } public static int readChoice(int lowerLimit, int upperLimit){ int choice=0; do {

String input = JOptionPane.showInputDialog("Temperature Conversion Menu \n1. Celsius to Fahrenhiet\n2. Fahrenheit to Celsius\n3. Quit\nEnter your choice <1/2/3>: "); choice = Integer.parseInt(input); if (choice <lowerLimit || choice > upperLimit){ JOptionPane.showMessageDialog(null, choice +" is not a valid choice."); } }while (choice <lowerLimit || choice>upperLimit); return choice; }

public static void convertCelsiusToFahrenheit(double celsius){ JOptionPane.showMessageDialog(null, celsius+ " degrees Celsius = " + (9*celsius/5+32)); }

public static void convertFahrenheitToCelsius(double fahrenheit){ JOptionPane.showMessageDialog(null, fahrenheit+ " degrees Fahrenheit = " + (5*(fahrenheit32)/9)); }

public static double readTemperature(){ String input = JOptionPane.showInputDialog("Enter the temperature to be converted:" ); return (Double.parseDouble(input)); }

S-ar putea să vă placă și