This document provides a cheat sheet for understanding methods in Java, including how to define methods, call static vs non-static methods, and the relationship between driver and servant classes. It explains that servant classes define methods without a main method, methods have headers and bodies, and parameters have local scope. Driver classes call methods by instantiating objects from servant classes or using class names for static methods, and pass actual parameters to methods.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
50 views
Driver Servant Cheat Sheet
This document provides a cheat sheet for understanding methods in Java, including how to define methods, call static vs non-static methods, and the relationship between driver and servant classes. It explains that servant classes define methods without a main method, methods have headers and bodies, and parameters have local scope. Driver classes call methods by instantiating objects from servant classes or using class names for static methods, and pass actual parameters to methods.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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)
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
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More