Exercises


Note

The solutions to these exercises are in Appendix B.

  1. Write a program that creates a frame with a File menu. The menu should have two items, Save… and Exit. When Save… is selected, the code should display a file dialog box, configured for saving a file. When the user has specified a file via the dialog box, your code should output the name of the file. All the information you need is on the API page for java.awt.FileDialog.

  2. The FileDialog class has a setDirectory() method that controls which directory the dialog box will display. Look up the method description in the API to become familiar with how it works. Modify the final project code so that when the file dialog box appears, it displays one of the directories on your computer where you have stored some of your own Java source code. This will make it easier to display your own work.

  3. Write an application that displays a canvas subclass in a frame, at Center. The frame does not contain any other components.

    Use the following code as the paint() method for the canvas subclass:

    1. public void paint(Graphics G) 2. { 3.   g.setFont(new Font("Serif", Font.PLAIN, 24)); 4.   g.setColor(Color.blue); 5.   g.drawString("Look at this!", 0, 0); 6. }

    Run the program. Do you see what you expected to see? How do you explain the results?

    Now change line 5 to this:

    g.drawString("A bluejay in a quagmire", 0, 0);

    Now do you see what you expected to see? Again, how do you explain the results?

  4. The FancySrcCanvas class has an array of Java keywords. In that array, throws comes before throw. Otherwise, the list is alphabetical. Why does throws comes before throw?

  5. There are several situations in which the project code would improperly draw text in the keyword color. How many of these situations can you name?

  6. How would you modify the project code so that null, true, and false are not rendered in the keyword color?




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