Sie sind auf Seite 1von 2

DRIVER SERVANT CHEAT SHEET

Servant: method header defined no semi-colon


A.
visibility return method (formal param formal parameter )
type name type name
______________________________________________________
B. Only if a class method, like the Math class had

visibility static return method (formal param formal param)
type name type name
_____________________________________________________
A. Examples, non-static:
public double nextDouble ( )
public int nextRandom (int p1)
________________________________________________
B. Example from Math class:

public static double sqrt (double pi)
________________________________________________

USING NON-STATIC METHODS
To use a method that is not static like methods of the
Scanner classs next double you must instantiate an object
like scan
Scanner scan = new Scanner (System.in);
then you can use the methods
double x = scan.nextDouble ( );


Driver: method being called (used) Has a semi-colon
1.
objectName. methodName (actual parameter)

_________________________________________
2.

Class Name. method Name (actual parameter)

_____________________________________________
1. Examples, non-static :

double x = scan.nextDouble ( );
int z = gen.nextRandom (9);
__________________________________________________
2. Example from Math class:

double d2 = Math.sqrt (16);

_________________________________________


USING A STATIC METHODs

Static methods are rarer than non-static methods.
Do not need to instantiate an object
You can use the method directly by using the class name
double d2 = Math.sqrt(16);

DRIVER SERVANT CHEAT SHEET
HELPER THE CALLED PROGRAM
Has no main method
Usually has many methods
Contains methods being defined
Since defined, has method headers
Each method being defined has a header and a method
body
Each method body is surrounded by curly braces
If the method has a return type specified in the header, it
needs a return statement.
If the method returns something, the type must be the
same as the type specified in the method header
In the method header we see formal parameter types and
formal parameter names. These have local scope, only
available within the method
In the method body if a variable is defined, it too has local
scope, only available within the method
If the value of a local variable is needed by another method
within the helper class, the local variable must be assigned
to a global variable
Variables defined within the class but not inside any
method has global scope.







DRIVER THE CALLING PROGRAM
Driver has a main method
Usually has one method, the main method
Driver needs to instantiate an object of the helper class.
Scanner scan = new Scanner (System.in);
Once an object has been instantiated, all methods of the
helper class are available to the driver:
double d1 = scan.nextDouble ( );

Driver does the calling of helpers methods by using:
objectName.methodName ( );

NOTE: If the method being called returns something, then
the Driver program must set up a variable to catch what the
helper is returning.

Since nextDouble ( ) returns a double value, we write:
double d1 = scan.nextDouble();
Here d1 will catch whatever nextDouble() is returning

The parameters sent by the Driver are called Actual
parameters, they are used by the methods of the Servant
class

Das könnte Ihnen auch gefallen