Activity 1.10.1.
Click on the next button below the code to step through the code and watch the red arrow jump to the method that is being run and watch how the argument is passed into the parameter variable and the return value.
// static method header
public static return-type method-name(parameters)
{
// method body
}
square(3)
would return 9.
static void
, the header includes static int
to indicate that the method returns an integer value. There could be another overloaded version of this method that takes a double number and returns a double value; remember methods are overloaded when there are multiple methods with the same name but different signatures with a different number or types of parameters.
public static int square(int number)
{
int result = number * number;
return result;
}
https://pythontutor.com/render.html#code=public%20class%20SquareMethod%0A%7B%0A%20%20%20%20%20%20%20%20%20public%20static%20int%20square%28int%20number%29%20%0A%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20result%20%3D%20number%20*%20number%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20result%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%0A%20%20%20%20%20%20%20%20%20public%20static%20void%20main%28String%5B%5D%20args%29%0A%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20x%20%3D%205%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20y%20%3D%20square%28x%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println%28%22The%20square%20of%20%22%20%2B%20x%20%2B%20%22%20is%20%22%20%2B%20y%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20x%20%3D%204%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println%28%22The%20square%20of%20%22%20%2B%20x%20%2B%20%22%20is%20%22%20%2B%20square%28x%29%29%3B%0A%20%20%20%20%20%20%20%20%20%7D%0A%7D&cumulative=false&curInstr=11&heapPrimitives=nevernest&mode=display&origin=opt- frontend.js&py=java&rawInputLstJSON=%5B%5D&textReferences=false
square
method, the return value can be stored in a variable or used as part of an expression. In the main method above, the variable y
is assigned the return value of the square
method. The return value can also be used directly in the print statement without storing it in a variable, for example System.out.println(square(4));
.
// Saving the returned value of the square method in a variable
int y = square(5);
System.out.println(y); // prints 25
// Printing the returned value of the square method directly
System.out.println(square(4)); // prints 16
https://pythontutor.com/render.html#code=%20%20%20%20%20%20public%20class%20MethodTrace%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20public%20static%20int%20square%28int%20x%29%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%28x%20*%20x%29%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20public%20static%20int%20divide%28int%20x,%20int%20y%29%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20%28x%20/%20y%29%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20public%20static%20void%20main%28String%5B%5D%20args%29%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println%28square%283%29%20%2B%20divide%285,2%29%29%3B%20%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt- frontend.js&py=java&rawInputLstJSON=%5B%5D&textReferences=false
public class MethodTrace
{
public static int square(int x)
{
return (x * x);
}
public static int divide(int x, int y)
{
return (x / y);
}
public static void main(String[] args)
{
System.out.println(square(3) + divide(5,2));
}
}
square
method is in a class called MathFunctions
, we would call it as MathFunctions.square(3)
. Class methods are typically called using the class name along with the dot operator (.). When the method call occurs in the defining class, the use of the class name is optional in the call.
Math
class in Java and will need to call them with the class name Math
. There is a method to compute the square of a number in the Math
library, but it is called pow
instead of square, and it takes 2 arguments to return a number raised to the power of an exponent number. Hereβs a quick preview of two of the methods in the Math class:
Math.sqrt(double number)
: returns the square root of a given number
Math.pow(double base, double exponent)
: returns \(base^{exponent}\text{,}\) the value of base, the first argument, raised to the power of exponent, the second argument.
double x = Math.pow(3, 2); // 3^2 is 9.0
double y = Math.sqrt(9); // the square root of 9 is 3.0
a
and b
given the Pythagorean Theorem \(c = \sqrt{a^{2} + b^{2}}\) where \(a\) and \(b\) are the lengths of the legs and \(c\) is the length of the hypotenuse?
Math.sqrt(a * a + b * b)
a * a
is a squared, likewise b * b
. Adding them with +
gives us the sum which is then passed to Math.sqrt
.Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2))
Math.pow(a, 2)
is a
squared, likewise Math.pow(b, 2)
. Adding them with +
gives us the sum which is then passed to Math.sqrt
.Math.sqrt(a + b)
a * a + b * b
Math.sqrt
to get the length of the hypotenuse.ladderSizeNeeded
method below using the Pythagorean Theorem and the Math.sqrt
method. Then in the main method, write a method call to test the ladderSizeNeeded
method with arguments for the height of 30 and the width of 40. The method should return the length of the ladder needed to reach the window of your beloved.
public static double calculatePizzaBoxes(int numOfPeople, double slicesPerBox)
{ /*implementation not shown */}
public void static inchesToCentimeters(double i)
{
double c = i * 2.54;
printInCentimeters(i, c);
}
public void static printInCentimeters(double inches, double centimeters)
{
System.out.print(inches + "-->" + centimeters);
}
inchesToCentimeters(10)
appears in a static method in the same class. What is printed as a result of the method call?