An Introduction to Methods and Modularity
with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
public static void main(String[] identifier)that is passed the command line arguments when the program is executed. Note: The formal parameter can have any identifier (though
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:
public class Geometry { public static double rectangleArea(double width, double height) { return (width * height); } }
area = Geometry.rectangleArea(300.0, 10.0);
Geometry
Class:
area = rectangleArea(10.0, 10.0);
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
/** * A utility class for performing calculations involving * geometric shapes. * * @author Prof. David Bernstein, James Madison University * @version 1.0 */ public class Geometry { /** * Calculate the area of a circle. * * @param diameter The diameter of the circle * @return The area */ public static double circleArea(double diameter) { double area, radius; radius = diameter / 2.0; area = Math.PI * Math.pow(radius, 2.0); return area; } /** * Calculate the area of a rectangle. * * @param width The width of the rectangle * @param height The height of the rectangle * @return The area */ public static double rectangleArea(double width, double height) { double area; area = width * height; return area; } /** * Calculate the area of a aquare. * * @param side The size of the sides * @return The area */ public static double squareArea(double side) { return rectangleArea(side, side); } /** * Return the number of angles for a closed geometric * shape with the given number of sides. * * @param sides The number of sides in the shape of interest * @return The number of angles in the shape */ public static int anglesIn(int sides) { return sides; } }
public class AreaCalculator { public static void main(String[] args) { double area, size; size = Double.parseDouble(args[0]); JMUConsole.open(); area = Geometry.squareArea(size); JMUConsole.print("The area of the square is "); JMUConsole.print(area); JMUConsole.print("\n"); area = Geometry.circleArea(size); JMUConsole.print("The area of the circle is "); JMUConsole.print(area); JMUConsole.print("\n"); JMUConsole.close(); } }
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 |
public static double fuelPerformance(double miles, double gallons) { double result; result = miles / gallons; return result; }
double fuelUsed; double milesDriven; double mpg; fuelUsed = 20.0; milesDriven = 643.5; fuelPerformance(milesDriven, fuelUsed); // What happens if you try to do this? Why? mpg = result;
double fuelUsed; double milesDriven; double mpg; fuelUsed = 20.0; milesDriven = 643.5; mpg = fuelPerformance();
public static double fuelPerformance() { double result; // What happens if you try to do this? Why? result = milesDriven / fuelUsed; return result; }
double fuelUsed; double milesDriven; double mpg; fuelUsed = 20.0; milesDriven = 643.5; mpg = fuelPerformance(milesDriven, fuelUsed);
public static double fuelPerformance(double milesDriven, double fuelUsed) { double result; result = milesDriven / fuelUsed; // What happens when you do this? Why? milesDriven = 0.0; return result; }