Skip to main content

Section 6.6 Assessment: Writing Selection Statements

Subgoals for Writing Selection Statements.

If Statement
  1. Define how many mutually exclusive paths are needed
  2. Order from most restrictive/selective group to least restrictive
  3. Write if statement with Boolean expression
  4. Follow with true bracket including action
  5. Follow with else bracket
  6. Repeat until all groups and actions are accounted for
OR Switch Statement
  1. Determine variable / expression for mutually exclusive ranges
  2. Write switch statement based on variable / expression
  3. Each range is a ‘case’
  4. Include break statements and default case if needed

Exercises Exercises

    1.

      Q1: Which of the following boolean expressions is appropriate to test if the age of a person is at least 18 and their weight is no more than 20 kilograms?
    • age > 18 && weight > 20
    • age < 18 && weight > 20
    • age >= 18 && weight >= 20
    • age >= 18 || weight < 20
    • age >= 18 && weight <= 20

    2.

      Q2: Given the following problem statement, what order should the following conditions be placed in?
      Problem: Any score that is less than 50 is considered outstanding. Any score that is 100 or more is considered invalid. Any score that is at least 50 less than 100 is acceptable.
      Condition 1: if (score < 50)
      Condition 2: if (score >= 100)
      Condition 3: if (score > 50 && score < 100)
    • 1 then 2 then 3
    • 2 then 1 then 3
    • 3 then 2 then 1
    • 3 then 1 then 2
    • 1 then 3 then 2
    • The order does not matter

    3.

      Q3: Which of the following pieces of code correctly solves the problem?
      Problem: If the length of the envelope at least 10 inches, then the price is determined by its weight. If the weight is less than 6 ounces then the price is $2.00; otherwise the price is $3.00. If the length of the envelope is less than 10 inches then the price is $1.00.
      1.     if (length < 10 && weight > 6.0)
            price = 1.0;
        else if (length >= 10)
            if (weight < 6.0)
               price = 2.0;
           else
           price = 3.0;
        
      2. if (length >= 10 && weight < 6.0)
            price = 2.0;
        else if (length >= 10)
            price = 3.0;
        else
            price = 1.0;
        
      3. if (length < 10)
            price = 1.0;
        if (length >= 10 && weight < 6.0)
            price = 2.0;
        if (length >= 10 && weight >= 6.0)
            price = 3.0
        
    • I only
    • II only
    • III only
    • II and III
    • I, II, and III

    4.

      Q4: Here is a solution to the following problem, however it is incorrect. How should the code be fixed to correct the errors?
      Problem: Given a variable, windDirection, the following values should be printed:
      N                      north
      S                      south
      E                      east
      W                      west
      all other values       unknown
      
      switch (windDirection) {			        // line 1
          case N: System.out.println("north");	// line 2
          case S: System.out.println("south");	// line 3
          case E: System.out.println("east");		// line 4
          case W: System.out.println("west");	    // line 5
          default: System.out.println("unknown");	// line 6
      }						                    // line 7
      
    • The datatype char should be added to line 1
    • break statements should be added to lines 2, 3, 4, and 5
    • single quotes should be around ’N’, ’S’, ’E’, and ’W’
    • both B and C should be done
    • remove the double quotes " " from the values to be printed

    5.

    Q5: Put the code in the right order to create a program that will determine the appropriate ticket price based on the time of day and age of the customer (both integers). If it is before 4pm (1600), then all tickets are $10.50. Otherwise if it is before 7pm (1900) and the customer is younger than 17 then the price is $12.00. If it is before 7pm and the customer is at least 17 then the price is $15.75. If it is 7pm (1900) or later, and the customer is younger than 17 then the price is $15.00. Otherwise the ticket price is $21.00.
You have attempted of activities on this page.