Search⌘ K
AI Features

Solution: The Coffee Shop Receipt

Explore how to define variables of different types, use arithmetic operators for calculations, and apply string concatenation in Java by working through a coffee shop receipt example that computes costs and taxes.

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);
}
}
...