DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1 Objectives 1 6.1 Methods 1 void or return 1 Parameters 1 Invocation 1 Pass by value 1 6.2 GUI 2 JButton 2 6.3 Patterns 2 6.4 Review 2 6.5 Challenge 3 6.6 Java Key Words 3 6.7 Exercises 3 Code Analysis Exercises 3 Complete the Code Exercises 4 Code Exercises 4 Index 5
Chapter Six: Methods Objectives How do Methods differ (return and void)? How are Methods invoked and consumed? How are parameters passed by value? How can I use a JButton? 6.1 Methods void or return All methods accomplish a specific goal. Methods are declared only once. Declaration requires header and body. If the goal is a product or the sharing of information, the method is a return method; otherwise, it is a void method. A return method must declare a data type in method header. In HelloGreeting (Figure 1), is the showmessage method void or return? What indicates void or return? What data type does this method return? Parameters A method header must also contain parentheses for parameters. Each parameter must declare its data type in the method s header. If no raw materials (parameters) are required, the parentheses are left empty. On what line of HelloGreeting.java is the showmessage method declared? Does Greeting s showmessage require any parameters? If so what type? On what line of HelloGreeting.java is the Greeting constructor declared? Does the constructor Greeting require any parameters? If so what type? Invocation Although a method is declared once (as are variables); it may be invoked (used or called) many times. A method is invoked by calling the method name followed by parentheses. If the method declaration includes parameters, the invocation must exactly match the data type and order of parameters in that declaration. And if the method is a return type (produces a product), the product must be consumed on the same line as the invocation. On what line of HelloGreeting.java is the method showmessage invoked? On what line is its product (return value) consumed? How is the return value from showmessage consumed? Pass by value When a method that requires parameters is invoked, data must be passed to the method. When a programmer places data (usually in the form of a variable) in the parentheses of a method invocation, that data is being passed to the method header. This data is passed by value. When Demystifying Programming Ch6: Methods 1
variables are used to pass information to or from a method, the actual value is passed rather than the variable reference. 6.2 GUI JButton Meet the JButton. We have all seen him before many times. A JButton follows the same pattern as a JTextField. It is declared, instantiated, and added to the GUI in the same way. A message (or Icon) usually appears on the face of the JButton. JButton btngo; //declare button as instance variable //following code belongs in init method btngo = new JButton( Go ); //instantiate button in init method container.add(btngo); //add button to GUI container btngo.addactionlistener(this); //register listener to button Don t forget to add a method to handle the event this button will cause when it is pushed. What is the name of the event handler for an ActionEvent? What does the method header look like for an ActionEvent handler? 6.3 Patterns The action event model reveals the following pattern: import java.awt.event.*; public class Demo extends JApplet implements ActionListener{ } public void init(){ object.addactionlistener(this); } public void actionperformed(actionevent e){ //appropriate code to respond to event goes here } Figure 1: JButton Pattern Action Event Pattern: 1. import the event library 2. implement ActionListener in class header 3. register a listener to event causing objects in init 4. create an actionperformed method to respond to events 6.4 Review The heart of this chapter is an understanding of methods. Every class includes at least one method, its constructor. A method is a collection or block of code to accomplish a single purpose. The method is declared in a header and performed in the body. Most methods are public, available for cooperative use by other classes. Some are void, they do their thing and quit. Others methods Demystifying Programming Ch6: Methods 2
produce a product that is returned and consumed by the line of code that invoked the method. Some methods define required parameters in parentheses that follow the method name in the method s header. Others require no parameters and thus have empty parentheses. Data is passed by value to a method by placing it in the parentheses while invoking the method. The JButton is a common GUI component, with which users commonly create events. The JButton must be listened to or the event does nothing. An event handler receives details of the event and responds according to the programmer s intent. Buttons cause action events, so they are handled by an actionperformed method. 6.5 Challenge Discover and demonstrate how to add an Icon to the face of a JButton. 6.6 Java Key Words return 6.7 Exercises Code Analysis Exercises Use the example code Demo.java to answer the following questions. 1. What does line 19 do? 2. Explain the purpose of each element on lines 25 and 26 of Demo.java. Demystifying Programming Ch6: Methods 3
Complete the Code Exercises Supply the missing code in Demo2.java [code found below] 3. Line 20: 4. Line 23: 5. Line 24: Figure 2: Demo2.java Code Exercises 6. Suppose each acre of farmland produces 18 tons of corn. How many tons of corn can be produced on a 30-acre farm? Produce an IPO, Class Diagram, and Pseudocode for an applet which includes a method for calculating yield based on this formula. The TestYield class will receive the number of acres from the user and pass this information to the method. The method will return the yield and the actionperformed method will display it for the user. Now code, compile, and test your solution. Demystifying Programming Ch6: Methods 4
7. Create a JApplet that includes the method calcnewbalance from Design Exercise 1 in section 5.4 of Chapter 5. The applet will need one JTextField for number of years and one button. 8. Modify the HelloGreeting.java code so that it contains two different Greeting objects. (Hint: instantiate gr1, gr2.) Each greeting will have its own unique message. You invent the message of each greeting object. Ask each Greeting object to express itself. (Hint: what is the difference between settext and append?) Index ActionEvent, 2 ActionListener, 2 actionperformed, 2, 3, 4 append, 5 block of code, 2 Class, 4 Class Diagram, 4 constructor, 1, 2 container, 2 data type, 1 declare, 1, 2 event, 2, 3 event handler, 2, 3 event model, 2 events, 2, 3 GUI, 2, 3 import, 2 instantiate, 2, 5 invocation, 1 JButton, 1, 2, 3 JTextArea, 5 JTextField, 2 Method, 1 methods, 1, 2 new, 2 parameters, 1, 3 parentheses, 1, 3 Pass by value, 1 public, 2 register, 2 return, 1, 3, 4 variable, 2 void, 1, 2 while, 3 Demystifying Programming Ch6: Methods 5