PreLab 7: Selection and Type boolean

  1. As activity directory at Lake Lazydays Resort, it is your job to suggest appropriate activities to guests based on the weather. You recommend that when the temperature is greater than 95 or less than 20 people should visit the resort shops. Otherwise you recommend the following activities:
      temp >= 80:         swimming
      60 <= temp < 80:    tennis
      40 <= temp < 60:    golf   
      20 <= temp < 40:    skiing 
    Fill in the blanks in the code below as directed in the comments. The if should be a cascading if with conditions that are no more complicated than necessary.
      int temp;
      System.out.println("What's the temperature today?");
      temp = scan.nextInt();
      // Declare a boolean variable extremeTemp and assign it an
      // expression that is true when the temperature is greater than 95 or
      // less than 20
      // Complete the cascading if (points will be deducted for unnecessary 
      // comparisons - use the boolean variable declared above where appropriate)
      if ( _________________________________ )
        System.out.println ("Visit our shops!");
      else if ( ________________________________________ )
        System.out.println("How about a dip in the pool?");
        System.out.println("Go grab a racquet!");
        System.out.println("It's a perfect day for golf.");
        System.out.println("Let's hit the slopes!");

  2. Rewrite each condition below in valid Java syntax (give a boolean expression):
    1. x > y > z



    2. neither x nor y is equal to 0



    3. both x and y are equal to 0



    4. x is greater than y but less than z



  3. Use the laws of logic to write a Java expression equivalent to the following without using the not (!) operator:
         !(age >= 65 && income < 20000)
  4. Do problem #8 (parts a - e) on the Logic and Programming Exercises handout
  5. A loop is a programming structure that allows a sequence of statements to be repeated. The number of times the statements are repeated depends on a condition. A while statement in Java is a looping structure with syntax as follows:
            while (  boolean condition  )
              ... sequence of statements to be repeated ...
              (This is called the body of the loop. It is
               executed when the boolean condition is true.)
    In Lab 6 you used an if statement to check that a test grade entered was valid (in the range 0 - 100). A while loop could have been used to keep asking the user to enter a score until one in the correct range was entered. The loop would be as follows:
            System.out.print("Enter " + name + "'s test score: ");
            test1 = scan.nextInt();
            while ( _____________________________________________________ )
              System.out.println (test1 + " is not a valid score.");
              System.out.print ("Enter a valid score in the range 0 - 100: ");
              test1 = scan.nextInt();
    The body of the loop should execute when the value of test1 is not valid. Hence, the blank should be the boolean expression for test1 NOT in the range 0 - 100. Fill in the blank with the correct expression in valid Java syntax.