public static void nameAndAddress()

Download Report

Transcript public static void nameAndAddress()

Lesson 8
Programming Using Methods
Creating Methods with No Arguments
• Method – a series of statements that
carry out some task.
• Any class can contain an unlimited
number of methods.
• The simplest methods invoked do not
require any arguments (parameters).
Adding to a Simple Hello World Program
// Example Program Welcome.java
// A first program in Java
public class Welcome
{
public static void main( String args[] )
{
System.out.println( "Welcome to Java Programming!" );
}
}
Suppose we want to add the three lines
Event Handlers Incorporated
8900 Airport Road
Bismarck, ND 58504
Adding to your First Program (cont)
•
There are two major reasons to create a
method to display the three new lines.
1. The main() method will remain short and easy
to follow because main() will contain just one
statement which is a call to the other method
rather than 3 println statements.
2. The method is easily reusable. After you create
it, it can be used in any program which requires
this address.
Construction of a Method
• A method must include the following:
–
–
–
–
A declaration (header)
An opening curly bracket
A body
A closing curly bracket
• The method declaration (header) contains:
–
–
–
–
–
–
Optional access modifiers
The return type for the method
The method name
An opening parenthesis
An optional list of parameters
A closing parenthesis
The nameAndAddress method
public static void nameAndAddress()
{
System.out.println(“Event Handlers Incorporated”);
System.out.println(“8900 Airport Road”);
System.out.println(“Bismarck, ND 58504”);
}
• When this method is called, it will print
the above three lines of code to the
screen.
• It is called by it’s name
“nameAndAddress”
Complete Program
//Example Program Welcome.java
//A first program in Java
public class Welcome
{
public static void main (String args[])
{
nameAndAddress();
System.out.println(“First Java Program”);
}
public static void nameAndAddress()
{
System.out.println(“Event Handlers Incorporated”);
System.out.println(“8900 Airport Road”);
System.out.println(“Bismarck, ND 58504”);
}
}
Methods that Use a Single Argument
• Methods that do not require any arguments
are simple to write and can be used in certain
situations.
• However, they are limited because they have
no communication with the calling class.
Ex.
Suppose you are writing a program to create
restaurant reservations. Without some form of
communication with the calling class, you would
have to use a different method for every date of
the year at every possible time of the day. By
supplying the method with the date and time, you
could create the same process once and just send
it different information.
Creating Arguments
• When creating arguments you need to have
the type of the argument and a local name
for the argument.
//Example Method accepting one argument
public static void predictRaise ( double moneyAmount)
{
doulbe newAmount;
newAmount = moneyAmount * 1.10;
System.out.println(“With raise salary is ” + newAmount);
}
• The method predictRaise is a void method
since it is not returning a value (i.e. it is
performing a task)
Argument Complete Program
public class DemoRaise
{
public static void main(String[] args)
{
double mySalary = 200.00;
System.out.println(“Demonstrating some raises”);
predictRaise(400.00);
predictRaise(mySalary);
}
public static void predictRaise ( double moneyAmount)
{
doulbe newAmount;
newAmount = moneyAmount * 1.10;
System.out.println(“With raise salary is ” + newAmount);
}
}
Program Output
Demonstrating some raises
With raise salary is 440.00
With raise salary is 220.00
Tips for Passing Arguments
• If more than one argument is passed, you
must list each type separately.
• The method call must have the same number
of arguments as the method header and
arguments and values must be in the correct
order.
• The arguments in the method call are called
actual parameters, whereas the arguments in
the method header are called formal
parameters.
Methods that Return Values
• Instead of performing a task, a method
can return a value.
• The return value can be any java data
type.
• The return type is commonly called the
method’s type.
Return Type Method Example
• Notice how the word double has been used
instead of the word void.
//Example of a return type
public static double predictRaise ( double moneyAmount)
{
doulbe newAmount;
newAmount = moneyAmount * 1.10;
return newAmount;
}
• With every return type function, you must
include a return statement to return a single
value.
Return Type Complete Program
public class DemoRaise
{
public static void main(String[] args)
{
double mySalary = 200.00;
double newsalary;
System.out.println(“Demonstrating some raises”);
newsalary = predictRaise(400.00);
newsalary = predictRaise(mySalary);
System.out.println(predictRaise(300.00);
//Note: this consists of the method calls, you would need some
println statements to get three different outputs.
}
public static double predictRaise ( double moneyAmount)
{
doulbe newAmount;
newAmount = moneyAmount * 1.10;
return newAmount;
}
}
Bottoms Up