CS160: Foundations in Programming: Spring 2015
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?
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?
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?
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?
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.");
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!
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.");
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.");
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.");