Solutions to Self-Study Exercises


[Page 651 (continued)]


[Page 652]
Solution 13.1

How can a button still be considered a component under the MVC model? This is a good question. The JButton class acts as a wrapper class and hides the model-view-controller details (Fig. 13.35). When you instantiate a JButton, you still get a single instance. Think of it this way: Your body consists of several systems that interact (internally) with each other, but it's still one body that other bodies interact with as a single object.

Figure 13.35. A JButton has internal model-view-controller components that interact with each other to produce the button's overall behavior.


Solution 13.2

A component can indeed be registered with more than one listener. For example, the ToggleButton defined in Chapter 4 has two listeners. The first is the button itself, which takes care of toggling the button's label. The second is the applet in which the button is used, which takes care of handling whatever action the button is associated with.

Solution 13.3

Some components can have two different kinds of listeners. For example, imagine a "sticky button" that works like this. When you click and release the button, it causes some action to take place, just like a normal button. When you click and hold the mouse button down, the button "sticks" to the cursor and you can then move it to a new location. This button would need listeners for ActionEvents, MouseEvents, and MouseMotionEvents.

Solution 13.4

To round a double you could use the Math.round() method. For example, suppose the number you want to round is d. Then the expression Math.round(100 * d) /100.0 will round to two decimal places. Alternatively, you could use the java.text.NumberFormat class. Both of these approaches were covered in Chapter 5.

Solution 13.5

Many cars today have cruise control as an alternative way to control the accelerator. Push buttons, usually located on the steering wheel, are used to speed up and slow down, so you can drive with your foot or your hand.

Solution 13.6

As an alternative, a north-west-center border layout for the top-level window in the Converter might work. So might center-south-east and center-south-west. What makes these possible is the fact that the layout manager will use up space in any edge area that is not assigned a component.

Solution 13.7

A flow layout would not be appropriate for the control panel because you would have little control of where the convert button would be placed relative to the keypad.

Solution 13.8

Interface design disaster: My car uses the same kind of on/off switch for the headlights and the windshield wipers. One is on a stem on the left side of the steering wheel, and the other is on a stem on the right side of the steering wheel. On more than one occasion, I've managed to turn off the headlights when I intended to turn on the wipers.


[Page 653]
Solution 13.9

Modify the addRecentCut() method so it limits the cuts stored in the vector to the last 10 cuts. Solution: Check the size of the vector after inserting the cut. If it exceeds 10, remove the last element in the vector.

private void addRecentCut(String cut) {   recentCuts.insertElementAt(cut, 0);    if (recentCuts.size() > 10) {                       // If more than 10 cuts     recentCuts.removeElementAt(10);                    // remove oldest cut   }   cutsMenu.removeAll();   for (int k = 0; k < recentCuts.size(); k++) {     JMenuItem item =       new JMenuItem((String) recentCuts.elementAt(k));     cutsMenu.add(item);     item.addActionListener(this);   } } // addRecentCut() 


Solution 13.10

Modify the addRecentCut() method so that it does not duplicate cuts stored in the vector. Solution: Use the indexOf() method to search for the cut in the vector. If it's already there, don't insert the cut.

private void addRecentCut(String cut) {   if (recentCuts.indexOf(cut) == -1) {                 // If not already cut     recentCuts.insertElementAt(cut,0);     if (recentCuts.size() > 10) {                      // If more than 10 cuts         recentCuts.removeElementAt(10);                // remove oldest     }     cutsMenu.removeAll();     for (int k = 0; k < recentCuts.size(); k++) {         JMenuItem item =           new JMenuItem((String) recentCuts.elementAt(k));         cutsMenu.add(item);         item.addActionListener(this);     }   }                                                    // if not already cut } // addRecentCut() 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net