In the previous section, we used simple methods like forward() and turnRight() to make the player move around. You may have noticed that forward() always moves the same distance (10 pixels), and turnRight() and turnLeft() always turn at right angles (90 degrees). This is a little limiting. What if we wanted the player to move 100 pixels? or move diagnoally? This requires different distance length and smaller/larger angles. We can add complex methods in the Player class that let you specify the number of pixels to move forward or the number of degrees to turn. These values that you can give to methods to help them do their job are called arguments or parameters.
The parentheses () after method names are there in case you need to give the method actual parameters or arguments (some data) to do its job. For example, we can give the argument 100 in forward(100) to make the player go forward 100 pixels or the argument 30 in turn(30) to make the player turn 30 degrees instead of 90 degrees.
Although some people use the words parameters and arguments interchangeably, there is a subtle difference. When you create your own method, the variables you define for it are called formal parameters. When you call the method to do its job, you give or pass in arguments or actual parameters to it that are then saved in the parameter variables. So, in the definition of the forward method, it has a parameter variable called distance, and in the call to forward(100), the argument is the value 100 which will get saved in the parameter variable pixels. You will learn to write your own methods in Unit 5. In this unit, you will learn to call methods that are already written for you.
// Method call
player1.forward(100); // argument is 100
// Method definition written for you
public void forward(int distance) // parameter distance
...
Subsection6.9.1Tracing Methods
You will not write your own methods until Unit 5, but you should be able to trace and interpret method calls like below.
Here is another version of the Old MacDonald Song with a more powerful abstraction. The method verse has 2 parameters for the animal and the noise it makes, so that it can be used for any animal. Use the Code Lens button or this Java visualizer to step through the code.
public class MethodTrace
{
public void square(int x)
{
System.out.print(x*x);
}
public void divide(int x, int y)
{
System.out.println(x/y);
}
public static void main(String[] args) {
MethodTrace traceObj = new MethodTrace();
traceObj.square(5);
System.out.print(" and ");
traceObj.divide(4,2);
}
}
25 and 2
Correct.
25 and .5
The order of the arguments to the divide(x,y) method will divide x by y and return an int result.
2 25
The square(x) method is called before the divide(x,y) method.
25 2
The main method prints out " and " in between the method calls.
public void splitPizza(int numOfPeople)
{
int slicesPerPerson = 8/numOfPeople;
/* INSERT CODE HERE */
}
public void printSlices(int slices)
{
System.out.println("Each person gets " + slices + " slices each");
}
Which of the following lines would go into /* INSERT CODE HERE */ in the method splitPizza in order to call the printSlices method to print the number of slices per person correctly?