---
title: "Jam 01 - Exercise 5 - Decision Making with Java"
tags:
- 3 ๐งช in testing
- 4 ๐ฅณ done
- jam01
- java
- control-flow
created: 2025-01-20
updated: 2025-01-20
---
<!-- markdownlint-disable line-length single-h1 no-inline-html -->
<!-- markdownlint-configure-file { "ul-indent": { "indent": 4 }, "link-fragments": {"ignore_case": true} } -->
# Exercise 5 - Decision Making with Java
{%hackmd dJZ5TulxSDKme-3fSY4Lbw %}
## Overview - Exercise 5
Now that we can get input from users, let's make our programs more interactive by having them make decisions. In Java, we use `if`, `else if`, and `else` statements to create different paths through our code based on conditions. This is called branching because the program's execution branches into different paths based on the conditions we specify.
For example, a simple if statement looks like this:
```java
if (temperature < 32) {
System.out.println("It's freezing outside!");
} else if (temperature < 70) {
System.out.println("It's cool outside.");
} else {
System.out.println("It's warm outside!");
}
```
:::info
๐ **Note**: You'll learn much more about if statements, boolean expressions, and complex conditions in your upcoming zyBooks readings. For now, we'll keep it simple with basic numeric comparisons.
:::
In this exercise, we'll modify our program to respond differently based on the user's age, using multiple conditions to provide specific responses.
## Required Steps - Age
- Prompt the user for their age
- If the user is < 20 years old, then output "You're a teenager."
- Else, if the user is >= 20 and < 30 then output "You're in your 20s."
- Otherwise, output "You're at least 30."
### Example Output - Simple Branching
```text
What is your name?
Tom
Here is your operating system info, Tom
OS Name: Mac OS X
OS Architecture: aarch64
OS Version: 14.6.1
Tom, your name is 3 characters long.
What is your age?
38
Tom, you're at least 30.
```
:::info
๐ **Java Best Practices**
As you start writing Java code, focus on these key practices:
- **Naming Conventions**
- Variables: Use camelCase (e.g., `userName`, `studentAge`)
- Classes: Use PascalCase (e.g., `HelloWorld`, `Scanner`)
- Constants: Use UPPER_SNAKE_CASE (e.g., `MAX_SIZE`, `PI_VALUE`)
- **Code Organization**
- Use consistent indentation (IntelliJ helps with this)
- Group related code together
- Keep methods focused on a single task
- **Self-Documenting Code**
- Choose descriptive variable names
- Write clear, focused methods
- Add comments for complex logic only
:::
## Code Style and Checkstyle
Writing clean, readable code is crucial for software development. While we're starting with basic style rules now, you'll soon be using Checkstyle, a tool that automatically verifies your code follows Java conventions. This will help you develop good coding habits and ensure your code is professional and maintainable.
:::warning
๐ง **Style Note**
Your code will be evaluated on style starting in the next jam. Take time now to practice these conventions!
:::
## Save Your Work - Exercise 5
Verify what files are uncommited:
```bash
git status
```
Stage your changes (This should be the file shown in `git status` as modified)
Feel free to use a different message as long as it's descriptive
```bash
git add src/main/java/jam01/HelloWorld.java
```
Commit your work
```bash
git commit -m "jam01: Complete branching exercise"
```
Your working directory should be clean.
> ๐ **Checkpoint**: Before continuing, verify:
>
> - Your program compiles without errors
> - Program handles all age ranges correctly
> - The program displays the user's name with each response
> - You're following Java naming conventions
> - Your code is properly indented