Question

In: Computer Science

How would I go about debugging this to make it work properly? import java.util.Scanner; public class...

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 + ".");
         }
 }
}

Solutions

Expert Solution

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.


Related Solutions

How would I prepare pseudocode and a flowchart for this? import java.util.Scanner; public class HotDogsBuns {...
How would I prepare pseudocode and a flowchart for this? import java.util.Scanner; public class HotDogsBuns {    private static int HOTDOG_COUNT_PKG = 10;    private static int BUNS_COUNT_PKG = 8;       public static void main(String[] args) {        //Scanner object to get user input        Scanner keyboard = new Scanner(System.in);               // Get number of people        System.out.print("How many people will be competing in the contest?: ");        int noOfPeople = keyboard.nextInt();...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square...
what i have: import java.util.Scanner; public class examples1 { public static void main(String[] args) { Square shade = new Square(getside()); System.out.println("Your chooses are:"); System.out.println("\nCircle"); System.out.println("Triangle"); System.out.println("Rectangle"); System.out.println("Square"); boolean moreShapes = true; boolean userChoice = true; while(moreShapes) { Scanner shapes = new Scanner (System.in); System.out.println("\nWhat shape do you want:"); String yourShape = shapes.nextLine(); if (userChoice = shade != null) { shade.getSide(); } System.out.println("\nWhat are two size paramters of the shape you choose:"); String yourParameter = shapes.nextLine(); System.out.println("\nYour shape is: " +...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT