Chapter 15


Exercise 1 Suppose you use the following code to create a checkbox:

Checkbox cbox = new Checkbox("Ok", true);

What is the checkbox's state after you click on it 20,000 times?

Solution 1 The checkbox's initial state is true. After you click on it an even number of times, it is true again. There are two ways to get the answer: thinking about it, or doing it. If you chose the second way, you might not be cut out to be a computer programmer.

Exercise 2 In the "Checkboxes" section of Chapter 15, the Boats application is 30 lines long. The code isolates literal strings in an array near the top of the listing. You saw how this approach, along with the use of a loop to create the checkboxes, results in more maintainable code. Rewrite the code to eliminate the loop and the string array. In place of the loop in the constructor, just create three checkboxes one by one. How many lines of code does your new application have?

Solution 2 Here is the rewritten code:

 1. import java.awt.*;  2.  3. class BoatsNoLoop extends Frame  4. {  5.   Checkbox[]    cboxes;  6.   Button        btn;  7.  8.   BoatsNoLoop()  9.   { 10.     setLayout(new FlowLayout()); 11. 12.     cboxes = new Checkbox[3]; 13.     cboxes[0] = new Checkbox("a small boat"); 14.     add(cboxes[0]); 15.     cboxes[1] = new Checkbox("a medium boat"); 16.     add(cboxes[1]); 17.     cboxes[2] = new Checkbox("a large boat"); 18.     add(cboxes[2]); 19.     btn = new Button("Add to shopping cart"); 20.     add(btn); 21. 22.     setSize(600, 200); 23.   } 24. 25.   public static void main(String[] args) 26.   { 27.     new BoatsNoLoop().setVisible(true); 28.   } 29. }

This version is only 29 lines, one line shorter than the original. The point is that a shorter program is not necessarily easier to read or maintain than a longer version. If you still aren't convinced that the original version is better, try Exercise 3.

Exercise 3 This is an extension of Exercise 2. Suppose you need to change the Boats application so that instead of offering three sizes (small, medium, and large), it offers ten (rubber duck, sponge, tiny, small, kinda small, medium, kinda large, large, huge, titanic). How does this affect the size of the code as it appears in the "Checkboxes" section of Chapter 15? How does it affect the size of the code that you wrote for Exercise 2?

Solution 3 The loop-based code will probably become longer by two lines, because the array of literal strings now has 10 members:

String[]  sizes = {"rubber duck", "sponge", "tiny", "small", "kinda small",  "medium", "kinda large", "large", "huge", "titanic"};

The no-loop version grows by two lines for every additional size option (one line to construct a checkbox, another to call add()). Seven new options were added, so the code grows by 14 lines, or 50%.

Note

Programs usually start out small, and gradually grow as they are required to support more and more functionality. Giving structure to a small program is always worth the effort. The payoff might not be immediately obvious, but it will become more and more evident as time goes by. In Exercise 2, the well-structured program was actually longer than the unstructured version. But when the code needed to support more functionality, the unstructured version grew by 50% while the structured version grew by about 7%.

Exercise 4 Write an application that displays a frame with a menu bar. The bar should have the following menus:

An Edit menu with items Copy and Cut.

A File menu with items Close, Exit, and Open.

A Help menu with item Help. Assume that clicking on this item will display a helpful dialog.

A Whatever menu with items Stuff and Nonsense. The Nonsense item should be a submenu with items Ordinary Nonsense and Extreme Nonsense.

Make sure that your GUI follows the guidelines listed at the end of the "Menus" section.

Solution 4 Here is one solution:

import java.awt.*; class Q4 extends Frame {   public Q4()   {     MenuBar mbar = new MenuBar();     Menu fileMenu = new Menu("File");     fileMenu.add("Open...");     fileMenu.add("Close");     fileMenu.add("Exit");     mbar.add(fileMenu);     Menu editMenu = new Menu("Edit");     editMenu.add("Cut");     editMenu.add("Copy");     mbar.add(editMenu);     Menu whateverMenu = new Menu("Whatever");     whateverMenu.add("Stuff");     Menu nonsenseMenu = new Menu("Nonsense");     nonsenseMenu.add("Ordinary Nonsense");     nonsenseMenu.add("Extreme Nonsense");     whateverMenu.add(nonsenseMenu);     mbar.add(whateverMenu);     Menu helpMenu = new Menu("Help");     helpMenu.add("Help...");     mbar.add(helpMenu);     setMenuBar(mbar);     setSize(300, 200);   }   public static void main(String[] args)   {       (new Q4()).setVisible(true);   } }

The following illustrations show the four menus, File, Edit, Whatever, and Help:

Exercise 5 Write a program that creates a GUI that looks like the following illustration. The text in the text area should be set programmatically by a single call to the text area's append() method. The call should come directly after the text area is constructed.

click to expand

Solution 5 Here is one solution:

 1. import java.awt.*;  2.  3. class Q5 extends Frame  4. {  5.   public Q5()  6.   {  7.     setLayout(new FlowLayout());  8.     TextArea ta = new TextArea(10, 30);  9.     ta.append("Hello\nWorld"); 10.     add(ta); 11. 12.     setSize(550, 220); 13.   } 14. 15.   public static void main(String[] args) 16.   { 17.     (new Q5()).setVisible(true); 18.   } 19. }

The text is set in line 9. The thing to notice is the newline character, which puts in a line break. It would be wrong to replace line 9 with this:

ta.append("Hello"); ta.append("World");

Line breaks are inserted only when you explicitly append a newline character. So with the substitution, you would see a single line of text that read "HelloWorld". There wouldn't even be a space between the words.

Exercise 6 Using the API page for java.awt.FlowLayout, determine how to create a flow layout manager that right-justifies its cluster of components rather than centering it.

Solution 6 Use the following constructor:

new FlowLayout(FlowLayout.RIGHT)

Exercise 7 The java.awt.Component class, which is a superclass of java.awt.Button, has a method called setSize(int width, int height). The method's documentation says that it resizes the component so that its size is width times height.

What do you expect the following code to do? First, read the listing and decide on your answer. Then, type in the code and run it. Did you see what you expected to see?

import java.awt.*; class Q7 extends Frame {   public Q7()   {     setLayout(new FlowLayout());     Button btn = new Button("Abcde");     btn.setSize(500, 500);     add(btn);     setSize(700, 700);   }   public static void main(String[] args)   {     (new Q7()).setVisible(true);   } }

Solution 7 The code seems to create a large (500 x 500) button in a 700 x 700 frame. But actually, the button's size is perfectly ordinary. The Flow layout manager sets the size of the button to its preferred size.

Exercise 8 This entire chapter has been about components that are installed inside containers. The previous chapter was about painting. What happens if a frame that contains components also has a paint() method that paints a part of the screen that is occupied by a component? Write a program that will reveal the answer.

Solution 8 The frame in the following application has a button, as well as a paint() method. The paint() method draws diagonal blue lines.

import java.awt.*; class PaintPlusComponent extends Frame {     public PaintPlusComponent()     {         setLayout(new FlowLayout());         add(new Button("Apply"));         setSize(300, 200);     }     public void paint(Graphics g)     {         g.setColor(Color.blue);         for (int i=0; i<500; i+=10)             g.drawLine(i, 0, 0, i);     }     public static void main(String[] args)     {         (new PaintPlusComponent()).setVisible(true);     } }

The following illustration shows the GUI. As you can see, the button is superimposed over the painted lines. Whenever a component and a paint() method are both responsible for the same part of the screen, the component wins.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

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