Activity 1.41.1.
calculateWeight(120, true, true)
calculateWeight(100, true, false)
calculateWeight(100, false, true)
public static double calculateWeight(double weight, boolean isFragile, boolean isBulk) { double adjustment = 0.0; if (isFragile) { adjustment += 0.1; } else if (isBulk) { adjustment += 0.05; } return weight * (1 + adjustment); }
- Correct!
public static double calculateWeight(double weight, boolean isFragile, boolean isBulk) { double finalWeight = weight; if (isFragile) { finalWeight += weight * 0.1; } if (isBulk) { finalWeight += weight * 0.05; } return finalWeight; }
- Not correct! Compare the outputs for calculateWeight(100, true, true)
public static double calculateWeight(double weight, boolean isFragile, boolean isBulk) { double finalWeight = weight; if (isFragile) { finalWeight += weight * 0.1; } else if (isBulk) { finalWeight += weight * 0.05; } return finalWeight; }
- Correct!
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)? You can check the Hint for some input examples.
public static double calculateWeight(double weight, boolean isFragile, boolean isBulk) {
if (isFragile) {
return weight + (weight * 0.1); // Add 10% extra for packaging
}
if (isBulk) {
return weight + (weight * 0.05); // Add by 5% for bulk items
} else {
return weight;
}
}
Hint.
You can use the following test cases to compare the code blocks’ functionality.
Select all that apply.