# Subgoals

## Section2.12Worked 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

### Subsection2.12.1

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

### Subsection2.12.2Problem Statement 1

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

### Subsection2.12.3SG1: Classify method as static method or instance method

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

### Subsection2.12.4SG2: Write (instance / class) dot method name and ( )

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


### Subsection2.12.5SG3: Determine whether parameter(s) are appropriate

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

### Subsection2.12.6SG4: 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;


### Subsection2.12.7SG5: 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).

### Subsection2.12.8Problem Statement 2

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

### Subsection2.12.9SG1: Classify method as static method or instance method

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

### Subsection2.12.10SG2: Write (instance / class) dot method name and ( )

Math.sqrt();


### Subsection2.12.11SG3: 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.

### Subsection2.12.12SG4: 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);


### Subsection2.12.13SG5: 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.