Skip to main content

Section 3.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 match method declaration (or be assignable)
  4. Determine what the method will return (if anything: data type, void, print, change state of object) and where it will be stored (nowhere, somewhere)
  5. Evaluate right hand side of assignment (if there is one). Value is dependent on method’s purpose

Subsection 3.12.1

You can watch this video or read through the content below it.

Subsection 3.12.2 Problem Statement 1

How can you access the value of PI? (Math.PI)

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

The value is stored in the Math class, with only static members.

Subsection 3.12.4 SG2: Write (instance / class) dot method name and ( )

First, check the API to find the constant value we need.
Figure 3.12.1.
Math.PI;

Subsection 3.12.5 SG3: Determine whether parameter(s) are appropriate

Not used in this example. We are accessing a data member, not calling a method.

Subsection 3.12.6 SG4: Determine what the method will return (if anything: data type, void, print, change state of object) and where it will be stored (nowhere, somewhere)

According to the API documentation, the PI value is a double-precision decimal, so we need a double type variable to store the value.
double pi = Math.PI;

Subsection 3.12.7 SG5: Evaluate right hand side (RHS) of assignment (if there is one). Value is dependent on method’s purpose

Notice the lack of parentheses from the previous subgoal’s code. We are directly accessing a value stored statically with the Math class, not calling a method. Generally, we would not store a copy of this value into a new variable, but use it directly in an expression (see chapter Evaluating Expressions).

Subsection 3.12.8 Problem Statement 2

How can you get the square root of PI? (Math.sqrt)

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

When performing math operations, it can be useful to check the Math class API documentation.
Figure 3.12.2.
The sqrt function is a static method of the class Math.

Subsection 3.12.10 SG2: Write (instance / class) dot method name and ( )

Math.sqrt();

Subsection 3.12.11 SG3: Determine whether parameter(s) are appropriate

In the documentation, we can drill down for more details on the sqrt function, which describes the 1 double parameter. We wish to take the square root of pi, which is a double value we know how to access from Problem Statement 1 above.
Math.sqrt(Math.PI);
Note: You can pass some other numeric types, such as int, and they will be promoted to double if possible. Otherwise you will get an error.

Subsection 3.12.12 SG4: Determine what the method will return (if anything: data type, void, print, change state of object) and where it will be stored (nowhere, somewhere)

According to the API documentation, the sqrt method returns a double type value, so we need a double type variable to store the value.
double value = Math.sqrt(Math.PI);

Subsection 3.12.13 SG5: Evaluate right hand side (RHS) of assignment (if there is one). Value is dependent on method’s purpose

As written in the previous subgoal, the sqrt method will calculate the square root of our parameter (Math.PI) and return the result.

Practice Pages.

You have attempted of activities on this page.