PreLab 7: Selection and Type boolean    Name____________________________________

  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. So far, we have worked with Java's GUI library, Swing , to create Frames, Panels and Labels. We can use these to generate static displays, but what we really want is some Interaction! Section 4.7 in your book introduces how GUIs respond to user events and section 4.8 deals with a specific GUI component: Buttons.

     

    1. Define in your own words the terms event and listener as they relate to Java.

       

       

       

       

       

       

    2. What kind of event does a JButton generate? _________________________________

       

    3. To make your program respond to an event generated by a JButton:

       

      1. You create an inner class that implements _______________________________

         

      2. This inner class must have a method called ________________________________

         

      3. What do you do to establish the relationship between the button and the inner class