Skip to main content
Contents
Search Book
Search Results:
No results.
Readability settings Prev Up Next Profile
title here
\(\newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 1.17 Code Refactoring
Activity 1.17.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 DiscountChecker {
public static String determineDiscountEligibility(int age, int membershipYears) {
if (age > 18) {
if (membershipYears >= 5) {
return "Eligible";
}
}
return "Not eligible";
}
}
Activity 1.17.2 .
Which of the following code blocks is the most similar to your final refactored code for the previous question?
public class DiscountChecker {
public static String determineDiscountEligibility(int age, int membershipYears) {
if (age > 18 && membershipYears >= 5) {
return "Eligible";
}
return "Not eligible";
}
}
You refactored correctly!
public class DiscountChecker {
public static String determineDiscountEligibility(int age, int membershipYears) {
if (age > 18 || membershipYears >= 5) {
return "Eligible";
}
return "Not eligible";
}
}
The code doesnβt have the same functionality with the original one.
public class DiscountChecker {
public static String determineDiscountEligibility(int age, int membershipYears) {
if (age <= 18 || membershipYears < 5) {
return "Not eligible";
}
return "Eligible";
}
}
You refactored correctly!
You have attempted
of
activities on this page.