CSU Banner

CS 150, Fall 2017

Lab 4 - Switch Statements

Monday - September 18, 2017


Objectives of this Lab

  1. Practice using the Scanner class and handling user input,
  2. learn about switch statements in Java, and
  3. using switch statements, print specific output to the console.

Getting Started

Create a new Java Project named R4, and make a class named R4 with a main method. Your TA will do a quick review of how to instantiate and use Scanner class methods, as well as review the format of a switch statement. This program will ask the user how much they like coffee and tea and will then, utilizing switch statements, print program results to the console based on the user's answers.

Switch Statements

This program introduces switch statements, which are really just a more efficient way of writing conditionals. You will write two switch statements for this program. You can write switch statements for integer values, including characters, or String values (which we will see in the next two weeks). Here are the instructions:
    Part One
  1. Declare two integer variables named likeCoffee and likeTea and initialize both to zero.
  2. Declare and initialize a Scanner object, with a name of your choice, to read from the keyboard.
  3. NOTE: Remember to import the Scanner library to be able to use Scanner class methods.
  4. Next write code that prints the following prompt to the console:
    "On a scale of 1 to 5, please enter the value
    that corresponds to how much you like coffee: "
  5. Make sure the prompt appears in the console with a new line after the word value. There are two ways to do this, either by using two print statements or including a new line character (\n) in a specific place.
  6. Assign the value entered by the user to the variable likeCoffee.
  7. Next, write a statement that prints a blank line to the console. This will improve readability for the user.
  8. Now write code that prints the prompt regarding tea to the console:
    "On a scale of 1 to 5, please enter the value
    that corresponds to how much you like tea: "
  9. Again, make sure the prompt appears in the console by starting a new line after the word value.
  10. Assign the value entered by the user to the variable likeTea.
  11. Write a statement that prints a blank line to the console.

  12. Part Two
  13. Write a switch statement that does the following:
  14. Write a second switch statement that does the following:
  15. Lastly, close your Scanner object with the statement variableName.close();. While it is not necessary to close your Scanner object, it is good practice.

Basic Program Structure

public class R4 {

    public static void main(String[] args) {
        // Declare program variables.

        // Declare and initialize a Scanner object.

        // Prompt user.

        // Assign likeCoffee variable.

        // Prompt user.

        // Assign likeTea variable.

        // Switch block for likeCoffee.

        // Switch block for likeTea.

    } // end main
} // end class R4

Sample Output

On a scale of 1 to 5, please enter the value
that corresponds to how much you like coffee: 1

On a scale of 1 to 5, please enter the value
that corresponds to how much you like tea: 6

You do not like coffee.
You did not enter a value 1 through 5 for tea.



On a scale of 1 to 5, please enter the value
that corresponds to how much you like coffee: 3

On a scale of 1 to 5, please enter the value
that corresponds to how much you like tea: 5

Sometimes you like coffee.
You love tea.



CS Banner
CS Building