/* * @topic T00520 Assignment a5 Temperature Converter demo * @brief BigDecimal demo to convert Fahrenheit to Celsius */ package Wk05_demo; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Scanner; public class a5_temperature_demo { public static Scanner sc = new Scanner(System.in); public static String userTemperatureInput; public static double conversionResult; public static final int CHOICE_SET_TEMPERATURE = 1; public static final int CHOICE_CHANGE_ROUNDING = 2; public static final int CHOICE_CONVERT_F_2_C = 3; public static final int CHOICE_CONVERT_C_2_F = 4; public static final int CHOICE_EXIT = 5; public static double centigrade2Fahrenheit() { // Formula: // F = C * 9/5 + 32 return 0.0; } public static double fahrenheit2Centigrade() { // Formula: // C = ( F - 32 ) * 5 / 9 // userTemperatureInput is F value // ik-10/15/2015 version using a double computation: //double fahrenheit = Double.parseDouble(userTemperatureInput); //double centigrade = (fahrenheit - 32.0) * 5.0 / 9.0; //return centigrade; // ik-10/15/2015 version using a BigDecimal BigDecimal fahrenheit = new BigDecimal(userTemperatureInput); fahrenheit = fahrenheit.setScale(2, RoundingMode.HALF_UP); BigDecimal centigrade = fahrenheit.subtract( new BigDecimal("32.0") ); centigrade = centigrade.multiply(new BigDecimal("5.0")); centigrade = centigrade.divide( new BigDecimal("9.0"), 2, RoundingMode.HALF_UP ); return centigrade.doubleValue(); } public static void main(String[] args) { for (;;) { int userChoice = menuInput(); switch (userChoice) { case CHOICE_SET_TEMPERATURE: break; case CHOICE_CHANGE_ROUNDING: break; case CHOICE_CONVERT_F_2_C: System.out.print("Enter F degrees: "); userTemperatureInput = sc.next(); conversionResult = fahrenheit2Centigrade(); System.out.print("\t\t\t\t\t\t" + userTemperatureInput + "F == " + conversionResult + "C\n"); break; case CHOICE_CONVERT_C_2_F: System.out.print("Enter C degrees: "); userTemperatureInput = sc.next(); conversionResult = centigrade2Fahrenheit(); break; case CHOICE_EXIT: System.out.println("Thank you, bye!"); return; default: break; } }//for } public static int menuInput() { for (;;) { System.out.println("_________________________________________"); System.out.print(CHOICE_SET_TEMPERATURE); System.out.println(" Set temperature range"); System.out.print(CHOICE_CHANGE_ROUNDING); System.out.println(" Change rounding scale"); System.out.print(CHOICE_CONVERT_F_2_C); System.out.println(" Convert Fahrenheit temperature to Celsius"); System.out.print(CHOICE_CONVERT_C_2_F); System.out.println(" Convert Celsius temperature to Fahrenheit"); System.out.print(CHOICE_EXIT); System.out.println(" Exit program"); System.out.print(">>> "); int userChoice = sc.nextInt(); if (1 <= userChoice && userChoice <= 5) { return userChoice; } System.out.println("Sorry, this option is not supported..."); }//for } }//class a5_temperature_demo