Exercises


[Page 190]

Note

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


Exercise 4.1

Fill in the blanks in each of the following sentences:

  1. An________is a Java program that can be embedded in a Web page.

  2. A method that lacks a body is an________method.

  3. An________is like a class except that it contains only instance methods, no instance variables.

  4. In a Java class definition a class can_________a class and_________an interface.

  5. Classes and methods not defined in a program must be_________from the Java class library.

  6. A subclass of a class inherits the class's_________instance variables and instance methods.

  7. An object can refer to itself by using the_________keyword.

  8. The JButton, JTextField, and JComponent classes are defined in the_________package.

  9. Java applets utilize a form of control known as_________programming.

  10. When the user clicks on an applet's JButton, an_________will automatically be generated.

  11. Two kinds of objects that generate ActionEvents are_________and_________.

  12. JButtons, JTextFields, and JLabels are all subclasses of_________.

  13. The JApplet class is a subclass of_________.

  14. If an applet intends to handle ActionEvents, it must implement the_________interface.

  15. When an applet is started, its_________method is called automatically.

Exercise 4.2

Explain the difference between the following pairs of concepts:

  1. Class and interface.

  2. Extending a class and instantiating an object.

  3. Defining a method and implementing a method.

  4. A protected method and a public method.

  5. A protected method and a private method.

  6. An ActionEvent and an ActionListener() method.

Exercise 4.3

Draw a hierarchy chart to represent the following situation. There are lots of languages in the world. English, French, Chinese, and Korean are examples of natural languages. Java, C, and C++ are examples of formal languages. French and Italian are considered romance languages, while Greek and Latin are considered classical languages.

Exercise 4.4

Arrange the Java library classes mentioned in the Chapter Summary into their proper hierarchy, using the Object class as the root of the hierarchy.

Exercise 4.5

Look up the documentation for the JButton class on Sun's Web site:

http://java.sun.com/j2se/1.5.0/docs/api/

List the signatures of all its constructors.


[Page 191]
Exercise 4.6

Suppose we want to set the text in our applet's JTextField. What method should we use, and where is it defined? (Hint: Look up the documentation for JTextField. If no appropriate method is defined there, see if it is inherited from a superclass.)

Exercise 4.7

Does a JApplet have an init() method? Explain.

Exercise 4.8

Does a JApplet have an add() method? Explain.

Exercise 4.9

Does a JButton have an init() method? Explain.

Exercise 4.10

Does a JButton have an add() method? Explain.

Exercise 4.11

Suppose you type the URL for a "Hello World" applet into your browser. Describe what happensthat is, describe the processing that takes place in order for the applet to display "Hello World" in your browser.

Exercise 4.12

Suppose you have an applet containing a JButton named button. Describe what happens, in terms of Java's event-handling model, when the user clicks the button.

Exercise 4.13

Java's Object class contains a public method, toString(), which returns a string that represents this object. Because every class is a subclass of Object, the toString() method can be used by any object. Show how you would invoke this method for a JButton object named button.

Exercise 4.14

The applet that follows contains a semantic error in its init() method. The error will cause the actionPerformed() method never to display "Clicked" even though the user clicks the button in the applet. Why? (Hint: Think scope!)

public class SomeApplet extends JApplet                         implements ActionListener {                                           // Declare instance variables     private JButton button;         public void init()     {                                           // Instantiate the instance variable         JButton button = new JButton("Click me");         add(button);         button.addActionListener(this);     } // init()     public void actionPerformed(ActionEvent e)     {         if (e.getSource() == button)            System.out.println("Clicked");     } // actionPerformed() } // SomeApplet class 



[Page 192]
Exercise 4.15

What would be output by the following applet?

public class SomeApplet extends JApplet {                                           // Declare instance variables   private JButton button;   private JTextField field;   public void init()   {                                           // Instantiate instance variables     button = new JButton("Click me");     add(button);     field = new JTextField("Field me");     add(field);     System.out.println(field.getText() + button.getText());   }   // init() } // SomeApplet class 


Exercise 4.16

Design and implement a GUI that has a JButton, a JTextField, and a JLabel and then uses the toString() method to display each object's string representation.

Exercise 4.17

The JButton class inherits a setText(String s) from its AbstractButton() superclass. Using that method, design and implement a GUI that has a single button labeled initially "The Doctor is out." Each time the button is clicked, it should toggle its label to "The Doctor is in" and vice versa.

Exercise 4.18

Design and implement a GUI that contains two JButtons, initially labeled "Me first!" and "Me next!" Each time the user clicks either button, the labels on both buttons should be exchanged. (Hint: You don't need an if-else statement for this problem.)

Exercise 4.19

Modify the GUI in the preceding exercise so that it contains three JButtons, initially labeled "First," "Second," and "Third." Each time the user clicks one of the buttons, the labels on the buttons should be rotated. Second should get First's label, Third should get Second's, and First should get Third's.

Exercise 4.20

Design and implement a GUI that contains a JTextField and two JButtons, initially labeled "Left" and "Right." Each time the user clicks a button, display its label in the JTextField. A JButton()'s label can be gotten with the getText() method.

Exercise 4.21

You can change the size of a JFrame by using the setSize(int h, int v) method, where h and v give its horizontal and vertical dimensions in pixels. Write a GUI application that contains two JButtons labeled "Big" and "Small." Whenever the user clicks on Small, set the JFrame's dimensions to 200 x 100, and whenever the user clicks on Big, set the dimensions to 300 x 200.

Exercise 4.22

Rewrite your solution to the preceding exercise so that it uses a single button whose label is toggled appropriately each time it is clicked. Obviously, when the JButton is labeled "Big," clicking it should give the JFrame its big dimensions.


[Page 193]
Exercise 4.23

Challenge: Design and write a Java GUI application that allows the user to change the JFrame's background color to one of three choices, indicated by buttons. Like all other Java Components, JFrames have an associated background color that can be set by the following commands:

setBackground(Color.red); setBackground(Color.yellow); 


The setBackground() method is defined in the Component class, and 13 primary colorsblack, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yelloware defined in the java.awt.Color class.

Additional Exercises

Exercise 4.24

Given the classes with the following headers

public class Animal ... public class DomesticAnimal extends Animal ... public class FarmAnimal extends DomesticAnimal... public class HousePet extends DomesticAnimal... public class Cow extends FarmAnimal ... public class Goat extends FarmAnimal ... public class DairyCow extends Cow ... 


draw a UML class diagram representing the hierarchy created by these declarations.

Exercise 4.25

Given the preceding hierarchy of classes, which of the following are legal assignment statements?

DairyCow dc = new FarmAnimal(); FarmAnimal fa = new Goat(); Cow c1 = new DomesticAnimal(); Cow c2 = new DairyCow(); DomesticAnimal dom = new HousePet(); 





Java, Java, Java(c) Object-Orienting Problem Solving
Java, Java, Java, Object-Oriented Problem Solving (3rd Edition)
ISBN: 0131474340
EAN: 2147483647
Year: 2005
Pages: 275

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