CS 150, Fall 2017
Lab 4 - Switch Statements
Monday - September 18, 2017
Objectives of this Lab
- Practice using the Scanner class and handling user input,
- learn about switch statements in Java, and
- 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
- Declare two integer variables named likeCoffee and likeTea and initialize both to zero.
- Declare and initialize a Scanner object, with a name of your choice, to read from the keyboard.
- NOTE: Remember to import the Scanner library to be able to use Scanner class methods.
- 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: "
- 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.
- Assign the value entered by the user to the variable likeCoffee.
- Next, write a statement that prints a blank line to the console. This will improve readability for the user.
- 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: "
- Again, make sure the prompt appears in the console by starting a new line after the word value.
- Assign the value entered by the user to the variable likeTea.
- Write a statement that prints a blank line to the console.
Part Two
- Write a switch statement that does the following:
- Prints "You do not like coffee." if likeCoffee is 1.
- Prints "Sometimes you like coffee." if likeCoffee is 2 or 3.
- Prints "You often like coffee." if likeCoffee is 4.
- Prints "You love coffee." if likeCoffee is 5.
- Prints "You did not enter a value 1 through 5 for coffee." if the user enters anything else.
- NOTE: Remember you can employ fall through with switch statements. In effect, if multiple values of the switch variable correspond to the same thing, you only need to write the associated code once. See the chapter 3 class slides (page 12) for an example.
- Write a second switch statement that does the following:
- Prints "You do not like tea." if likeTea is 1.
- Prints "Sometimes you like tea." if likeTea is 2 or 3.
- Prints "You often like tea." if likeTea is 4.
- Prints "You love tea." if likeTea is 5.
- Prints "You did not enter a value 1 through 5 for tea." if the user enters anything else.
- 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.
- Make sure your R4.java compiles and produces 8 lines of output (this includes 2 blank lines).
- Have your TA review your output for credit as well as show your TA R3 output if you did not finish last week.
- Log in to Canvas and take the associated attendance quiz.