Lesson Set 8

Lesson Set 8 - CS 1136 Lab 8 Methods Every Java program...

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

View Full Document Right Arrow Icon
CS 1136 Lab 8 Methods Every Java program contains one or more methods; one of those methods must be named main. The main method is sometimes called the driver because it drives the program. It is always executed first and it is executed automatically without a method call. This is the only method you have written thus far but it is actually not the only method you have used. next() is an example of a library method and it is defined in java.util.Scanner. You used this method when you made the method call next() in a previous lab ’s assignments. You can tell the other methods you have used by noting the parenthesis after the identifiers; this notation signifies a method call. In this lab you will learn how to write and call your own methods. Value returning vs. Void methods Value returning methods are methods that return a value to the calling method, allowing the returned value to be used somewhere else, either by storing it in a variable or using it immediately. The value is returned using a return statement. The return statement can occur anywhere in the method but it is usually the last line of the method body. Void methods do not return a value to the calling method. Instead, they perform certain actions, such as asking the user for input or performing an in-depth calculation. System.out.println() is a void method which displays a carriage return on the monitor. User defined methods The methods you have been using so far have been defined in library files that we access with the import statement, such as import java.util.Scanner;. We can also define methods within the file containing our program. Here is the format for the definition of a method that returns an int (a value returning method): int method_name(int x, char c) // the variables inside the parenthesis //are termed formal parameters { // statements making up the method body
Background image of page 1

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

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

Page1 / 3

Lesson Set 8 - CS 1136 Lab 8 Methods Every Java program...

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

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