Section 6.1 - User-defined method basics

Lehman College City University of New York CMP 167 Spring 2016: Programming in Java

1/30/16, 11:00 AM

Chapter 6 - User-Defined Methods

Section 6.1 - User-defined method basics

A method is a named list of statements. Invoking a method's name, known as a method call, causes the method's statements to execute. The following illustrates.

P Participation Activity

6.1.1: Method example: Printing a face.

Start public class SimpleFace {

o o o

ooo

public static void printFace() { char faceChar = 'o';

System.out.println(" " + faceChar + " " + faceChar);

// Eyes

System.out.println(" " + faceChar);

// Nose

System.out.println(" " + faceChar + faceChar + faceChar); // Mouth

return; }

public static void main (String [] args) { printFace(); return;

} }

A method definition consists of the new method's name and a block of statements, as appeared above: public static void printFace() { ... }. The name can be any valid identifier. A block is a list of statements surrounded by braces.

Methods must be defined within a class. The line: public class SimpleFace { begins a new class. The class has two methods, printFace() and main(). Both use the access modifiers



Page 1 of 75

Lehman College City University of New York CMP 167 Spring 2016: Programming in Java

1/30/16, 11:00 AM

public static. public tells the compiler that the method may be called from any class in the program, and static that the method only uses values that are passed to the method; details of such items are discussed elsewhere. For now, just know that a method defined using public static can be called from the program's main() method.

The method call printFace() causes execution to jump to the method's statements. The method's return causes execution to jump back to the original calling location.

Other aspects of the method definition, like the () and the word void, are discussed later.

P Participation Activity

6.1.2: Method basics.

Given the printFace() method defined above, and the following main() method:

public static void main (String [] args) { printFace(); printFace(); return;

}

# Question

How many method calls to printFace() exist in 1 main()?

Your answer

How many method definitions of printFace() exist 2 within main()?

How many output statements would execute in 3 total?

How many output statements exist in printFace()? 4

Is main() itself a method? Answer yes or no. 5



Page 2 of 75

Lehman College City University of New York CMP 167 Spring 2016: Programming in Java

P Participation Activity

6.1.3: Adding to the face printing program.

1/30/16, 11:00 AM

1. Run the following program, observe the face output.

2. Modify main() to print that same face twice.

3. Complete the method definition of printFaceB() to print a different face of your choice, and then call that method from main() also.

1 2 public class FacePrinterSimple {

Run

3

public static void printFaceB() {

4

// FIXME: FINISH

5

return;

6

}

7

8

public static void printFaceA() {

9

char faceChar = 'o';

10

System.out.println(" " + faceChar + " " + faceChar

11

System.out.println(" " + faceChar);

12

System.out.println(" " + faceChar + faceChar + faceChar

13

return;

14

}

15

16

public static void main (String [] args) {

17

printFaceA();

18

return;

19

}

Exploring further: Methods tutorial from Oracle's Java tutorials.



Page 3 of 75

Lehman College City University of New York CMP 167 Spring 2016: Programming in Java

C Challenge Activity

6.1.1: Basic method call.

1/30/16, 11:00 AM

Complete the method definition to print five asterisks ***** when called once (do NOT print a newline). Output **********

1 public class CharacterPrinter {

2

3

public static void printPattern() {

4

5

/* Your solution goes here */

6

7

}

8

9

public static void main (String [] args) {

10

printPattern();

11

printPattern();

12

System.out.println("");

13

return;

14

}

15 }

Run



Page 4 of 75

Lehman College City University of New York CMP 167 Spring 2016: Programming in Java

C Challenge Activity

6.1.2: Basic method call.

Complete the printShape() method to print the following shape. End with newline. Example output:

*** *** ***

1 public class MethodShapePrinter {

2

public static void printShape() {

3

4

/* Your solution goes here */

5

6

return;

7

}

8

9

public static void main (String [] args) {

10

printShape();

11

return;

12

}

13 }

1/30/16, 11:00 AM

Run

Section 6.2 - Parameters

Programmers can influence a method's behavior via an input to the method known as a parameter. For example, a face-printing method might have an input that indicates the character to print when printing the face.



Page 5 of 75

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download