3.2 Lab - If Statements in Java

Introduction

In this lab, you will learn how to use if statements to make decisions in a Java program. The if statement is one of the fundamental control flow structures in programming, allowing you to execute code conditionally based on specific conditions. You will write a program that takes user input and makes decisions using if, else if, and else statements.

Objectives

By the end of this lab, you will be able to:

  1. Understand the syntax and structure of if statements in Java.
  2. Use if, else if, and else to create decision-making logic.
  3. Write a Java program that makes decisions based on user input.
  4. Compile and run a Java program that utilizes if statements.

Lab Steps

Step 1: Access the Linux Terminal
  1. Open the terminal on your Linux system.
  2. Ensure you are in your home directory by running the command:
    cd ~
    
Step 2: Create and Edit a Java File
  1. Create a new Java file using Nano called IfStatementExample.java:
    nano IfStatementExample.java
    
  2. Type the following Java code into the file:
    import java.util.Scanner;
    
    public class IfStatementExample {
        public static void main(String[] args) {
            // Create a Scanner object for input
            Scanner scanner = new Scanner(System.in);
    
            // Prompt the user to enter their age
            System.out.print("Enter your age: ");
            int age = scanner.nextInt();
    
            // If statement to check the age
            if (age < 13) {
                System.out.println("You are a child.");
            } else if (age >= 13 && age < 18) {
                System.out.println("You are a teenager.");
            } else if (age >= 18 && age < 65) {
                System.out.println("You are an adult.");
            } else {
                System.out.println("You are a senior.");
            }
        }
    }
    
  3. Save the file by pressing CTRL + O, then hit Enter to confirm the filename.
  4. Exit Nano by pressing CTRL + X.
Step 3: Compile the Java Program
  1. In the terminal, compile the Java program using the javac command:
    javac IfStatementExample.java
    
  2. If there are no errors, this command will create a file named IfStatementExample.class. Verify this by running:
    ls
    
Step 4: Run the Java Program
  1. Run the compiled program with the java command:
    java IfStatementExample
    
  2. The program will prompt you to enter your age. Based on the age you enter, the program will categorize you as a child, teenager, adult, or senior. For example:
    Enter your age: 25
    You are an adult.
    
Step 5: Modify the Program
  1. Open the IfStatementExample.java file again using Nano:
    nano IfStatementExample.java
    
  2. Modify the program to add more conditions. For example, you could add a condition for people exactly 18 years old:
    if (age < 13) {
        System.out.println("You are a child.");
    } else if (age >= 13 && age < 18) {
        System.out.println("You are a teenager.");
    } else if (age == 18) {
        System.out.println("You are exactly 18 years old.");
    } else if (age > 18 && age < 65) {
        System.out.println("You are an adult.");
    } else {
        System.out.println("You are a senior.");
    }
    
  3. Save, exit, recompile, and run the program again to see the updated behavior.
Step 6: Add More Inputs
  1. Modify the program to ask the user for more information, such as their favorite number, and add additional conditions:
    System.out.print("Enter your favorite number: ");
    int favoriteNumber = scanner.nextInt();
    
    if (favoriteNumber == 7) {
        System.out.println("7 is a lucky number!");
    } else {
        System.out.println("That's a nice number.");
    }
    
  2. Save, exit, recompile, and run the program. The program should now prompt for both age and a favorite number, making decisions based on both inputs.
Step 7: Test Boundary Conditions
  1. Run the program multiple times with different input values to test various conditions, such as edge cases (e.g., 12, 13, 17, 18, 65).
Step 8: Cleanup (Optional)
  1. Once you're done, clean up the directory by removing the .class files:
    rm *.class
    

Summary

In this lab, you learned how to use if statements to make decisions in a Java program. You wrote a program that uses if, else if, and else statements to categorize users based on their age. You also practiced expanding the program by adding more conditions and inputs. Understanding control flow with conditional statements is crucial to creating dynamic and interactive Java programs.