Search⌘ K
AI Features

Solution: The Coffee Shop Receipt

Explore how to apply Java variables, primitive and reference types, and operators to solve a practical problem. Learn to calculate price, tax, and totals using double and int data types, multiplication and addition operators, and string concatenation to format output.

We'll cover the following...
Java 25
public class CoffeeShopReceipt {
public static void main(String[] args) {
// 1. Define order details
double lattePrice = 5.50;
int quantity = 3;
double taxRate = 0.08; // 8% tax
// 2. Perform calculations
double subtotal = lattePrice * quantity;
double taxAmount = subtotal * taxRate;
double grandTotal = subtotal + taxAmount;
// 3. Print the receipt
System.out.println("Receipt Summary:");
System.out.println("Subtotal: " + subtotal);
System.out.println("Tax: " + taxAmount);
System.out.println("Total: " + grandTotal);
}
}
...