Free course · Lesson 5 of 5
Problem Solving with Java
In this lesson you will
- Break a small problem into classes and methods
- Build and test a solution step by step
Combine variables, control flow, collections, and classes to solve a complete, small problem. Java rewards planning: sketch your classes and methods before typing, since the compiler will hold you to every declared type and signature.
- Identify the "things" in the problem — each usually becomes a class.
- Identify the "actions" on those things — each usually becomes a method.
- Compile early and often; fix the first error the compiler reports before moving to the next.
- Test with realistic sample data, then with edge cases (empty arrays, zero values).
💡 Practical Skill: This step-by-step discipline is exactly what's expected in a professional Java codebase — clear classes, clear methods, and code that compiles cleanly.
import java.util.HashMap;import java.util.ArrayList; public class Main { static ArrayList<String> checkStock(HashMap<String, Integer> inventory, int threshold) { ArrayList<String> lowStock = new ArrayList<>(); for (String item : inventory.keySet()) { if (inventory.get(item) < threshold) { lowStock.add(item); } } return lowStock; } public static void main(String[] args) { HashMap<String, Integer> inventory = new HashMap<>(); inventory.put("exercise books", 120); inventory.put("pens", 45); inventory.put("chalk boxes", 8); ArrayList<String> reorder = checkStock(inventory, 10); if (reorder.isEmpty()) { System.out.println("All stock levels are healthy."); } else { System.out.println("Reorder needed for: " + reorder); } }}
Practice exercise
Plan and write a small program that records three expenses and prints the total and the largest expense, testing it with your own numbers.
Key takeaways
- Plan the classes and methods before you code
- Build and run the program in small steps
- Test with normal, boundary and invalid values
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.