Skip to main content

Learning about Code Structure

Section 1.13 Rule 2) Directly return boolean expressions

Continue watching to learn about the second rule, then answer the following questions.
Figure 1.13.1. Directly return boolean expressions part 3

Activity 1.13.1.

    What is the negation of the following expression?
    (isPeerReviewed && hIndex > 3) || citation > 50
    
    Select all that apply.
  • (!isPeerReviewed && hIndex <= 3) || citation <= 50
    
  • (!isPeerReviewed || hIndex <= 3) && citation <= 50
    
  • Correct! When applying negation, || will become && and vice versa. Also < will become >= and > will become <=.
  • (!isPeerReviewed || hIndex > 3) && citation > 50
    
  • Read the feedback explanation for option 2.

Activity 1.13.2.

    Look at the first code block. Which of the following options have the same functionality as it (i.e., they produce the same output for the same inputs)?
    public static boolean isHighImpact(boolean isPeerReviewed, String journalName, int citationCount) {
        if (isPeerReviewed) {  
            if (isWellKnownJournal(journalName)) { 
                if (citationCount > 50) { 
                    return true;
                }
            }
        }
        return false;
    }
    
    Select all that apply.
  • public static boolean isHighImpact(boolean isPeerReviewed, String journalName, int citationCount) {
        if (!isPeerReviewed) {
            return false;
        }  
        if (!isWellKnownJournal(journalName)) {
            return false;
        }
        if (citationCount <= 50) { 
            return false;               
        }               
        return true;
    }
    
  • Correct! This code is has the same functionality with the first code block. Use the truth table to verify.
  • public static boolean isHighImpact(boolean isPeerReviewed, String journalName, int citationCount) {
        if (isPeerReviewed && isWellKnownJournal(journalName) && citationCount > 50) { 
           return true;
        }
        return false;
    }
    
  • Correct! This is the pattern for the first rule, nested if statements that can be conjoined with &&.
  • public static boolean isHighImpact(boolean isPeerReviewed, String journalName, int citationCount) {
        if (!isPeerReviewed || !isWellKnownJournal(journalName) || citationCount <= 50) { 
            return false;               
        } else {               
            return true; 
        }
    }
    
  • Correct! The if condition returns false; therefore, we can negate the condition. This makes the if body return true and else return false, same as the code in option B.
  • public static boolean isHighImpact(boolean isPeerReviewed, String journalName, int citationCount) {
        return isPeerReviewed && isWellKnownJournal(journalName) && citationCount > 50;
    }
    
  • Correct! This code has the same functionality as option B but it is well-structured as it directly returns the condition.
You have attempted 1 of 4 activities on this page.