Chapter5PPT - 1 Chapter 5 METHODS 2 INTRODUCTION TO METHODS...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 5 METHODS 2 INTRODUCTION TO METHODS It is common to break programs into manageable methods. A method is a named block of statements that performs a specific task. 3 void METHODS AND VALUE-RETURNING METHODS There are two general categories of methods: void methods and value-returning methods. • Methods that do not return a value are called void methods . • A value-returning method not only performs a task, but also sends a value back to the code that called it. 4 void METHODS AND VALUE-RETURNING METHODS • The println method of the System.out object is an example of a void method. It performs its job, displaying a string of characters in a console window, and terminates. The segment below calls the void method println to display a string of characters in a console window. int number = 10; System.out.println("The number is: " + number); 5 void METHODS AND VALUE-RETURNING METHODS • The parseInt method of the Integer class is an example of a value-returning method. It does its job of converting the string it receives as its argument to an int and then returns this value back to the code that called it. For example, the segment below calls the parseInt method to convert a string to an integer, assigns the integer returned by the method to the variable number, and then increases the contents of number by 5. String numberString = "15"; int number; number = Integer.parseInt(numberString); number += 5; 6 DEFINING A METHOD • Every Java application program must include the definition of a method named main . • Java programs can contain multiple methods. 7 DEFINING A METHOD • To create a method you write a method definition. A method definition includes the method header and the method body. – The method header , which is the beginning of the method definition, gives important information about the method, including its name. – The body is the group of statements, enclosed in braces, which perform the methods operation. 8 DEFINING A METHOD Here is an example of the definition of a simple method called displayMenu . /** A method that displays the Health Club Membership Menu in a console window. */ public static void displayMenu( ) { System.out.println( ); System.out.println("\t\tHealth Club Membership Menu\n"); System.out.println("1. Standard Adult Membership"); System.out.println("2. Child Membership"); System.out.println("3. Senior Citizen Membership"); System.out.println("4. Quit the Program\n"); System.out.print("Enter your choice: "); return; } This is the header of the method named displayMenu . 9 DEFINING A METHOD • The key words public and static are method modifiers. Every method we write in this class will begin with these modifiers....
View Full Document

This note was uploaded on 04/14/2008 for the course CS 1336 taught by Professor Thompson during the Spring '08 term at University of Texas at Dallas, Richardson.

Page1 / 26

Chapter5PPT - 1 Chapter 5 METHODS 2 INTRODUCTION TO METHODS...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online