Colorado State University

CS160: Foundations in Programming: Spring 2015


Recitation R6 - Switch Statements


The goal of this lab is to introduce the switch statement as another way to do flow of control. The TA will work through each part with the class. There is an optional final section for those seeking a challenge.

In this recitation we will:

Getting Started

Create a new Java Project named R6, and make a class named R6.

Part 1: switch statement -- another way to do flow of control

Sometimes, people prefer using a switch to do flow of control:
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int age = keyboard.nextInt();

switch (age)
{
	case 21:
		System.out.println("You are 21 and the same age as Miley Cyrus!  Not sure if that's good or bad.");
		break;
	case 18:
		System.out.println("You're 18 years old! and the same age as Zendaya.  No idea who she was -- I had to Google her.");
		break;
 	case 20:
		System.out.println("You're 20 years old and the same age as Justin Bieber!  Has he retired yet?");
		break;
	case 16:
		System.out.println("You're 16 years old and the same age as Jaden 'Don't call me Will' Smith!");
		break;
	default:
		System.out.println("Oops! You're not the age of any people I know.");
		break;
}		

keyboard.close();
System.out.println("End of program.");
We did the same thing in R5, but this time we used switch instead of if.. else if.. else... You can also use chars and Strings in your switch (see Part 2 for an example with a String and Part 3 for a char example).

Part 2: Using a String object in a switch statement

We can also use a String in our switch to do flow of control:
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your first name: ");
String firstName = keyboard.next();

switch (firstName)
{
	case "Miley":
		System.out.println("Do you like to twerk?");
		break;
 	case "Justin":
		System.out.println("I have Bieber-fever.");
		break;
	case "Chris":
		System.out.println("You must love chai from the Alley Cat.");
		break;
	case "Arnold":
		System.out.println("Get to da choppa!!");
		break;
	default:
		System.out.println("Sorry mate, I don't recognize your first name.");
		break;
}		

keyboard.close();
System.out.println("End of program.");
Notice that the format is the same; the only change is data type we use.

Part 3: Why the 'break' is important

Try using this complete chunk of code:
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a letter: ");
char myChar = keyboard.next().charAt(0); // what's going on here?

switch (myChar)
{
	case 'a':
		System.out.println("a!");
	case 'b':
		System.out.println("b!");
 	case 'c':
		System.out.println("c!");
		break;
	case 'd':
		System.out.println("d!");
	default:
		System.out.println("I'm the fallback case.");
}		

keyboard.close();
System.out.println("End of program.");
What happens when you input 'a'? What happens when you input 'b'? What happens when you input 'c'? What happens when you input 'p'?

Part 4: Let's make a simple calculator

Start with this code:
Scanner keyboard = new Scanner(System.in);

// Here we print a welcome message
System.out.println("Welcome to a simple calculator program!");
System.out.println("---------------------------------------");
System.out.println("All we know how to do is add, subtract, multiply, divide, and compute modulo.");
System.out.println();

// Here we get input from the user
System.out.print("Please enter a the first number of your calculation: ");
double lhs = keyboard.nextDouble();

System.out.print("Please enter the operator (+|-|*|/|%): ");
// ADD: get operator using Scanner object

System.out.print("Please enter a the second number of your calculation: ");
// ADD: get the second number use the Scanner object

// Here we will calculate the result:
double result;

// ADD: write a switch statement that will calculate
// the correct result.


// Here we will print the result
System.out.println("---------------------------------------");
System.out.println("Result:");
// ADD: print the answer to the calculation using the
// so it looks like: "2.44 - 1.44 = 1.00"
System.out.println("---------------------------------------");


keyboard.close();
System.out.println("End of program.");
This code is not complete. Fill in the blanks to finish the program.

Optional, nefarious, and insidious challenges!

Add the following to the calculator program you did in Part 4:
  1. Add another operator: '^' that is used to raise a number to a power. Example: 3 ^ 4 = 81. You should be able to do this with decimal numbers as well, because 3.5 ^ 2.22 gives an answer on a normal calculator. (if you're curious about the math you can read more here)
  2. Figure out a way to gracefully handle the user typing in a bad operator (like '@'). Print an error message instead of the result.
  3. Figure out a way to print an error message when the user tries to divide by zero. Example: 6 / 0 = undefined.


You must submit your R6.java program to the drop box on RamCT.
© 2015 CS160 Colorado State University. All Rights Reserved.