Skip to main content

Section 13.11 Exercises

Subsection 13.11.1

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

  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 and 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.

  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 __________ .

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

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

  5. Suppose you have a GUI that contains a JButton and a JLabel. Each time the button is clicked, the GUI 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.

  6. Draw a containment hierarchy for the most recent GUI version of the OneRowNim program.

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

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

  9. Design and implement a GUI for the CDInterest program (Fig.Ā 5.18). This program should let the user input the interest rate, principal, and period and should accumulate the value of the investment.

  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.

  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 displayā€”that is, it doesn't actually do the math.

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

  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.

  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.

  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.11.1.

    Figure 13.11.1. A basic JOptionPane error dialog.
    Such a dialog box can be created with a single statement:
    JOptionPane.showMessageDialog(this,
             "Sorry, your number is out of range.");
    
    Convert the Validate program (Fig.Ā 6.12 from ChapterĀ 6) to a GUI interface and use the JOptionPane dialog to report errors.

  16. Challenge: Design and implement a version of the game Memory. In this game you are given a two-dimensional grid of boxes that contains 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.

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

You have attempted of activities on this page.