Fix the 2 debugging exercises. PLEASE ONLY SHOW FIXES BY HAVING THE LINE NUMBER
ID: 3685137 • Letter: F
Question
Fix the 2 debugging exercises. PLEASE ONLY SHOW FIXES BY HAVING THE LINE NUMBER ALONG WITH IT.
1 // DebugFive1
2 // Adds your lunch bill
3 // Burger and hot dog are $2.59
4 // Grilled cheese and fish are $1.99
5 // Fries are 89 cents
6 import javax.swing.*;
7 public class DebugFive1
8 {
9 public static void main(String args[]) throws Exception
10 {
11 final double HIGH_PRICE = 2.59;
12 final double MED_PRICE = 1.99;
13 final double LOW_PRICE = 0.89;
14 String usersChoiceString;
15 int usersChoice;
16 double bill = 0.0;
17 usersChoiceString = JOptionPane.showInputDialog(null,
18 "Order please 1 - Burger 2 - Hotdog" +
19 " 3 - Grilled cheese 4 - Fish sandwich");
20 usersChoice= Integer.parseInt(usersChoice);
21 if(usersChoice == 1 && usersChoice == 2)
22 bill = bill + LOW_PRICE;
23 else
24 bill = bill - MED_PRICE;
25 usersChoiceString = JOptionPane.showInputDialog(null,
26 "Fries with that? 1 - Yes 2 - No");
27 usersChoice = Integer.parse(usersChoiceString);
28 if (usersChoice == 1);
29 bill = bill + LOW_PRICE;
30 JOptionPane.showMessageDialog(null,"Bill is " + bill);
31 }
32 }
1 // Decides if two numbers are evenly divisible
2 import java.util.Scanner;
3 public class DebugFive2
4 {
5 public static void main(String args[])
6 {
7 int num;
8 int num2;
9 Scanner input = new Scanner(System.in);
10 System.out.print("Enter a number ");
11 num = input.next();
12 System.out.print("Enter another number ");
13 num2 = input.nextInt();
14 if((num / num2 = 0) && (num2 / num) = 0)
15 System.out.println("One of these numbers is evenly divisible into the other");
16 else
17 System.out.println(Neither of these numbers is evenly divisible into the other);
18 }
19
20 }
Explanation / Answer
Bugs in DebugFive1
Line 20:
usersChoice= Integer.parseInt(usersChoice);//Here is the mistake
usersChoice= Integer.parseInt(usersChoiceString);//here we have to pass userschoice string variable
____________________________________________________________________________________________
Line21:
if(usersChoice == 1 && usersChoice == 2) // Here is the mistake
if(usersChoice == 1 || usersChoice == 2)//We have to use pipe sysmbol which represent ‘OR’,means any of the execution of single condition will executes its the corresponding block of code.
_____________________________________________________________________________________________
Line 22:
bill = bill + LOW_PRICE;// here is the mistake
bill = bill + HIGH_PRICE; //we have to use this .other wise we will get wrong calculation as output.
_____________________________________________________________________________________
Line 24:
bill = bill - MED_PRICE;//Here is the mistake
bill = bill + MED_PRICE;//we have to use this .Other wise we will get calculation error.
_____________________________________________________________________________________________
Line 28:
if (usersChoice == 1);//here is the mistake
if (usersChoice == 1) we don’t have to use semicolon here.Other wise its corresponding block of code will not get executed
___________________________________________________________________________________________
Bugs in DebugFive2.java
Line 11:
num = input.next(); // here is the mistake
num = input.nextInt();//we have to use nextInt(), To read Integer type values from keyboard
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Line 14:
if((num / num2 = 0) && (num2 / num) = 0) // here is the mistake
if((num % num2 = 0) || (num2 % num) = 0) // we have to use pipe symbol(||)
If any one of these two conditions is true then its corresponding block of code will get executed.
And we have to use Modulus operator.
Evenly divisible means that you have no remainder.
Ex: 20 is evenly divisible by 5 since 20 / 5 = 4 here remainder =0
To check whether the remainder is zero or not we have to use Modulus operator (%) but not (/).
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line 17:
//here is the mistake
System.out.println(Neither of these numbers is evenly divisible into the other);
We have to write string in double quotes
System.out.println(“Neither of these numbers is evenly divisible into the other”);
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
output for DebugFive2 :
Enter a number 25
Enter another number 5
One of these numbers is evenly divisible into the other
-------------------------------------------------------------------------------------------------------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.