A.6. Methods and Breaking Out

Table of contents:

Unless a program is very tiny, breaking it down into several methods can make it easier to understand. For example, in the Guess program, we could shove the task of choosing a guess off onto a separate method (Figure A-14). This method is invoked using its name followed by a left parenthesis, any arguments (separated by commas), and a right parenthesis. The value of this expression is whatever is returned by the method.

Figure A-14. The choice of a guess can be moved to a separate method.

 1 /** Game where the computer guesses a number. */
 2 public class Guess {
 4 /** Guess a number between min and max, inclusive. */
 5 public static int makeGuess(int min, int max) {
 6 return min;
 7 }
 9 /** Play the game. */
10 public static void main(String[] args) {
11 System.out.println("Think of an integer between 1 and 100.");
12 int min = 1;
13 int max = 100;
14 char comparison;
15 do {
16 int guess = makeGuess(min, max);
17 // See Figure A-12, lines 314, for the rest of the loop body
18 } while (comparison != '=');
19 }
21 }

Within the method, a return statement causes the method to end immediately and (unless the method has a return type of void) return a value. Enough people miss this point that it is worth repeating: once areturn statement is executed, the method invocation is over, and any subsequent code is skipped.

A return statement is not the same as a break statement. We saw the break statement in the discussion of the switch statement. It can also be used in any kind of loop. It tells Java, "Okay, I'm done with this loop, let's go do whatever comes after the loop." A return statement is stronger, because it exits the entire method, no matter how deep in loops and switch statements it is.

In the event that we want an even stronger statement, use the exit() method from the built-in System class. This takes one int as an argument. By convention, this is 0 if the program is exiting normally, some other number if it is exiting due to some kind of error. When a statement such as


is executed, the entire program ends. This drastic measure is rarely used.

Dividing a program into methods has a number of advantages, as will be discussed in Chapter 1. One is that it makes the program easier to modify. For example, if we want to make our program smarter, we can have it guess the number in the middle of the possible range, rather than at the bottom. To make this improvement, we don't even have to look at the main() method. We just have to change makeGuess() (Figure A-15).

Figure A-15. A change to the makeGuess() method makes the program smarter.

1 /** Guess a number between min and max, inclusive. */
2 public static int makeGuess(int min, int max) {
3 return (min + max) / 2;
4 }



Write a method which behaves exactly like the one in Figure A-5, but in which line 4 is:

while (true) {

Write a method which accepts an int n as an argument and returns the square of n.


Write a method which accepts an int n as an argument, prints out the numbers 1 through n, and does not return a value.

A 7 Constants

Part I: Object-Oriented Programming




Part II: Linear Structures

Stacks and Queues

Array-Based Structures

Linked Structures

Part III: Algorithms

Analysis of Algorithms

Searching and Sorting


Part IV: Trees and Sets



Part V: Advanced Topics

Advanced Linear Structures


Advanced Trees


Memory Management

Out to the Disk

Part VI: Appendices

A. Review of Java

B. Unified Modeling Language

C. Summation Formulae

D. Further Reading


Data Structures and Algorithms in Java
Data Structures and Algorithms in Java
ISBN: 0131469142
EAN: 2147483647
Year: 2004
Pages: 216
Authors: Peter Drake

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net