CSU Banner

CS 150, Spring 2018

Lab 6 - Switch Statements

Wednesday, February 21, 2018


Lab 5 Regrade: If you need a regrade for last week's lab, please open Lab 5 requirements and show one of the TAs your completed work within the first 5 minutes of class. The TAs will not accept regrades after this time. Additionally, if you forgot to take the quiz last week for the single attendance point, your maximum possible grade for the previous lab is now 3 out of 4.

Objectives of this Lab

  1. Sign up for CodingBat in preparation for this Friday's assignment,
  2. learn about switch statements in Java, and
  3. using switch statements, print specific output to the console.

Getting Started

First in today's lab you will sign up for CodingBat. Visit CodingBat here to do so. In the upper right-hand corner click create account. Enter your CSU email address, a password of your choice, and in the Name field enter your EID. Again, in the Name field enter your EID (i.e. the alias of your name at CSU - usually the name in your email address). Next, log in to CodingBat and go to the prefs page - link is in the upper right-hand corner. In the Teacher Share field enter the following email address: waker@colostate.edu

Keep CodingBat open as you complete today's lab - the TAs will be checking for proper set up as part of your grade today. As a demonstration, your TA will walk through how to do Warmup-1>or35 and Warmup-1>intMax. CodingBat uses return statements for your answers. At this point in the class you may think of the return statement as a print statement, which we normally use to show program answers to a user.

Next, create a new Java Project named Lab6, and make a class named Lab6 with a main method. Your TA will do a quick review of the switch statement format of. This program will ask the user how much they like coffee and tea and will then, utilizing a switch statement, 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, 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. 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: "
  4. 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.
  5. Assign the value entered by the user to the variable likeCoffee.
  6. Next, write a statement that prints a blank line to the console. This will improve readability for the user.
  7. 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: "
  8. Again, make sure the prompt appears in the console by starting a new line after the word "value".
  9. Assign the value entered by the user to the variable likeTea.
  10. Write a statement that prints a blank line to the console.

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

Basic Program Structure

public class Lab6 {

    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 Lab6

Sample Output (Two Program Runs Shown)

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.



© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building