Exercises


[Page 653 (continued)]

Note

For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.


Exercise 13.1

Explain the difference between the following pairs of terms:

  1. A model and a view.

  2. A view and a controller.

  3. A lightweight component and a heavyweight component.

  4. A JButton and a Button.

  5. A layout manager and a container.

  6. A containment hierarchy and an inheritance hierarchy.

  7. A content pane and a JFrame.


[Page 654]
Exercise 13.2

Fill in the blanks.

  1. A GUI component that is written entirely in Java is known as a ________component.

  2. The AWT is not platform independent because it uses the ________ model to implement its GUI components.

  3. The visual elements of a GUI are arranged in a ________.

  4. A ________ is an object that takes responsibility for arranging the components in a container.

  5. The default layout manager for a JPanel is ________.

  6. The default layout manager for a JApplet is ________.

Exercise 13.3

Describe in general terms what you would have to do to change the standard look and feel of a Swing JButton.

Exercise 13.4

Explain the differences between the model-view-controller design of a JButton and the design of an AWT Button. Why is MVC superior?

Exercise 13.5

Suppose you have an applet that contains a JButton and a JLabel. Each time the button is clicked, the applet rearranges the letters in the label. Using Java's event model as a basis, explain the sequence of events that happens in order for this action to take place.

Exercise 13.6

Draw a containment hierarchy for the most recent GUI version of the OneRowNimprogram.

Exercise 13.7

Create a GUI design, similar to the one shown in Figure 13.25, for a program that would be used to buy tickets online for a rock concert.

Exercise 13.8

Create a GUI design, similar to the one shown in Figure 13.25, for an online program that would be used to play musical recordings.

Exercise 13.9

Design and implement a GUI for the BankCD program in Self-Study solution 5.20. The program should let the user input the interest rate, principal, and period and should accumulate the value of the investment.

Exercise 13.10

Design and implement a GUI for the Temperature class (Fig. 5.5). One challenge of this design is to find a good way for the user to indicate whether a Fahrenheit or Celsius value is being input. This should also determine the order of the conversion: F to C or C to F.

Exercise 13.11

Design an interface for a 16-button integer calculator that supports addition, subtraction, multiplication, and division. Implement the interface so that the label of the button is displayed in the calculator's displaythat is, it doesn't actually do the math.

Exercise 13.12

Challenge: Design and implement a Calculator class to go along with the interface you developed in the preceding exercise. It should function the same way as a hand calculator except that it only handles integers.

Exercise 13.13

Modify the Converter application so that it can convert in either direction: from miles to kilometers or from kilometers to miles. Use radio buttons in your design to let the user select one or the other alternative.


[Page 655]
Exercise 13.14

Here's a design problem for you. A biologist needs an interactive program that calculates the average of some field data represented as real numbers. Any real number could be a data value, so you can't use a sentinel value, such as 9999, to indicate the end of the input. Design and implement a suitable interface for this problem.

Exercise 13.15

Challenge: A dialog box is a window associated with an application that appears only when needed. Dialog boxes have many uses. An error dialog is used to report an error message. A file dialog is used to help the user search for and open a file. Creating a basic error dialog is very simple in Swing. The JOptionPane class has class methods that can be used to create the kind of dialog shown in Figure 13.36. Such a dialog box can be created with a single statement:

JOptionPane.showMessageDialog(this, "Sorry, your number is out of range."); 


Figure 13.36. A basic JOptionPane error dialog.


Convert the Validate program (Fig. 6.12) to a GUI interface, and use the JOptionPane dialog to report errors.

Exercise 13.16

Challenge: Design and implement a version of the game Memory. In this game you are given a two-dimensional grid of boxes that contain pairs of matching images or strings. The object is to find the matching pairs. When you click a box, its contents are revealed. You then click another box. If its contents match the first one, their contents are left visible. If not, the boxes are closed up again. The user should be able to play multiple games without getting the same arrangement every time.

Exercise 13.17

Challenge: Extend the SimpleTextEditor program by adding methods to handle the opening, closing, and saving of text files.




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