An Introduction to Methods and Modularity
with Examples in Java |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
args
is common) but must be
a String[]
.
.java
appended)public
(more on this
in CS159)static
indicating that
the method "belongs to" the class (more on this
later)public static double circleArea(double diameter)
private static boolean isPrime(int n)
Geometry.circleArea(5.0)
circleArea(5.0)
main()
)java GradeCalculator
void
) or one valuevoid
Methods:
Geometry
Class:
Review Questions
Math
Class:
double abs(double d)
, int abs(int i)
, ...double sin(double d)
, ...double log(double d)
, double log10(double d)
, ...double min(double d, double e)
,
double max(double d, double e)
, ...double pow(double d, double p)
double sqrt(double d)
Integer
Class:
int parseInt(String s)
Double
Class:
double parseDouble(String s)
Array
Class:
int getLength(double[] a)
, ...static
modifier)
public
they can even
be used outside of the class (e.g., Math.PI
)
final
they can't
be changed
Make sure you can trace the execution of the above program
assuming it is executed with a single command-line parameter
(e.g., the String
10
).
Block | Within a block of code (i.e., between a
{ and } ) |
Method | Within a method (which includes both formal parameters and variables declared within the method) |
Class | Everywhere within a class |
Global | Everywhere |