PreLab 6: 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. Fill in the blanks in the code below to suggest activities as follows (use a cascading if):
      temp >= 80:       swimming
      60 <= temp < 80:  tennis
      40 <= temp < 60:  golf
      temp < 40:        skiing
    
    Be sure that your conditions are no more complicated than necessary.
      int temp;
      System.out.println("What's the temperature today?");
      temp = Keyboard.readInt();
    
      if (temp >= 80)
        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!");
    
    
    1. Declare a boolean variable extremeTemp and write an assignment statement that makes extremeTemp true if the temperature is greater than 95 or less than 20, false otherwise.
      
      
      
      
      
    2. Write an if statement that prints "Visit our shops!" if the value in variable temp is extreme as determined by the extremeTemp variable. Don't worry about what to do if the temperature is not extreme.
      
      
      
      
      
    3. Modify the code at the beginning of the handout that prints the activities for Lake Lazydays Resort to use extremeTemp to print "Visit our shops!" when the temp is too high or too low for other activities (just note your changes on the code in (1)). You should still have just one cascading if.

  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 truth tables to determine whether each pair of expressions is equivalent.
    1. NOT q OR p
      p OR NOT(q OR p)
      
      
      
      
      
      
      
      
      
      
      
      
      

    2. (p AND r) OR (q AND NOT r)
      (p AND q AND NOT r)