Milestone 2 Review: Building a Functional Application
Consolidate Your Learning
Purpose
This document provides a comprehensive review of the key concepts you learned while building and refactoring the Task Management application in Milestone 2. Through a series of active learning exercises, you will solidify your understanding of debugging, handling errors, choosing data structures, and turning a design into a functional Java program.
What You’ll Accomplish
By the end of this review session, you will have:
- Reinforced your skills in using the VS Code debugger to trace and fix bugs.
- Practiced identifying and handling potential runtime errors with exceptions.
- Analyzed the trade-offs between different data structures like
ArrayListandHashMap. - Connected the concepts of UML design, Java implementation, and iterative refactoring.
Active Learning and Engagement
For all the exercises and questions in this review document, you are to record your work on a new page in your Microsoft Teams Student Notebook. This will be the official record of your review process.
Exercise 1: You Are the Debugger
Read the following scenario and the buggy code snippet. In your notebook, describe the step-by-step process you would take using the VS Code debugger to find the bug. Be specific about where you would place your breakpoint and which debugger controls you would use.
Scenario: A new method was added to TaskManager to find all tasks with a certain priority. When tested, it seems to be returning an empty list, even when there are tasks that match.
Buggy Code:
// In TaskManager.java
public List<Task> findTasksByPriority(Priority priority) {
List<Task> matchingTasks = new ArrayList<>();
for (Task task : taskStore) {
// There is a bug on the next line
if (task.getPriority().equals(priority)) {
matchingTasks.add(task);
}
// The developer forgot to return the list!
}
return null; // Oops! This should be outside the loop.
}Your Task: Describe your debugging process. Where do you set the breakpoint? Do you “Step Over” or “Step Into”? What do you expect to see in the “Variables” panel as you step through the loop? How would you identify that the return statement is in the wrong place?
Exercise 2: Code Review Challenge
Good developers review each other’s code to catch bugs before they happen. Review the following new method for the TaskManager class. It’s missing some important error handling.
Code to Review:
// In TaskManager.java
public void assignTaskToUser(String username, int taskId) {
Task task = findTaskById(taskId); // This line can throw TaskNotFoundException
System.out.println("Assigning task '" + task.getTitle() + "' to user '" + username.toUpperCase() + "'.");
}Your Task:
- Identify two different types of
Exceptionthat could occur at runtime in this method. (Hint: What ifusernameisnull? What if thetaskIddoesn’t exist?) - Refactor the method. Add validation and the necessary
try...catchblocks to handle these errors gracefully. For the invalid username, throw a newIllegalArgumentException. For the task not being found, you should catch theTaskNotFoundExceptionthatfindTaskByIdalready throws. - Write the improved, robust version of the method in your notebook.
Exercise 3: Peer Instruction
This exercise is for discussion. Find a partner or a small group and choose one of the topics below. Each person should take a turn explaining their topic to the others. The goal is to explain it clearly and concisely.
Explain how to translate a single attribute and a single method from a UML Class Diagram into a Java class. Use the Task class as your example.
- How does UML’s
-visibility symbol translate to a Java keyword? - How do you create a “stub” for a method that has a return type but no logic yet?
Explain the difference between a built-in exception like IllegalArgumentException and a custom exception like TaskNotFoundException.
- When would you choose to throw
IllegalArgumentException? - What is the main benefit of creating your own custom exception?
Explain the performance difference between finding an item by ID in an ArrayList versus a HashMap.
- What is the “space-time trade-off” we made when we added the
HashMap? - In which situations would the
ArrayListstill be the better choice?
Explain why trying to remove an item from an ArrayList inside a for-each loop can cause a ConcurrentModificationException.
- What is the name of the object you should use to loop through a list if you need to remove items?
- What method do you call on that object to safely remove the current item?
Review Questions
Answer the following questions in your Microsoft Teams Student Notebook.
In the VS Code debugger, what information does the Call Stack panel provide?
You are paused on a line of code that calls a method you wrote. You suspect the bug is inside that method. Which debugger control should you use: “Step Over” or “Step Into”? Why?
What is the primary benefit of using a
HashMapto supplement anArrayListfor data storage in ourTaskManager?When is it necessary to use an
Iteratorto loop through a list instead of a standardfor-eachloop?What is the purpose of the
breakkeyword in aswitchstatement?A
NullPointerExceptionoccurs when you try to do what?
Reflect and Review
Reflect on your journey through Milestone 2. In your Microsoft Teams Student Notebook, answer the following:
- Connect: How did the debugging skills you learned in the first exercises help you when you had to implement the more complex logic for the
TaskManager? - Challenge: What was the most challenging concept for you in this milestone (e.g., exceptions,
HashMap,Iterator, debugging), and what step did you take to better understand it? - Confidence: Describe one skill you gained in this milestone that makes you feel more like a professional programmer.