DEMYSTIFYING PROGRAMMING: CHAPTER SIX METHODS (TOC DETAILED) CHAPTER SIX: METHODS 1

Similar documents
DEMYSTIFYING PROGRAMMING: CHAPTER FOUR

Introduction. Introduction

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

DEMYSTIFYING PROGRAMMING: CHAPTER TEN REPETITION WITH WHILE-LOOP (TOC DETAILED)

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

EVENTS, EVENT SOURCES AND LISTENERS

SINGLE EVENT HANDLING

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

GUI Forms and Events, Part II

We are on the GUI fast track path

Critical Thinking Assignment #6: Java Program #6 of 6 (70 Points)

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CS180 Recitation. More about Objects and Methods

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

More About Objects and Methods

Class 16: The Swing Event Model

RAIK 183H Examination 2 Solution. November 10, 2014

Page 1 of 7. public class EmployeeAryAppletEx extends JApplet

Chapter 5: Enhancing Classes

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

Swing from A to Z Some Simple Components. Preface

CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

Agenda. Container and Component

RAIK 183H Examination 2 Solution. November 11, 2013

Lab 3. A Multi-Message Reader

Window Interfaces Using Swing Objects

Window Interfaces Using Swing Objects

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

8. Polymorphism and Inheritance

MIT AITI Swing Event Model Lecture 17

CS 251 Intermediate Programming GUIs: Event Listeners

More About Objects and Methods

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

DEMYSTIFYING PROGRAMMING: CHAPTER SIXTEEN ADD AND SEARCH AN ARRAY (TOC DETAILED)

Inheritance. Chapter 7. Chapter 7 1

PART1: Choose the correct answer and write it on the answer sheet:

Programming graphics

To gain experience using GUI components and listeners.

MODULE 8p - Applets - Trials B

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

DUKE UNIVERSITY Department of Computer Science. Midterm Solutions

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

Chapter 1 GUI Applications

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

Lecture 18 Tao Wang 1

Swing - JButton. Adding buttons to the main window

Lecture (06) Java Forms

CSC207 Week 4. Larry Zhang

Systems Programming. Bachelor in Telecommunication Technology Engineering Bachelor in Communication System Engineering Carlos III University of Madrid

Chapter 10 Introduction to Classes

AP CS Unit 11: Graphics and Events

Graphical User Interface (GUI)

CS 170 Java Programming 1. Week 9: Learning about Loops

Contents Introduction 1

CS415 Human Computer Interaction

Java Programming Lecture 6

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Systems Programming Graphical User Interfaces

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

CS 106A, Lecture 23 Interactors and GCanvas

Packages: Putting Classes Together

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

ASSIGNMENT NO 14. Objectives: To learn and demonstrated use of applet and swing components

Java IDE Programming-I

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

Part 3: Graphical User Interface (GUI) & Java Applets

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

11. Abstract Classes and Interfaces

Window Interfaces Using Swing. Chapter 12

Java Programming Lecture 7

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Graphical User Interfaces. Comp 152

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Introduction to the JAVA UI classes Advanced HCI IAT351

Programming Language Concepts: Lecture 8

Chapter 17 Creating User Interfaces

Example: Building a Java GUI

COMP 401 Recitation 8. Observer Pattern

CS 134 Programming Exercise 7:

Eclipsing Your IDE. Figure 1 The first Eclipse screen.

MVC: Model View Controller

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

Example: Building a Java GUI

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Event Driven Programming

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

Lecture 19 GUI Events

CoSc Lab # 5 (The Controller)

CSC 1214: Object-Oriented Programming

Transcription:

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