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
- Sign up for CodingBat in preparation for this Friday's assignment,
- learn about switch statements in Java, and
- 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
- 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.
- 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. This means, 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. 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.
- Make sure your Lab6.java compiles and produces 8 lines of output (this includes 2 blank lines).
- Have your TA review the information you entered in CodingBat.
- Log in to Canvas and take the associated attendance quiz.
- Remember to take the quiz for the attendance point even if you do not finish the lab.
© 2018 CS150 Colorado State University. All Rights Reserved.