Skip to main content

Learning about Code Structure

Section 1.11 Practice Problems

Checkpoint 1.11.1.

The function eligibleForDiscount() takes an integer input and returns a boolean. Complete the function by filling in the blanks so that it returns true when the age is less than 18 or greater than 60 and otherwise it returns false.
public static boolean eligibleForDiscount(int age) {   
  	   return .....Blank.......... ; 
}
Write your responses here:
Blank:

Activity 1.11.1.

There is a copy of the following code block in an interactive mode that you can 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 FileProcess {
	public static boolean canProcessFile(int fileSize, boolean hasPermission) {
		if (!hasPermission) {
			   return false; 
		}
		if (fileSize >= 1024) {
			   return false; 
		}
		return true;
	}
}

Activity 1.11.2.

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