Step 1: Read — Understand What the Program Does
Input
Variable
Value
Condition
Output
Create a program for a sandwich shop. Ask the user what size sandwich they want (1 for Regular at $5.45, 2 for Large at $8.95). Then ask for their age. If they are 17 or under, give a 10% discount. If they are 65 or older, give a 20% discount. Display the final price.
Pattern recognized: This program takes input → applies rules → shows output. Most exercises follow this pattern.
Step 2: Identify — What Are the Inputs, Rules, and Outputs?
Inputs
Sandwich size
intCustomer age
intRules / Logic
Size 1 = Regular →
$5.45Size 2 = Large →
$8.95Age ≤ 17 →
10% offAge ≥ 65 →
20% offOutputs
Final price with
$ formattingStep 3: Plan — Write the Steps in Plain English
1Ask the user for their sandwich size (1 or 2)
↓
2Look up the base price based on size chosen
↓
3Ask the user for their age
↓
4Determine discount: 10% if ≤ 17, 20% if ≥ 65, else 0%
↓
5Calculate final price = base price × (1 − discount)
↓
6Display the result to the user
No Java yet — just think through the logic first.
Step 4: Translate — Map Each Step to Java
English Plan
Java Code
1. Ask for sandwich size
System.out.println("Choose a size: 1) Regular 2) Large");
int size = scanner.nextInt();
int size = scanner.nextInt();
2. Look up base price from size
double price = 0;
if (size == 1) { price = 5.45; }
else if (size == 2) { price = 8.95; }
if (size == 1) { price = 5.45; }
else if (size == 2) { price = 8.95; }
3. Ask for age
System.out.println("Enter your age:");
int age = scanner.nextInt();
int age = scanner.nextInt();
4. Determine discount
double discount = 0;
if (age <= 17) { discount = 0.10; }
else if (age >= 65) { discount = 0.20; }
if (age <= 17) { discount = 0.10; }
else if (age >= 65) { discount = 0.20; }
5. Calculate final price
double finalPrice = price * (1 - discount);
6. Display the result
System.out.println("Your total: $" + String.format("%.2f", finalPrice));
Complete Program
import java.util.Scanner;
public class SandwichShop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
orderSandwich(scanner);
}
public static void orderSandwich(Scanner scanner) {
// Step 1: Ask for sandwich size
System.out.println("Choose a size: 1) Regular 2) Large");
int size = scanner.nextInt();
// Step 2: Look up base price
double price = 0;
if (size == 1) { price = 5.45; }
else if (size == 2) { price = 8.95; }
// Step 3: Ask for age
System.out.println("Enter your age:");
int age = scanner.nextInt();
// Step 4: Determine discount
double discount = 0;
if (age <= 17) { discount = 0.10; }
else if (age >= 65) { discount = 0.20; }
// Step 5: Calculate final price
double finalPrice = price * (1 - discount);
// Step 6: Display result
System.out.println("Your total: $" + String.format("%.2f", finalPrice));
}
}