Adult web hosting - Chapter 6 Methods 297 An important feature
Chapter 6 Methods 297 An important feature of method definitions is the coercion of arguments. In many cases, argument values that do not correspond precisely to the parameter types in the method definition are converted to the proper type before the method is called. In some cases, these conversions can lead to compiler errors if Java s promotion rules are not followed. The promotion rules specify how types can be converted to other types without losing data. The promotion rules apply to mixed-type expressions. The type of each value in a mixed-type expression is promoted to the highest type in the expression. Method Math.random generates a double value from 0.0 up to, but not including, 1.0. Values produced by Math.random can be scaled and shifted to produce values in a range. The general equation for scaling and shifting a random number is n = a + (int) ( Math.random() * b ); where a is the shifting value (the first number in the desired range of consecutive integers) and b is the scaling factor (the width of the desired range of consecutive integers). A class can inherit existing attributes and behaviors (data and methods) from another class specified to the right of keyword extends in the class definition. In addition, a class can implement one or more interfaces. An interface specifies one or more behaviors (i.e., methods) that you must define in your class definition. The interface ActionListener specifies that a class must define a method with the first line public void actionPerformed( ActionEvent actionEvent ) The task of method actionPerformed is to process a user s interaction with a GUI component that generates an action event. This method is called in response to the user interaction (the event). This process is called event handling. The event handler is the actionPerformed method, which is called in response to the event. This style of programming is known as event-driven programming. Keyword final declares constant variables. Constant variables must be initialized before they are used in a program. Constant variables are often called named constants or read-only variables. A JLabelcontains a string of characters to be displayed on the screen. Normally, a JLabelindicates the purpose of another GUI element on the screen. JTextFields get information from the user or displays information on the screen. When the user presses a JButton, the program normally responds by performing a task. Container method setLayout defines the layout manager for the applet s user interface. Layout managers are provided to arrange GUI components on a Container for presentation purposes. FlowLayout is the simplest layout manager. GUI components are placed on a Container from left to right in the order in which they are attached to the Container with method add. When the edge of the container is reached, components are continued on the next line. Before any event can be processed, each GUI component must know which object in the program defines the event-handling method that will be called when an event occurs. Method addActionListener is used to tell a JButtonor JTextField that another object is listening for action events and defines method actionPerformed. This procedure is called registering the event handler with the GUI component. To respond to an action event, we must define a class that implements ActionListener and defines method actionPerformed. Also, we must register the event handler with the GUI component. Method showStatus displays a String in the applet container s status bar. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01