← Back to Week 1 Hub

From Problem to Code

A step-by-step process for turning any exercise into a working Java program

1Read
2Identify
3Plan
4Translate
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 int
Customer age int

Rules / Logic

Size 1 = Regular → $5.45
Size 2 = Large → $8.95
Age ≤ 17 → 10% off
Age ≥ 65 → 20% off

Outputs

Final price with $ formatting
Step 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();
2. Look up base price from size
double price = 0;
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();
4. Determine discount
double discount = 0;
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)); } }
Click "Next" to begin highlighting key phrases

Try It Yourself

Before writing any code on your next exercise, follow these four steps:

  1. Read the entire problem description first
  2. Identify the inputs, rules, and outputs
  3. Plan the steps in plain English
  4. Translate each English step into Java

Use the Next / Previous buttons or click the step tabs at the top to navigate.

← switch Statement Flow