





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Methods Material Type: Notes; Professor: Sleem; Class: Computer Programming in Java; Subject: Computer Science; University: Texas Southern University; Term: Fall 2010;
Typology: Study notes
1 / 37
This page cannot be seen from the preview
Don't miss anything!
Why Do We Need Methods?^ Why Do We Need Methods?^
public static void main(String[] args){
statement;statement;
public static void doTask1( ){ } public static void doTask2( ){ }
statement;statement;statement;statement; statement;statement;statement;
doTask1();doTask2();statement;statement;
} }
The Basics of Making a Method^ The Basics of Making a Method
method declaration.
public class MyClass{
public static void main(String[] args){
statement;statement;
} //----------------------------------------------public static int doSomething( int value){
statement;statement;
} //----------------------------------------------public static double dosomethingElse( double num1){
statement;statement;
} }
Using Your Methods: Calling a MethodUsing Your Methods: Calling a Method^ Using Your Methods: Calling a Method
Example^ Example
public class HelloWorldMethod{
public static void main(String[] args){
sayHello(); } //-------------------------------------public static void sayHello(){
System.out.println("Hello, World!"); } } public class HelloWorldMethod{
public static void sayHello(){
System.out.println("Hello, World!"); } //-------------------------------------public static void main(String[] args){
sayHello(); } }
Methods that Take ParametersMethods that Take Parameters^ Methods that Take Parameters
public
static
void
dispAverage(int
num1,
int
num2,
int
num3)
{
double
average;
average
=
(double)
(num1+num2+num3)/3.0;
System.out.println("The
average
is
"+
average);
}
.^ .^ .^ .^
.
dispAverage(
2,
4,
5);
.^ .^ .^ .^
.
public static void main(String[] args){
dispAverage( 2, 14, 5);
} public static void dispAverage(int num1, int num2, int
num3)
double average;average = (double) (num1+num2+num3)/3.0;System.out.println("The average is "+ average);
} }
Returning Values from a Method.^ Returning Values from a Method.
For example, a method that take thee numbers then returntheir average to the calling method.
simply indicate the type of that return value on the methoddeclaration in place of the void keyword. public
static
double
getAverage(int
num1,
int
num2,
int
num3)
2.
at the end of the body of the method, use the
return
statement to return the value to the calling method.