Colorado State University

CS160: Foundations in Programming: Spring 2015


Recitation R5 - Conditional Basics


The goal of this lab is to introduce flow of control and logical expressions. 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 learn about:

Getting Started

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

Part 1: Experimenting with if

IMPORTANT: Before leaving, be sure to submit your work to RAMCT. There's a dropbox for this recitation on the left-hand side.

Copy and paste this code into Eclipse and run it:
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int age = keyboard.nextInt();

if (age > 20) // no semicolon here!!!
{
	System.out.println("You can legally buy and consume alcohol in the USA.");
}		

keyboard.close(); // not necessary, but good practice
System.out.println("End of program.");
Notice that this code does the exact same thing:
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int age = keyboard.nextInt();

if (age >= 21) 
{
	System.out.println("You can legally buy and consume alcohol in the USA.");
}		

keyboard.close();
System.out.println("End of program.");
What's different?

Part 2: More with if

Consider this code:
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your age: ");
int age = keyboard.nextInt();

if (age == 21) 
{
	System.out.println("Wow!  You are 21 years old!");
}		

if (age != 21) 
{
	System.out.println("Interesting!  You are NOT 21 years old!");
}	

keyboard.close();
System.out.println("End of program.");
What happens?

Part 3: if.. else..

Wouldn't it be nice to combine Part 2 into one block? Well, if we add an else, we can do just that (note: from now on, I'm omitting the code that creates a Scanner, gets input from the user, and closes the Scanner.):
if (age == 21) 
{
	System.out.println("Wow!  You are 21 years old!");
}
else
{
	System.out.println("Interesting!  You are NOT 21 years old!");
}		

System.out.println("End of program.");
What's going on now?

Now try this:
if (age >= 21) 
{
	System.out.println("You can legally buy and consume alcohol in the USA.");
}
else
{
	System.out.println("You are under 21 and cannot legally buy and consume alcohol in the USA.");
}		

System.out.println("End of program.");
Cool, huh?

Part 4: if.. else if..

Now suppose I want to write something special for being 21 and 18, but nothing for all other ages. How could we do this? Well, there's else if too:
if (age == 21) 
{
	System.out.println("Boom!  You are 21!");
}
else if (age == 18)
{
	System.out.println("Hey-o!  You're 18 years old!");
}		

System.out.println("End of program.");
In fact, we can add as many else if's as we want:
if (age == 21) 
{
	System.out.println("You are 21 and the same age as Miley Cyrus!  Not sure if that's good or bad.");
}
else if (age == 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.");
}	
else if (age == 20)
{
	System.out.println("You're 20 years old and the same age as Justin Bieber!  Has he retired yet?");
}	
else if (age == 16)
{
	System.out.println("You're 16 years old and the same age as Jaden 'Don't call me Will' Smith!");
}		

System.out.println("End of program.");

Part 5: Bring it together: if.. else if.. else..

We can even use all three things together to cover all the bases:
if (age == 21) 
{
	System.out.println("You are 21 and the same age as Miley Cyrus!  Not sure if that's good or bad.");
}
else if (age == 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.");
}	
else if (age == 20)
{
	System.out.println("You're 20 years old and the same age as Justin Bieber!  Has he retired yet?");
}	
else if (age == 16)
{
	System.out.println("You're 16 years old and the same age as Jaden 'Don't call me Will' Smith!");
}
else
{
	System.out.println("Oops! You're not the age of any people I know.");
}		

System.out.println("End of program.");
Now we always will get some response from our program!

Part 6: Question to solve

Question: Does this code have a problem? If so, what is it and how could one fix it?:
boolean canDrive;

if (age >= 16) 
{
	canDrive = true;
}	


if (canDrive)
{
	System.out.println("According to our records, you can legally drive a car in the USA.");
}
else
{
	System.out.println("According to our records, you are prohibited from driving a car in the USA.");
}

System.out.println("End of program.");
Question: How could you rewrite this so if the user enters 22, both facts are printed?:
if (age >= 21) 
{
	System.out.println("You can legally buy and consume alcohol in the USA.");
}
else if (age >= 18)
{
	System.out.println("Can legally drive a car in the USA.");
}	
else 
{
	System.out.println("You're under 18 years old.");
}

System.out.println("End of program.");

Part 7: Getting even more control with && and || (logical AND and OR)

Now we're going to slightly change our program to do something else:
if ( (age > 11) && (age < 20) ) // the extra parentheses help readability, but aren't necessary...
{
	System.out.println("You are a teenager!");
}
else if (age > 100 || age < 2) // ...as demonstrated here
{
	System.out.println("You are either Gandalf or learning to walk.");
}	
else 
{
	System.out.println("I have nothing interesting to say.");
}

System.out.println("End of program.");

Optional bonus challenge for brave souls!

Use the code below as a starting point and do the following (the TAs are here to help too!):
  1. Fill in the 2 blanks so the code works.
  2. Make your program print something witty/zen/prophetic if the program doesn't recognize the fullName.
  3. Make your program recognize a new name and give responses for whether that person is happy or not.
  4. Change the program so that you use embedded if.. else.. statements. (Say what?!)
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a full name: ");
String fullName = keyboard.______________.toLowerCase();
System.out.print("Is the person happy? (y/n): ");
char happyAnswer = keyboard.next().toLowerCase().charAt(0);
boolean isHappy = (happyAnswer == 'y');

if ( fullName.equals("chris wilcox") && isHappy)
{
	System.out.println("Looks like Chris had his chai from the Alley Cat!");
} 
else if (____________ && !isHappy)	
{
	System.out.println("Chris needs to visit the Alley Cat to get his chai fix!!");
} 


keyboard.close();
System.out.println("End of program.");

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