A static method takes zero or more arguments and returns an answer. Use it in an expression. For example, use the Math.sqrt method to compute the (approximate) square root of a number. Expression
double c = 2*Math.sqrt(49.0);makes c = 14.0. The argument can be any expression of the correct type. For example,
double r = 16.0; double y = Math.sqrt(2*r + 4);makes y = 6.0. The Math.pow method takes two real numbers x and y and returns xy. For example,
double z = Math.pow(3.0, 4.0);makes z approximately equal to 81.0.
Arguments or parameters
I will use words argument and parameter interchangeably. Each refers to something that is passed to a method. |
Parentheses are required around arguments
Arguments to a method must be enclosed in parentheses. If there are two or more arguments, they must be separated by commas. |
Methods with no arguments
Some methods take no arguments. Write an
empty set of parentheses to show no arguments.
For example, if method mystic takes no
arguments and returns an integer, then
int n = mystic();runs method mystic and sets n to its result. |
Use the correct number of arguments
Most methods have a fixed number of arguments. If a method takes two arguments, the use it with two arguments. If it takes one argument, then use it with one argument. |
Know what a method does
Each method does a particular job. It is critical that you understand what it does. It will do you know good to hope that it does whatever you want it to do. Understand what type of value it returns, what that value means, and how the arguments affect that. |
Suppose that variables a, b and c have type double, and each already has a value. Write a statement that creates variable d of type double and makes d = √b2 - 4ac. Answer
If a given method takes one parameter, are you allowed to use it with two parameters? Answer