Exercises


Note

The solutions to these exercises are in Appendix B.

  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?

  2. In the "Checkboxes" section of this chapter, 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?

  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 this chapter? How does it affect the size of the code that you wrote for Exercise 2?

  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.

  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

  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.

  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);   } }
  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.




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