CIS 234: Methods

Download Report

Transcript CIS 234: Methods

CIS 234: Java Methods
Dr. Ralph D. Westfall
April, 2010
What is a Method?

a block of code that does something
has a name and parentheses after name
 curly brackets enclose statement(s) in body
 usually has header declarations, and often
has arguments inside the parentheses
public static void main (String [] args)
{
System.out.println("Hi!");
}

Declarations - type of access

modifiers before method name

public – any other class can use method



main method must be public
private – only methods in same class can
use it
can leave out the access modifier

defaults to friendly – can be used by
other classes in same "package" (a
package is somewhat like a directory)
Declarations - static

static means unchanging



all objects created from this class will use
this same method
there will not be additional copies of a
static class method
if don't use word static, each object
created from this class will have its own
instance (copy) of method
Declarations – void or [type]

return value type
void means doesn't return any data
 if not void, need to identify the type of
data the method will return
public static long cubeIt(int x) // int arg
{ // returns a long integer
return x * x * x;
}

Arguments (args)

the parentheses often enclose
"argument(s)" (data) that the method
uses, identified by data type
public void say(String what) // argument
{
System.out.println(what); // used here
} // from Hello, world! Person class code
Arguments - 2


Java is "strongly typed," so the data
type of every variable must be declared
somewhere in a Java class
argument data types are declared
within method's parentheses
public static double raised(int salary, double pct)
{
return (100 + pct) / 100.0) * salary;
}
Calling a Method

from within its class
method name([arguments, if has any]) ;
say("Hi!");
// literal argument


from another object or class
[object].[method name]([arguments]) ;
myObject.cube(num); // variable argument
myObject.raised(empSalary, 5); //both types
Math.sqrt(4); // literal in a class method

Calling a Method - 2

arguments must be in right order
static double raise(double salary, double pct)
......
raise (wages, increase) ; // correct order
raise(7, 50000) ;
// logical error

argument names don't need to match
myObject.raise(pay, increase) ;
// declared in method as
// (double salary, double pct)
"Signature"

a method's "signature" is a pattern:

number, order, and types of arguments


can have 0 [called with [name]( );] to many
an "overloaded" method has more than
one signature

e.g., can be called with different patterns
of arguments:
(); (int x); (int x, double y); (int x, char y);
Types of Java Programs

applications



class that can run without any other
programs (often used with other classes)
class(es) that doesn't run by itself, but is
used by another application class
applet

class runs inside a web browser (the
browser actually is a program)
// [PgDn]
Types of Java Programs - 2

servlet



runs on a server and makes it possible to
generate web pages with database content
programmer can create a Java Server Page
(JSP), which then gets compiled into a
servlet (Blackboard pages are servlets)
servlets are part of the Java Enterprise
Edition (JDK + Java EE bundle)
Main Method

one class in a Java application must
have a main( ) method


but a class used by an application doesn't
need a main( ) method
main method must be declared as:
public static void
Main Method - 2

main method runs first when application
starts



it can call methods in its own class, and/or
use methods in other classes
methods it calls in its own class must be
static
variables that are not inside any method in
the class must also be static
Main Method - 3

main method has String[ ] argument(s)
means that you can "pass" one or more
words as inputs into class when you run it
public class MyProgram
{
public static void main (String[ ] args)
[in DOS after class has been compiled]
C:\>[path] java MyProgram Le 19 // 2 args

Using Extra Methods in a Class

saves typing (or pasting)


can use a separate method, rather than
keep repeating same block of code in main
method
makes code easier to read and maintain


smaller programs
code is organized into blocks //Notes Page
Exercise

following pattern in DemoMethods.java,
fill in the blanks in the handout to:


declare 3 numbers and one String as
member variables (above main method),
using meaningful variable names
assign values to three of these variables in
the loadData method (leave one of the
numeric variables without a value
Exercise (part 2)

following pattern in DemoMethods.java,
write the following 4 methods:




print a literal value with say( method
add 2 numbers together and store them in
a member variable (above main method)
print this calculated member variable
multiply two numbers and print result with
some identifying text