Java Course 4: Selection

if statement

Scanner scan = new Scanner(System.in); 
System.out.println("How old are you?"); 
int age = scan.nextInt(); 
if (age < 18) { 
            System.out.println("You're too young to vote"); 
} else { 
            System.out.println("You're old enough to vote"); 
}

Operators

  • <, >, <=, >=
  • .equals() (for Strings)
  • .equalsIgnoreCase()
  • ! not
  • | or
  • & and

Examples

  • if (age>10 & age<19)
  • if(surname.equals(“Ballantyne”))
  • if(surname.equalsIgnoreCase(“Ballantyne”))
  • if(!answer.equals(“quit”))

Questions

Don’t forget to paste your code beneath the questions!

  1. Write a program that asks a user to input their BMI (body mass index) and then outputs their BMI Category as follows: Underweight = <18.5, Normal weight = 18.5–24.9, Overweight = 25–29.9, Obesity = BMI of 30 or greater
  2. Ask the user to input the name and age of two people. e.g. George, 32, Jill, 35. Get it to print out who is the youngest/oldest e.g. George is younger than Jill
  3. Honorific Generator: Write a program that accepts as input a person’s sex and age, and outputs the honorific Mr, Ms, Miss or Master
  4. Days in the month: Input the name of the month and whether or not it’s a leap year, then output the number of days in that month
  5. Write a quiz with 2 questions. Print out the users score out of 2 at the end of the quiz
  6. Write a password checker. Give the user three chances to enter the correct password.

Extension

  1. Personality test: Look at the personality test here http://www.personalityquiz.net/profiles/typology/index.htm Write a program that replicates this test.
  2. Allow the user to input a number. Output if the number is odd or even
  3. Search for the java switch case statement. Rewrite the last question using switch case.

Leave a Comment