Problem of the Day
Friday, May 22, 2026
Problem:
Tickets for a bus are sold for a price based on the following system.
- A standard full price ticket for a bus ride is $2.00.
- Tickets for students or seniors are discounted 20%.
- Tickets purchased on a weekend receive a single discount of $1.00 on the entire purchase.
- Multiple tickets may be purchased at a time.
Consider the method below, intended to identity the total purchase price of a quantity of tickets.
/**
* Calculates bus ticket prices
* @param ticketQuantity the number of tickets being bought
* @param student true if ticket is for a student (20% discount)
* @param senior true if ticket is for a senior (20% discount)
* @param weekend true if ticket(s) purchased on a weekend ($1.00 off total purchase price)
*/
public static double getTotalPrice(int ticketQuantity, boolean student, boolean senior, boolean weekend)
{
final double FULL_PRICE_TICKET = 2.00;
/* missing code */
return purchasePrice;
}
Which of the following can be used to replace /* missing code */ so that the method works as intended?
if (student || senior)
FULL_PRICE_TICKET *= 0.80;
double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
if (weekend)
purchasePrice -= 1.00;double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
if (student || senior)
purchasePrice *= 0.80;
if (weekend)
purchasePrice -= 1.00 * ticketQuantity;double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
if (student && weekend || senior && weekend)
purchasePrice *= 0.80 - 1.00;double purchasePrice = ticketQuantity * FULL_PRICE_TICKET;
if (student || senior)
purchasePrice *= 0.80;
if (weekend)
purchasePrice -= 1.00;
The correct answer is d. In choice (a), the value of FULL_PRICE_TICKET cannot be altered. In (b), the code incorrectly discounts every ticket by $1.00 rather than a single discount for the entire purchase. And in (c), students or seniors purchasing a ticket not on a weekend will not get a discount.