3.6 Lab - Switch Statements in Java

Introduction

In this lab, you will learn how to use switch statements in Java. The switch statement provides an alternative to the if-else structure when dealing with multiple possible values of a variable. This control structure can make your code more readable when you have a set of predefined values to check. You will write a program that utilizes the switch statement to make decisions based on user input.

Objectives

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

  1. Understand the structure and syntax of a switch statement in Java.
  2. Use the switch statement to handle multiple cases in a program.
  3. Write a Java program that uses the switch statement to evaluate user input.
  4. Compile and run a Java program using switch 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 SwitchExample.java:
    nano SwitchExample.java
    
  2. Type the following Java code into the file. This program will ask the user for a number between 1 and 7 and then output the corresponding day of the week:
    import java.util.Scanner;
    
    public class SwitchExample {
        public static void main(String[] args) {
            // Create a Scanner object for user input
            Scanner scanner = new Scanner(System.in);
    
            // Prompt the user to enter a number between 1 and 7
            System.out.print("Enter a number (1-7) to select a day of the week: ");
            int day = scanner.nextInt();
    
            // Switch statement to determine the day of the week
            switch (day) {
                case 1:
                    System.out.println("Sunday");
                    break;
                case 2:
                    System.out.println("Monday");
                    break;
                case 3:
                    System.out.println("Tuesday");
                    break;
                case 4:
                    System.out.println("Wednesday");
                    break;
                case 5:
                    System.out.println("Thursday");
                    break;
                case 6:
                    System.out.println("Friday");
                    break;
                case 7:
                    System.out.println("Saturday");
                    break;
                default:
                    System.out.println("Invalid input! Please enter a number between 1 and 7.");
                    break;
            }
        }
    }
    
  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 SwitchExample.java
    
  2. If there are no errors, this command will create a file named SwitchExample.class. Verify this by running:
    ls
    
Step 4: Run the Java Program
  1. Run the compiled program using the java command:
    java SwitchExample
    
  2. The program will prompt you to enter a number between 1 and 7. Based on the number you enter, the program will output the corresponding day of the week. For example:
    Enter a number (1-7) to select a day of the week: 3
    Tuesday
    
Step 5: Modify the Program
  1. Open the SwitchExample.java file again using Nano:
    nano SwitchExample.java
    
  2. Modify the program to ask the user to choose a number between 1 and 12 and display the corresponding month of the year using a switch statement:
    System.out.print("Enter a number (1-12) to select a month of the year: ");
    int month = scanner.nextInt();
    
    switch (month) {
        case 1:
            System.out.println("January");
            break;
        case 2:
            System.out.println("February");
            break;
        case 3:
            System.out.println("March");
            break;
        case 4:
            System.out.println("April");
            break;
        case 5:
            System.out.println("May");
            break;
        case 6:
            System.out.println("June");
            break;
        case 7:
            System.out.println("July");
            break;
        case 8:
            System.out.println("August");
            break;
        case 9:
            System.out.println("September");
            break;
        case 10:
            System.out.println("October");
            break;
        case 11:
            System.out.println("November");
            break;
        case 12:
            System.out.println("December");
            break;
        default:
            System.out.println("Invalid input! Please enter a number between 1 and 12.");
            break;
    }
    
  3. Save, exit, recompile, and run the program again to see the updated behavior:
    javac SwitchExample.java
    java SwitchExample
    
Step 6: Add More Functionality
  1. Modify the program to include both options (day of the week and month of the year). Use a switch statement to prompt the user to first choose between the two categories, and then proceed to either the day or month selection:
    System.out.print("Enter 1 for day selection, 2 for month selection: ");
    int choice = scanner.nextInt();
    
    switch (choice) {
        case 1:
            // Ask for day
            System.out.print("Enter a number (1-7) to select a day of the week: ");
            int day = scanner.nextInt();
    
            switch (day) {
                case 1:
                    System.out.println("Sunday");
                    break;
                case 2:
                    System.out.println("Monday");
                    break;
                case 3:
                    System.out.println("Tuesday");
                    break;
                case 4:
                    System.out.println("Wednesday");
                    break;
                case 5:
                    System.out.println("Thursday");
                    break;
                case 6:
                    System.out.println("Friday");
                    break;
                case 7:
                    System.out.println("Saturday");
                    break;
                default:
                    System.out.println("Invalid input! Please enter a number between 1 and 7.");
                    break;
            }
            break;
        case 2:
            // Ask for month
            System.out.print("Enter a number (1-12) to select a month of the year: ");
            int month = scanner.nextInt();
    
            switch (month) {
                case 1:
                    System.out.println("January");
                    break;
                case 2:
                    System.out.println("February");
                    break;
                case 3:
                    System.out.println("March");
                    break;
                case 4:
                    System.out.println("April");
                    break;
                case 5:
                    System.out.println("May");
                    break;
                case 6:
                    System.out.println("June");
                    break;
                case 7:
                    System.out.println("July");
                    break;
                case 8:
                    System.out.println("August");
                    break;
                case 9:
                    System.out.println("September");
                    break;
                case 10:
                    System.out.println("October");
                    break;
                case 11:
                    System.out.println("November");
                    break;
                case 12:
                    System.out.println("December");
                    break;
                default:
                    System.out.println("Invalid input! Please enter a number between 1 and 12.");
                    break;
            }
            break;
        default:
            System.out.println("Invalid input! Please choose 1 or 2.");
            break;
    }
    
  2. Save, exit, recompile, and run the program to test the updated functionality.
Step 7: 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 the switch statement to handle multiple conditions in Java. You explored how switch is used as an alternative to if-else for checking multiple values. You implemented a program that allowed users to choose a day of the week or a month of the year, and expanded it to include more user input options. The switch statement is a powerful tool for simplifying decision-making logic when you have multiple discrete values to evaluate.