Lesson Overview
Split Java programs into small methods with parameters, return values and single responsibilities.
Portfolio focus: Find repeated code in a simple program.
Starter: think before typing
Before running this structure example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `With VAT: 12.0`; predict how the focus line helps produce that evidence.
Learning Objectives
- Write void and value-returning methods.
- Pass parameters without relying on global state.
- Use decomposition to reduce duplicated code.
- Explain method contracts using inputs, outputs and side effects.
Learning Outcomes
- By the end of the lesson, you can write void and value-returning methods.
- By the end of the lesson, you can pass parameters without relying on global state.
- By the end of the lesson, you can use decomposition to reduce duplicated code.
- By the end of the lesson, you can explain method contracts using inputs, outputs and side effects.
Why this idea exists
Decomposition is one of the main ways programmers control complexity. As programs grew, it became impossible to understand everything at once, so programmers learned to divide systems into named pieces with limited responsibilities.
Methods are a practical form of abstraction: callers need to know what a method promises, not every internal step. This idea connects directly to later topics such as APIs, modules, encapsulation and services.
A good method is not merely short. It has a reason to exist, a clear input/output relationship and a name that makes the surrounding program easier to read.
Deep dive
Mechanism in this example
The important mechanism is visible around `return price * 1.20;`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.
Design pressure
A good method is not merely short. It has a reason to exist, a clear input/output relationship and a name that makes the surrounding program easier to read.
Failure mode to watch
For Methods and Decomposition, deliberately disturb the assumption behind `return price * 1.20;`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the structure design.
Extension step
Extend the example by doing this: Extract it into a method. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.
Portfolio standard
The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: write a method contract in one sentence.
Lesson visual

Type this and run it
Create MethodsAndDecompositionDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.
public class MethodsAndDecompositionDemo {
public static void main(String[] args) {
double netPrice = 10.0;
System.out.println("With VAT: " + addVat(netPrice));
}
static double addVat(double price) {
return price * 1.20;
}
}Build and run it with:
javac MethodsAndDecompositionDemo.java && java MethodsAndDecompositionDemoExpected baseline: With VAT: 12.0
Run the code in your browser
Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates structure and compare the new behaviour with the reference output.
With VAT: 12.0Line-by-line explanation
Read the code as a sequence of responsibilities. The focus line for this lesson is return price * 1.20;; the surrounding lines prepare it, use its result or make the behaviour observable.
public class MethodsAndDecompositionDemo {This names the runnable class for the Methods and Decomposition example, giving the compiler and JVM one clear unit to build.
public static void main(String[] args) {This is the program entry point. In Methods and Decomposition, it keeps the demonstration of structure in one traceable starting script.
double netPrice = 10.0;This introduces netPrice as named state for Methods and Decomposition. Later lines can read, update, pass or print that specific value as evidence.
System.out.println("With VAT: " + addVat(netPrice));This prints "With VAT: " + addVat(netPrice) as the observable evidence for Methods and Decomposition. The output lets the learner check whether the structure idea behaved as predicted.
}This closes the innermost Methods and Decomposition block, so the immediately preceding method, branch or loop has finished.
static double addVat(double price) {This starts addVat, a named Methods and Decomposition operation. Its parameters describe what information comes in; its body decides what work is done.
return price * 1.20;This sends a Methods and Decomposition result back to the caller, so the surrounding code can use the answer.
}This closes the outer Methods and Decomposition structure, returning the reader to the surrounding class or file.
}This closing brace number 3 completes another layer of the Methods and Decomposition source structure Java has been checking.
Worked example
From code to explanation
Problem: Use Methods and Decomposition to complete a small portfolio-quality step: Find repeated code in a simple program.
Method: Locate the line `return price * 1.20;`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.
Reveal worked answer
The checked run should produce `With VAT: 12.0`. A strong answer links the result back to structure: what was created, selected, stored, called or protected, and why that matters for the portfolio task.
Trace the program
Before: Before the key operation, identify the relevant value, object, branch or resource that the structure concept depends on.
During: Trace `return price * 1.20;` as the Methods and Decomposition example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.
After: Compare the run with the expected evidence: `With VAT: 12.0`.
Change: Now extract it into a method, run again, and explain the smallest reason the behaviour changed.
Common misconception
A common mistake in methods and decomposition is treating the example as a finished answer. For structure, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?
Quick checks
1. In this Methods and Decomposition example, what is the best reason to focus on `return price * 1.20;`?
2. Which evidence is strongest after you edit and rerun this example?
How to study this lesson
For Methods and Decomposition, predict how structure changes the run before you press Run.
Use the first portfolio task as your main edit: Find repeated code in a simple program.
Use the second task as your variation: Extract it into a method.
Finish with evidence, not a diary entry: Write a method contract in one sentence.
Portfolio Practice
- Find repeated code in a simple program.
- Extract it into a method.
- Write a method contract in one sentence.
Final self-check
Can you explain the key operation?
Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output With VAT: 12.0 appears.
Can you justify the portfolio evidence?
Your evidence should include the original run, one edited run, and a short note explaining how the edit affected structure.
Study route
Practise structure by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.
Next, move into Arrays, Strings and Input and carry forward one improvement from this lesson into the next program.
