Question

In: Computer Science

Instructions:  Save this file as YourName_PracticeDebuggingTest. - Part 1 – 4 Debugging Exercises – there are things...

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

Solutions

Expert Solution

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 ();
        }
}

Related Solutions

- Part 1 – 4 Debugging Exercises – there are things wrong – either logic or...
- 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.   Debugging 2:  Add the missing code in bold and red. start                   Declarations                            num number                             housekeeping()                   while number >= 15                            detailLoop()                   endwhile                             finish()     stop housekeeping()          number = 1 return detailLoop()          output number          num = number + 1 return finishUp()          output “End of program”...
Chapter 6 Debugging Exercises - Problem 1 The programmer intends for this pseudocode to display three...
Chapter 6 Debugging Exercises - Problem 1 The programmer intends for this pseudocode to display three random numbers in the range of 1 through 7. According to the way we've been generating random numbers in the book; however, there appears to be an error. Assume that the random() function is a built-in library function. Correct the pseudocode so that the program works as it should (This has 1 error and is easy to spot) //This program displays 3 random numbers...
1.The file name Final.xlsx, sheet named Part 4 is kept blank. Use Part 4 sheet to...
1.The file name Final.xlsx, sheet named Part 4 is kept blank. Use Part 4 sheet to answer this question. Put a box around your answers. Round your answer to 4 decimal places. Information from the American Institute of Insurance indicates the mean amount of life insurance per household in the United States is $110,000 with a standard deviation of $40,000. Assume the population distribution is normal. A random sample of 100 households is taken. (a)What is the probability that sample...
Chapter 4 Assignments - Part #1 (Part #2 is on next page) Submit an HTML file...
Chapter 4 Assignments - Part #1 (Part #2 is on next page) Submit an HTML file attached to an email with your web page containing the elements below. - Create a web page with a comment containing your name after this line. o - In the head section, add character encoding. - Add the title "Chapter 4 Web Page #1 - by yourname" - Create a file called myStyles.css as the style sheet for the web page. - Add the...
part a. In class we learned that the debugger, and _________ are effective in debugging C++...
part a. In class we learned that the debugger, and _________ are effective in debugging C++ programs. a. a logic analyzer b. print statements c. code reviews d. network analyzers part b. A ‘side-effect’ is a. code written in a memory-efficient way b. a style of writing C++ statements that is risky. c. code written so that more than one thing is being done in one statement. d. (a) and (b) e. (b) and (c) f. none of the above...
Project Part 4: Part Four: Complete the addCruise() Method 1. In the Driver.java file, see below...
Project Part 4: Part Four: Complete the addCruise() Method 1. In the Driver.java file, see below for where you will add code to finish the addCruise() method. // Adda New Cruise public static void addCruise() { // complete this method } 2. Next, review the required functionality of the “Add Cruise” menu option below. Menu Option Validation Check(s) Functionality Add Cruise - Ensures cruise does not already exist in our system - Ensures all class variables are populated Adds cruise...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are working and 1 program (Python). Explainthe intent of the pseudocode / program. If you use test data, note the test data.  .  You are NOT trying to find mistakes. Use this textbox to explain the pseudocode/ code intent. Include any test data used: What does this do? Desk Checking #4:  Explain the intent of this code.  Be as specific as possible.   List the data you use for example...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are working and 1 program (Python). Explainthe intent of the pseudocode / program. If you use test data, note the test data.  .  You are NOT trying to find mistakes. What does this do? Desk Checking #2:  Explain the intent of this pseudocode.  Be as specific as possible. List the data you use as the example data. Use this textbox to explain the pseudocode/ code intent. Include any test...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are working and 1 program (Python). Explainthe intent of the pseudocode / program. If you use test data, note the test data.  .  You are NOT trying to find mistakes What does this do? Desk Checking #3:  Explain the intent of this pseudocode.  Be as specific as possible.   List the data you use for example data. Use this textbox to explain the pseudocode/ code intent. Include any test data...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are...
- Part 2 – 4 Desk Checking Exercises – these are 3 programs (pseudocode) that are working and 1 program (Python). Explainthe intent of the pseudocode / program. If you use test data, note the test data.  .  You are NOT trying to find mistakes. What does this do? Desk Checking #1:  Explain the intent of this pseudocode. List the data you use as the example data. start                   Declarations          num balance Use this textbox to explain the pseudocode/ code intent. Include any test...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT