Note: For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.
1.Java Concept Matching.
Match each of the following GUI concepts.
model
a component’s internal state and consists of such properties as whether it is enabled and visiblen
view
a component’s appearance.
controller
monitors the component’s state and the actions that involve the component
inheritance hierarchy
the superclass/subclass relationship among classes
lightweight component
programmed entirely within Java code, such as most Swing components
heavyweight component
depend on peer classes that are written in the native system rather than in Java itself, such as all AWT components
JButton
Swing version of a button.
Button
AWT version of a button
layout manager
an object that manages the relative size and positioning of components in a container
container
holds GUI components.
containment hierarchy
describes which components are contained in which containers
content pane
a built-in JPanel that serves as the working area within a JFrame.
JFrame
a window that holds a content pane.
Fill in the Blanks.
Fill in the blanks.
2..
A GUI component that is written entirely in Java is known as a component.
3..
The AWT is not platform independent because it uses the model to implement its GUI components.
4..
The visual elements of a GUI are arranged in a .
5..
A is an object that takes responsibility for arranging the components in a container.
6..
The default layout manager for a JPanel is .
7.Change JButton.
Describe in general terms what you would have to do to change the standard look and feel of a Swing JButton.
8.MVC vs AWT.
Explain the differences between the model-view-controller design of a JButton and the design of an AWT Button. Why is MVC superior?
9.Sequence Label Update.
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.
10.OneRowNim Containment Hierarchy.
Draw a containment hierarchy for the most recent GUI version of the OneRowNim program.
11.Rock Tickets GUI.
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.
12.Music Player GUI.
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.
13.Certificate of Deposit Interest GUI.
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.
14.Temperature GUI.
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.
15.Integer Calculator Label GUI.
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.
16.Calculator Class.
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.
17.Refactor Converter.
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.
18.Field Data Program.
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.
19.Refactor Validate.
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. 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.
20.Code Memory Game.
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.
21.Refactor Text Editor.
Challenge: Extend the SimpleTextEditor program by adding methods to handle the opening, closing and saving of text files.