CS594 Midterm

  1. This exam is open-note, open book.
  2. You must answer all of the questions.
  3. Use the exam pages to answer the questions
  4. You must use Java to implement the coding questions.
  5. Good luck!

  1. (20 points)
    1. true: You should use a JPanel if you want to create a drawing window for the user.

    2. true: The (x,y) point at which Java starts drawing a text string is called the baseline.

    3. false: The RGB model is a subtractive/reflective color model.

    4. false: If your class implements a listener interface it only has to implement those methods which it cares about (i.e., are not empty).

    5. true: You can cause a box layout manager to put different sized horizontal gaps between its elements.

    6. true: A grid layout manager does not respect an object's preferred size.

    7. true: Sans-serif fonts do not have decorations or curls.

    8. false: Dark objects are more effective at drawing the user's attention than light objects.

    9. false: The human eye is an order of magnitude better at perceiving the contrast between the hues of two objects, than the brightness of two objects

    10. true: Menus should be used to list the set of available commands when screen space is at a premium.

  2. (20 points) Each of the following questions will ask you to collect some type of data or provide some type of operation to the user. For each question, select the most appropriate widget to use for that question. You may justify your answer if you wish but it is not necessary. If you do so limit yourself to no more than 2 sentences. You should select each answer from the following list of widgets:

    1. command buttons You want to put a sequence of possible actions at the bottom of a dialog box, such as "Save", "Cancel", "Delete". These actions will be immediately executed when selected by the user.

    2. slider You want to assign a grade of 0-20 to a student's solution.

    3. check boxes You want the user to select the items they want included in a cable package, which can include internet, video, and telephone service. The choices are not mutually exclusive.

    4. radio buttons You want to allow the user to select a tool from a tool pallette. Nothing executes when the tool is selected. Instead, you may now use the tool in an editor window. You should use radio buttons because you are asking the user to choose one tool from a set of mutually exclusive tools. You should not use a command button because you are merely selecting a tool, you are not executing it.

    5. menu You want the user to select a city from a list of cities for the state in which they live. The number of cities from which to select will typically exceed 10. Radio buttons and check boxes should normally be limited to no more than 5-7 items. With the maximum number of cities unknown, but the minimum number known to be 10, it is best to use a menu, in order to conserve screen real estate.

  3. (12 points) Which Java layout manager is the best and simplest choice for each of the following situations. Please limit yourself to the following five layout managers:

    1. Grid You want to create a tool pallette with equal sized chips for each of the tools.

    2. Flow You want to lay out a set of command buttons horizontally at their natural size, with a gap of 10 pixels between buttons. There are no other alignment or justification conditions.

    3. Gridbag You want to lay out the elements of a form

    4. Box You want to lay out 5 check boxes by vertically stacking them. They should maintain their preferred size, no matter how big the window gets.

  4. (18 points) Fill in the blanks for each of the following questions:

    1. Observer pattern. This design pattern is being used when 1) views register with a model and the model notifies views of changes to its state information, 2) listeners register with an event generator and the event generator notifies the listeners as it produces events:

    2. 5 updates. If the user wants to drag an object across the screen, experiments have shown that this number of updates per second is the lower bound for acceptable interaction.

    3. HorizontalGlue. You would use this type of object in a box layout manager to create elastic space between two objects.

    4. If you want a Java component to listen for key presses you must 1) add a

      key listener to the component, and 2) you must use the

      mouseEntered method in a mouse listener to request the focus.

    5. view This portion of a graphical application handles the graphical presentation of the model's data.

    6. CENTER This element of a border layout manager gets all the space not assigned to the other elements.

    7. Sans Serif This type of font should be used to display text items on a computer display.

    8. command objects. In object-oriented languages that do not have function pointers, such as Java, these types of objects are created explicitly to encapsulate callback functions.

    9. 5 to 7 items is ideally the upper limit on the number of choices you would want to provide for button groups or sections of a menu, since if you observe this limit, most users can spot the choices they want instantly (i.e., in O(1) time). If your button groups have more than this number of choices, users will be forced to scan the list of choices and their reaction time will degrade to O(n), where n is the number of items in your button group.

  5. (15 points) You are given the following declarations for the properties of a rectangle and its label:
    	Rectangle rect;
    	String label;
    	
    Write a paintComponent method that accomplishes the following tasks:
    1. Draws the rectangle as a blue rectangle with a black boundary.
    2. Draws the label 10 pixels beneath the rectangle and horizontally centered with respect to the rectangle. The top of the text string should be 10 pixels beneath the rectangle.
      public void paintComponent(Graphics gr) {
        Graphics2D g = (Graphics2D)gr;
        super.paintComponent(g);
        FontMetrics fm = g.getFontMetrics(font);
    
        // to get a boundary to appear properly over a filled object, you
        // must first fill the object and then draw the boundary over it
        g.setColor(Color.blue);
        g.fill(rect);
        
        g.setColor(Color.black);
        g.draw(rect);
    
        // In order to get the top of the string to be 10 pixels below the
        // bottom of the rectangle, we must set the baseline to be 10 pixels
        // plus the font's ascent below the bottom of the rectangle.
        g.drawString(label, rect.getX() + rect.getWidth()/2 - fm.stringWidth(label) / 2,
    		 rect.getY() + rect.getHeight() + 10 + fm.getAscent());
      }
    

  6. (15 points): Write a piece of code to:

    1. count the number of times a mouse enters the above rectangle. The mouse must leave the rectangle and then re-enter it for it to be counted as another entry (hint: you will need to write a listener object that listens for MouseMotion events).
    2. adds the listener object you just created to the JPanel that contains the rectangle.

    Here is some code to provide you context:

    	class Counter extends JPanel {
    	    Rectangle rect;
    	    String label;
    	    // need a state variable to keep track of whether the mouse
                // was last seen inside or outside the rectangle. It is helpful
                // to use named constants to keep track of this state information.
                final int OUTSIDE = 1;
                final int INSIDE = 2;
    
                int state = OUTSIDE;
    	    int entered = 0; // counts number of times rect is entered
                
                public Counter(Rectangle r, String l) {
    	       rect = r;
    	       label = l;
    	       
    	       // we need to monitor the mouseMoved event in the 
    	       // MouseMotionListener. You might be tempted to use
    	       // a MouseListenere and monitor the 
    	       // mouseEntered and mouseExited events, but these events
    	       // do not work with custom
    	       // graphics. They only apply to JComponents
    	       addMouseMotionListener(new MouseMotionAdapter() {
    	         public void mouseMoved(MouseEvent e) {
    	           if (rect.contains(e.getX(), e.getY())) {
    	             // increment the entered counter only if the mouse
    	             // was last seen outside the rectangle
    	             if (state == OUTSIDE) {
    	               state = INSIDE;
    	               entered++;
                 	     }
    	           }
    	           else {
    	             // else the mouse is outside the rectangle. If the mouse
    	             // was last seen inside the rectangle, change the
    	             // state to OUTSIDE
    	             if (state == INSIDE)
    	               state = OUTSIDE;
    	           }
    	         }
                   });
    	       
                 }