JMU
An Introduction to Methods and Modularity
with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Motivation
Achieving Modularity in Object-Oriented (and Other) Languages
Modular Programs in Java
Declaring Classes in Java
Declaring Methods in Java
Invoking/Calling Methods in Java
Invoking/Calling Methods in Java (cont.)
Invoking/Calling Methods in Java (cont.)
More on Parameters (a.k.a. Arguments)
More on Parameters (cont.)
More on the Returned Value/Result
More on the Returned Value/Result (cont.)
A Method with One Parameter
javaexamples/basics/FunctionExamples.java (Fragment: circleArea)
    public static double circleArea(double diameter)
    {
       double    pi;
       double    radius;
       double    result;
       
       pi     = 3.1415926;
       radius = diameter / 2.0;
       
       result = pi * (radius * radius);
       
       return result;       
    }
        
A Method with Multiple Parameters
javaexamples/basics/FunctionExamples.java (Fragment: fuelPerformance)
    public static double fuelPerformance(double miles, double gallons)
    {
       double     result;
       
       result = miles / gallons;
       
       return result;       
    }    
        
An Example of Using a Method
An Example of Using a Method (cont.)

Review Questions

A Visualization of a Method Call
images/railroad_method.gif
A Visualization of a Method Call and Return
images/railroad_method_call-and-return.gif
Some Important Methods in the Java API
Class Constants in Java
Class Constants in Java (cont.)
Some Important Class Constants in the Java API
Putting It All Together - A Utility Class
javaexamples/basics/Geometry.java
/**
 * 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;
    }
}
        
Putting It All Together - A Main Class
javaexamples/basics/AreaCalculator.java
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).

An Introduction to Scope
An Introduction to Scope (cont.)
javaexamples/basics/FunctionExamples.java (Fragment: fuelPerformance)
    public static double fuelPerformance(double miles, double gallons)
    {
       double     result;
       
       result = miles / gallons;
       
       return result;       
    }    
        
javaexamples/basics/FunctionExamples.java (Fragment: fuelUseReporter)
       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;       
        
An Introduction to Scope (cont.)
javaexamples/basics/FunctionExamples.java (Fragment: noParameters)
       double       fuelUsed;
       double       milesDriven;
       double       mpg;
       

       fuelUsed    = 20.0;
       milesDriven = 643.5;
       mpg         = fuelPerformance();       
        
javaexamples/basics/FunctionExamples.java (Fragment: scope1)
    public static double fuelPerformance()
    {
       double     result;
       
       // What happens if you try to do this? Why?
       result = milesDriven / fuelUsed;
       
       return result;       
    }    
        
An Introduction to Scope (cont.)
javaexamples/basics/FunctionExamples.java (Fragment: actualParameters)
       double       fuelUsed;
       double       milesDriven;
       double       mpg;
       

       fuelUsed    = 20.0;
       milesDriven = 643.5;
       mpg         = fuelPerformance(milesDriven, fuelUsed);       
        
javaexamples/basics/FunctionExamples.java (Fragment: scope2)
    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;       
    }