Download Methods for Making Application Modular in Computer Programming | CS 140 and more Study notes Computer Science in PDF only on Docsity!
Methods:Methods:^ Methods:Making Your Application Modular.Making Your Application Modular.Making Your Application Modular.
Overview^ OverviewIn these chapters you will learn:^
What is a method?
Declaring methods.
Using (or calling) methods.
Writing methods that take parameters.
Writing methods that return values
Declaring variables in the method.^
Scope of variables.
Method overloading
Java Methods^ Java Methods
In Java, a
method is a block of statements that has a
name and can be executed by
calling it from some
other place in your program.
You have already been using methods. For example:^
to print text to the console, you use the
println
or
print
methods.
to get an integer from the user, you use the
nextInt
method.
and the granddaddy of all methods,
main
With the exception of
main
, all the methods you’ve
used so far have been methods that are defined bythe Java API and that belong to a particular Javaclass.
The
main
method belongs to the class defined by
your application.
Why Do We Need Methods?^ Why Do We Need Methods?^
Modular programming: breaking a programup into smaller, manageable modules or“methods”.
Motivation for modular programming:^
Simplifies the process of writing programs
Enables a divide-and-conquer approach
Improves maintainability of programs
Prevents repeating code within the sameprogram.
Promotes code-reusability in later programs
Modular Design^ Modular Design public class MyClass{
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;
} }
This is an example of a modular design usingmethods
The Basics of Making a Method^ The Basics of Making a Method
public static return-type method-name( parameter-list ){
statements...
All methods — including the main method — mustbegin with a
method declaration.
Here’s the basic form for a method declaration,at least for the types of methods we have seen sofar:
This method canbe seen byother classes.
This method can exist even if noobjects are created from thisclass. Will explain this more later.
The data type of thevalue returned by themethod when it ends
Lists what can bepassed to the method. The name ofthis method.
The body of themethod
Declaring Methods in Your Class^ Declaring Methods in Your Class
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(); } }
Executionstarts at main
Methods that Take ParametersMethods that Take Parameters^ Methods that Take Parameters
Example^ Example
A method that takes three numbers and displays their average.
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);
}
To call this method:
.^ .^ .^ .^
.
dispAverage(
2,
4,
5);
.^ .^ .^ .^
.
Example^ Example
Putting it all together: public class AverageCalculator{
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.
The real benefit of methods comes when they canperform some task and then return the result of thattask to the calling method^
For example, a method that take thee numbers then returntheir average to the calling method.
To create a method that returns a value:1.
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.
The^ The
returnreturn
Statement.Statement.
Is used to return a value from a method tothe calling method. Here is the general formof it:
return expression;
The expression must evaluate to a valuethat’s the same type as the type listed in themethod declaration.
The
return
statement is last thing that will
be executed in the method body.