Skip to main content

Practice Code Structure

Section 1.35 Code Refactoring

Activity 1.35.1.

There is a copy of the following code block in an interactive mode that you can modify and compile. Your task is to improve the code’s style as an expert would view it, while ensuring its behavior remains unchanged. You may run your refactored code to verify that it still passes all test cases.
public class WithinRange {
	public static boolean isInRange(int number, int lowerBound, int upperBound) {
		if (number >= lowerBound && number <= upperBound) {
			return true;
		} else {
			return false;
		}
	}
}

Activity 1.35.2.

    Which of the following code blocks is the most similar to your final refactored code for the previous question?
  • public class WithinRange {
    	public static boolean isInRange(int number, int lowerBound, int upperBound) {
    		if (number >= lowerBound && number <= upperBound) {
    			return true;
    		}
    		return false;
    	}
    }
    
  • This code can further be improved!
  • public class WithinRange {
    	public static boolean isInRange(int number, int lowerBound, int upperBound) {
    		return number >= lowerBound && number <= upperBound;
    	}
    }
    
  • You did it correctly!
You have attempted 1 of 3 activities on this page.