Activity 1.43.1.
displayDiscount("VIP", 100)
displayDiscount("Regular", 200)
displayDiscount("Guest", 300)
displayDiscount("Visitor", 300)
public static void displayDiscount(String customerType, double price) { double finalPrice = price; if ("VIP".equals(customerType)) { finalPrice = price * 0.8; System.out.println("Customer Type: VIP"); } else if ("Regular".equals(customerType)) { finalPrice = price * 0.9; System.out.println("Customer Type: Regular"); } else { System.out.println("Customer Type: Guest"); } System.out.println("Original Price: $" + price); System.out.println("Discounted Price: $" + finalPrice); }
- Correct!
public static void displayDiscount(String customerType, double price) { double adjustedPriceFactor = 1; if ("VIP".equals(customerType)) { adjustedPriceFactor = 0.8; System.out.println("Customer Type: VIP"); } else if ("Regular".equals(customerType)) { adjustedPriceFactor = 0.9; System.out.println("Customer Type: Regular"); } else { System.out.println("Customer Type: Guest"); } double finalPrice = price * adjustedPriceFactor; System.out.println("Original Price: $" + price); System.out.println("Discounted Price: $" + finalPrice); }
- Correct!
public static void displayDiscount(String customerType, double price) { double finalPrice = price; double adjustedPriceFactor = 1; if ("VIP".equals(customerType)) { adjustedPriceFactor = 0.8; } else if ("Regular".equals(customerType)) { adjustedPriceFactor = 0.9; } System.out.println("Customer Type:" + customerType); finalPrice = price * adjustedPriceFactor; System.out.println("Original Price: $" + price); System.out.println("Discounted Price: $" + finalPrice); }
- Not always! Compare the outputs for displayDiscount("Visitor", 300)
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 void displayDiscount(String customerType, double price) {
double finalPrice = 0;
if ("VIP".equals(customerType)) {
finalPrice = price * 0.8; // 20% discount
System.out.println("Customer Type: VIP");
System.out.println("Original Price: $" + price);
System.out.println("Discounted Price: $" + finalPrice);
} else if ("Regular".equals(customerType)) {
finalPrice = price * 0.9; // 10% discount
System.out.println("Customer Type: Regular");
System.out.println("Original Price: $" + price);
System.out.println("Discounted Price: $" + finalPrice);
} else {
finalPrice = price; // No discount
System.out.println("Customer Type: Guest");
System.out.println("Original Price: $" + price);
System.out.println("Discounted Price: $" + finalPrice);
}
}
Hint.
You can use the following test cases to compare the code blocks’ functionality.
Select all that apply.