In: Computer Science
How would I go about debugging this to make it work properly?
import java.util.Scanner; public class TemperatureConverter{ public static void main(String[] args) { // Declare named constants. final double MIN_FAHRENHEIT = -459.67; // Declare the variables. double fahrenheit; double convertedDegrees; int tempScale; String tempScaleStr=""; // Creating the Scanner object Scanner keyboard = new Scanner(System.in); System.out.print("Enter the temperature in Fahrenheit: "); fahrenheit = keyboard.nextDouble(); // Set a breakpoint here // Verify the user's input if (fahrenheit > MIN_FAHRENHEIT) { // first if-statement: Set a breakpoint here System.out.print("The temperature must be greater than or equal to " + MIN_FAHRENHEIT); System.exit(0); } System.out.print ( "Enter the temperature scales you want to convert to:\n"+ "1. Kelvin \n"+ "2. Rankine \n"+ "3. Reaumur \n"+ "4. Celsius\n"+ "Enter a temperature scale: "); tempScale = keyboard.nextInt(); if (tempScale > 1 && tempScale < 4) { // Second-if statement: Set a breakpoint here System.out.println("Unknown temperature scale -" + " cannot do calculation. Bye"); } else { if (tempScale == 1) { // Set a breakpoint here convertedDegrees = fahrenheit + MIN_FAHRENHEIT*5/9; tempScaleStr="Kelvin"; } else if (tempScale == 2 || tempScale == 3) { convertedDegrees = convertedDegrees - MIN_FAHRENHEIT; tempScaleStr="Rankine"; } else if (tempScale == 3) { convertedDegrees = fahrenheit - 32*4/9 ; // Set a breakpoint here tempScaleStr="Rankine"; } else { convertedDegrees = (fahrenheit - 32)*9/5; // Set a breakpoint here tempScaleStr="Celsius"; } System.out.println(fahrenheit + " degrees Fahrenheit is " + convertedDegrees + " degrees "+ tempScaleStr + "."); } } }
Note: You have commited mistakes in only 2 areas, one is verify the user input area where first if-statement is used and next is second if-statement is used. There we have to reverse the conditional operators so that the functionality is worked. Following is the debugged code.
Debugged code for above problem
import java.util.Scanner;
public class TemperatureConverter{
public static void main(String[] args) {
// Declare named
constants.
final double
MIN_FAHRENHEIT = -459.67;
// Declare the
variables.
double fahrenheit
= 0.0;
double
convertedDegrees = 0.0;
int tempScale =
0;
String
tempScaleStr="";
// Creating the
Scanner object
Scanner keyboard =
new Scanner(System.in);
System.out.print("Enter the temperature in Fahrenheit:
");
fahrenheit =
keyboard.nextDouble(); // Set a breakpoint here
// Verify the
user's input
if (fahrenheit
< MIN_FAHRENHEIT) { // first if-statement: Set a
breakpoint here
System.out.print("The temperature must be greater than or equal to
" + MIN_FAHRENHEIT);
System.exit(0);
}
System.out.print
(
"Enter the temperature scales you want to convert to:\n"+
"1. Kelvin \n"+
"2. Rankine \n"+
"3. Reaumur \n"+
"4. Celsius\n"+
"Enter a temperature scale: ");
tempScale =
keyboard.nextInt();
if (tempScale <
1 && tempScale > 4) { // Second-if statement: Set a
breakpoint here
System.out.println("Unknown temperature scale -" +
" cannot do calculation. Bye");
}
else {
if (tempScale == 1) { // Set a breakpoint here
convertedDegrees = fahrenheit + MIN_FAHRENHEIT*5/9;
tempScaleStr="Kelvin";
} else if (tempScale == 2 || tempScale == 3) {
convertedDegrees = convertedDegrees - MIN_FAHRENHEIT;
tempScaleStr="Rankine";
} else if (tempScale == 3) {
convertedDegrees = fahrenheit - 32*4/9 ; // Set a breakpoint
here
tempScaleStr="Rankine";
} else {
convertedDegrees = (fahrenheit - 32)*9/5; // Set a breakpoint
here
tempScaleStr="Celsius";
}
System.out.println(fahrenheit + " degrees Fahrenheit is " +
convertedDegrees + " degrees "+ tempScaleStr + ".");
}
}
}
Sample output
Mention in comments if any mistakes or errors are found. Thank you.