In: Computer Science
Instructions: Save this file as YourName_PracticeDebuggingTest.
- Part 1 – 4 Debugging Exercises – there are things wrong – either logic or syntax errors. Correct the pseudocode and one Java program. Add the corrected code in BOLD and change the corrected color to RED.
Part 1
Debugging 1: Add the missing code in bold and red.
start
Declarations
num firstNum
num secondNum
string MSG = “Got it!”
housekeeping()
detail()
finish()
stop
housekeeping()
output “Enter three numbers: ”
input firstNum, thirdNum
return
detail()
if ((firstNm + secondNum) = thirdnum) OR
((secondNum + thirdNum) = firstNum)) OR
((firstNum + thirdNum) = secondNum) then
output MSNG
endif
return
finish()
output “End of program”
return
We will first find out the errors in the pseudocode and then convert it into the Java program as specified in the problem statement.
Let's look at the detail() function:
detail()
if ((firstNm + secondNum) = thirdnum) OR
((secondNum + thirdNum) = firstNum)) OR
((firstNum + thirdNum) = secondNum) then
output MSNG
endif
return
In the above code:
Problem 1 - We are using an undeclared variable, thirdNum.
Solution - We will add the declaration of thirdNum, in the Declarations part of the code.
Problem 2 - We are using an undeclared variable, MSNG.
Solution - If we carefully look at the Declarations section, we have a variable MSG declared in it. Therefore, unlike in Problem 1, we can solve this problem by replacing the MSNG variable with MSG in the detail() function.
Problem 3 - In the three parts of the, if condition mentioned, there is an error of parentheses. Consider the bolded parentheses in the statements below:
( (secondNum + thirdNum) = firstNum) ) => valid
( (secondNum + thirdNum) = firstNum) ) => valid
( (secondNum + thirdNum) = firstNum) ) => invalid, because, there is no opening parentheses corresponding to this last closing parentheses.
Solution - We will remove the last closing parentheses as it is of no use and will lead to error.
Therefore, after debugging, the corrected code of the detail() function will be:
detail()
if ((firstNm + secondNum) = thirdnum) OR
((secondNum + thirdNum) = firstNum) OR
((firstNum + thirdNum) = secondNum) then
output MSG
endif
return
We will make our Declarations section correct by adding the declaration of thirdNum into it. Therefore, after debugging, the corrected code of the Declarations will be:
start
Declarations
num firstNum
num secondNum
num thirdNum // declaring thirdNum
string MSG = “Got it!”
housekeeping()
detail()
finish()
stop
Let's look at the housekeeping() function:
housekeeping()
output "Enter three numbers: "
input firstNum, thirdNum
return
Problem 1 - We are using an undeclared variable, thirdNum.
Solution - We will add the declaration of thirdNum, in the Declarations part of the code. (we have already corrected this problem during correcting the detail() code)
Problem 2 - One error that can possibly be present here is that the secondNum is not taken as input, which, maybe considered valid in some programming languages, but, for our pseudocode, we will consider it to be invalid.
Solution - We will code such that secondNum is also taken as input.
Therefore, after debugging, the corrected code of the housekeeping() function will be:
housekeeping()
output "Enter three numbers: "
input firstNum, secondNum, thirdNum
return
Let's take the last function of the code, the finish() function:
finish()
output “End of program”
return
There seems to be no error in this function.
You can find the final code below. We have tried to put the code as mentioned in the problem statement, bold and red.
Also, please find the code with comments stating the error correction just after this.
start
Declarations
num firstNum
num secondNum
num
thirdNum
string MSG = “Got it!”
housekeeping()
detail()
finish()
stop
detail()
if ((firstNm + secondNum) = thirdnum) OR
((secondNum +
thirdNum) = firstNum) OR
((firstNum + thirdNum) = secondNum) then
output MSG
endif
return
housekeeping()
output "Enter three numbers: "
input firstNum, secondNum, thirdNum
return
finish()
output "End of program"
return
Code with comments stating the error correction:
start
Declarations
num firstNum
num secondNum
num thirdNum // declaring thirdNum
string MSG = “Got it!”
housekeeping()
detail()
finish()
stop
detail()
// removed the closing parenthese in the second condition
if ((firstNm + secondNum) = thirdnum) OR
((secondNum + thirdNum) = firstNum) OR
((firstNum + thirdNum) = secondNum) then
output MSG // changed MSNG to MSG
endif
return
housekeeping()
output "Enter three numbers: "
// secondNum is also taken as input
input firstNum, secondNum, thirdNum
return
finish()
output "End of program"
return
Let us now implement the above pseudocode in Java.
Please note that in order to keep our Java program simple, we have used all the variables and methods as static. (If we would use non-static data and methods, then, we would have needed to create an object and then call the methods on it, therefore, we have avoided that)
Here is the Java code.
import java.util.*;
public class YourName_PracticeDebuggingTest {
// declaration
static int firstNum = 0;
static int secondNum = 0;
static int thirdNum = 0;
static String MSG = "Got it!";
// execute the if condition statements
public static void detail () {
if ((firstNum + secondNum) == thirdNum ||
(secondNum + thirdNum) == firstNum ||
(firstNum + thirdNum) == secondNum) {
System.out.println (MSG);
}
}
// this is used for taking input
public static void housekeeping () {
// show the message
System.out.println ("Enter three numbers: ");
Scanner input = new Scanner (System.in);
// take input for the 3 variables
firstNum = input.nextInt();
secondNum = input.nextInt();
thirdNum = input.nextInt();
}
// finish the program
public static void finish () {
// show the finish message
System.out.println ("End of Progtam");
}
public static void main (String [] args) {
// we will:
// 1. First take input.
// 2. Then, execute if condition statements.
// 3. Finish the program.
// taking input
housekeeping ();
// if statements
detail ();
// finish message
finish ();
}
}