java coding I am having difficulty with adding a method for choice and then roll
ID: 3737908 • Letter: J
Question
java coding I am having difficulty with adding a method for choice and then roll that into the switch. I also would like to be able to add a try catch so that the user can not select the wrong choice. Finally the ignore case for the Boolean is giving me fits. thanks for the help
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package conversion1;
/**
*
* @author William Shermer
*/
import java.util.Scanner;
public class Conversion1 {
public static void main(String[] args) {
boolean showKelvin = false;
System.out.println("Welcome to the English-to-Metric Converter!");
System.out.print("On Temp conversions, would you also like to ");
System.out.print("see degrees Kelvin (K) in the results? (Y/N):");
Scanner sc = new Scanner(System.in);
showKelvin = (sc.next().charAt(0) == 'Y');
while (true) {
System.out.print("Select conversion type");
System.out.print(" (1=mi-to-km, 2=in-to-cm, 3=f-to-c, 4=c-to-f, 0=end): ");
switch(sc.next().charAt(0)) {
case '3':
System.out.print("Enter your Fahrenheit temperature: ");
double temp = sc.nextDouble();
System.out.print("A temp of " + temp + " Fahrenheit converted to Celsius ");
System.out.println(fToC(temp) + " .");
if (showKelvin)
System.out.println("This is also a temperature of: " + fToK(temp));
break;
case '4':
System.out.print("Enter your Celsius temperature: ");
double ftemp = sc.nextDouble();
System.out.print("A temp of " + ftemp + " Celsius converted to Fahrenheit ");
System.out.println(cToF(ftemp) + " .");
if (showKelvin)
System.out.println("This is also a temperature of: " + cToK(ftemp));
break;
case '1':
System.out.print("Enter your Miles: ");
double miles = sc.nextDouble();
System.out.print("The distance of " + miles + " mile(s) converted to kilometer(s) = ");
System.out.println(milesToKM(miles));
break;
case '2':
System.out.print("Enter the inches: ");
double inches = sc.nextDouble();
System.out.print("The measurement of " + inches + " inch(es) converted");
System.out.print(" to centimeter(s) = " + iToC(inches));
break;
case '0':
System.exit(0);
} // switch
} // while
} // main
/**
* Miles -> Kilometers
* @param {double} m miles
*/
public static double milesToKM(double m) { return 1.60934 * m; }
/**
* Inches -> Centimeters
* @param {double} i Inches
*/
public static double iToC(double i) { return i * 2.54; }
/**
* Fahrenheit -> Celsius
* @param {double} f Fahrenheit
*/
public static double fToC(double f) { return (f - 32) * 5 / 9; }
/**
* Fahrenheit -> Kelvin
* @param {double} f Fahrenheit
*/
public static double fToK(double f) { return (f - 459.67) * 5 / 9; }
/**
* Fahrenheit -> Celsius
* @param {double} f Fahrenheit
*/
public static double cToF(double c) { return (c *9/5) + 32; }
/**
* Celsius -> Kelvin
* @param {double} c Celsius
*/
public static double cToK(double c) { return c + 273.15; }
}
Explanation / Answer
package January;
import java.util.Scanner;
// Class Conversion1 definition
public class Conversion1
{
// Scanner class object created
static Scanner sc = new Scanner(System.in);
// static method to display menu and accept user choice and return it
static int menu()
{
// Initializes choice to -1
int choice = -1;
// try block begins
try
{
// Displays the menu
System.out.print("Select conversion type");
System.out.print(" (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end): ");
// Accepts use choice
choice = sc.nextInt();
sc.nextLine();
// Checks if the choice less than zero or greater than 4
if(choice < 0 || choice > 4)
// throws exception
throw new Exception();
}// End of try block
// Catch block to handle exception
catch(Exception e)
{
// Displays error message
System.out.println("Invalid choice! Please reenter");
}// End of catch block
// Returns choice
return choice;
}// End of method
// main method definition
public static void main(String[] args)
{
boolean showKelvin = false;
char choice;
System.out.println("Welcome to the English-to-Metric Converter!");
System.out.print("On Temp conversions, would you also like to ");
System.out.print("see degrees Kelvin (K) in the results? (Y/N):");
// Accepts user choice for kelvin
choice = sc.next().charAt(0);
showKelvin = (choice == 'Y' || choice == 'y');
System.out.println("Welcome to the English - to - Metric Converter!");
// Loops till user choice is not 0
while (true)
{
// Calls the method to display menu and selects appropriate operation based on choice
switch(menu())
{
case 3:
System.out.print("Enter your Fahrenheit temperature: ");
double temp = sc.nextDouble();
System.out.print("A temp of " + temp + " Fahrenheit converted to Celsius ");
System.out.println(fToC(temp) + " .");
if (showKelvin)
System.out.println("This is also a temperature of: " + fToK(temp));
break;
case 4:
System.out.print("Enter your Celsius temperature: ");
double ftemp = sc.nextDouble();
System.out.print("A temp of " + ftemp + " Celsius converted to Fahrenheit ");
System.out.println(cToF(ftemp) + " .");
if (showKelvin)
System.out.println("This is also a temperature of: " + cToK(ftemp));
break;
case 1:
System.out.print("Enter your Miles: ");
double miles = sc.nextDouble();
System.out.print("The distance of " + miles + " mile(s) converted to kilometer(s) = ");
System.out.println(milesToKM(miles));
break;
case 2:
System.out.print("Enter the inches: ");
double inches = sc.nextDouble();
System.out.print("The measurement of " + inches + " inch(es) converted");
System.out.println(" to centimeter(s) = " + iToC(inches));
break;
case 0:
System.exit(0);
} // End of switch chase
} // End of while loop
} // End of main method
/**
* Miles -> Kilometers
* @param {double} m miles
*/
public static double milesToKM(double m) { return 1.60934 * m; }
/**
* Inches -> Centimeters
* @param {double} i Inches
*/
public static double iToC(double i) { return i * 2.54; }
/**
* Fahrenheit -> Celsius
* @param {double} f Fahrenheit
*/
public static double fToC(double f) { return (f - 32) * 5 / 9; }
/**
* Fahrenheit -> Kelvin
* @param {double} f Fahrenheit
*/
public static double fToK(double f) { return (f - 459.67) * 5 / 9; }
/**
* Fahrenheit -> Celsius
* @param {double} f Fahrenheit
*/
public static double cToF(double c) { return (c *9/5) + 32; }
/**
* Celsius -> Kelvin
* @param {double} c Celsius
*/
public static double cToK(double c) { return c + 273.15; }
}// End of class
Sample Output:
Welcome to the English-to-Metric Converter!
On Temp conversions, would you also like to see degrees Kelvin (K) in the results? (Y/N):y
Welcome to the English - to - Metric Converter!
Select conversion type (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end): 12
Invalid choice! Please reenter
Select conversion type (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end): 4
Enter your Celsius temperature: 10
A temp of 10.0 Celsius converted to Fahrenheit 50.0 .
This is also a temperature of: 283.15
Select conversion type (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end): 2
Enter the inches: 25
The measurement of 25.0 inch(es) converted to centimeter(s) = 63.5
Select conversion type (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end): 9
Invalid choice! Please reenter
Select conversion type (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end): 3
Enter your Fahrenheit temperature: 65
A temp of 65.0 Fahrenheit converted to Celsius 18.333333333333332 .
This is also a temperature of: -219.26111111111112
Select conversion type (1 = mi - to - km, 2 = in - to - cm, 3 = f - to - c, 4 = c - to - f, 0 = end):
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.