Skip to main content

Section 4.12 Worked Example: Call a method of Math

Subgoals for Calling a Method.

  1. Classify method as static method or instance method
    1. If static, use the class name
    2. If instance, must have or create an instance
  2. Write (instance / class) dot method name and ( )
  3. Determine whether parameter(s) are appropriate
    1. Number of parameters passed must match method declaration
    2. Data types of parameters passed must be compatible with method declaration
  4. Determine what the method will return and where it will be stored
  5. Evaluate right hand side of assignment. Value is dependent on method’s purpose

Subsection 4.12.1 Problem Statement

Write the Java code to calculate and store the area of a circle, where the radius is an input from the user.

Subsection 4.12.2

You can reference section 3.8 to remind yourself how to declare and instantiate a Scanner object to read input from the user. Here is what the code should look like:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the radius");  // prompt the user for input
double radius = input.nextDouble();

Subsection 4.12.3 SG1: Classify method as static method or instance method

First, we need to determine what method we need to call.
Figure 4.12.1.
The formula for calculating the area of a circle is π*radius2. Looking at the documentation for the Math class, we see the pow() method which will raise a number to a power. You can also see the keyword static in the documentation for this method, so the method is a static method.
Because we are calling a static method, we do not need an instance. We will call the method using the class name (Math).
If want to call an instance method, we need to identify the instance which will call the method. In this example we have already created a Scanner instance which is named input.

Subsection 4.12.4 Write (instance / class) dot method name and ( )

Here is the method call:
Math.pow( );

Subsection 4.12.5 Determine whether parameter(s) are appropriate

In the documentation, we see that two parameters are required. The first argument is the base and the second argument is the exponent. In our problem, we want to square the value of the radius. So the first argument will be the radius variable and the second will be the value 2.
Let’s look at the data types of the parameters - both are of type double. Our radius variable was declared of type double, so that value is compatible as the declared parameter data type. The value 2 is an integer, but is compatible with the data type double, as an integer value can be assigned to a double variable. (Remember, you can look back to the assignment subgoals to determine the validity of this assignment.)
So the code for this method call looks like:
Math.pow(radius, 2);

Subsection 4.12.6 Determine what the method will return and where it will be stored

According to the API documentation, the pow method returns a double, so we need a double type variable to store the value.
double result = [value of Pi] * Math.pow(radius, 2);
Some common constant values are defined in the Java language and Pi is one of these. It happens to be declared in the Math class:
Figure 4.12.2.
To reference this value, you access the value in the same manner we do the methods, by using the class name:
Math.PI;

Subsection 4.12.7 Evaluate right hand side (RHS) of assignment. Value is dependent on method’s purpose

In the solution for this example, both the pow method and Math.PI return double values. A double multiplied by a double is a double which is then assigned to our declared double variable result.
Here’s the complete solution to the problem:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the radius");  // prompt the user for input
double radius = input.nextDouble();
double result = Math.PI * Math.pow(radius, 2);
Note that using members of the Math class does not require an import statement as that class is already included with any Java program.

Practice Pages.

You have attempted of activities on this page.