Expressions in Parameter Lists

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

What is wrong with the following code segment?

Point myPoint = new Point();  // construct a point at x=0, y=0
myPoint.move();               // move the point to a new location

Answer:

The second statement calls the move() method of the Point, but does not supply information about where to move the point.

Expressions in Parameter Lists

In this case, saying what method to run is not enough. The method needs information about what it is to do. When you look at the description of class Point the move() method is described as follows:

public void move(int x, int y);   // change (x,y) of a point object 

This description says that the method requires two parameters:

  1. The first parameter is type int. It will become the new value for x.
  2. The second parameter is type int. It will become the new value for y.

Dot notation is used to specify which object, and what method of that object to run. The parameter list of the method supplies the method with data. Here are some examples:

Point pointA = new Point();
Point pointB = new Point( 94, 172 );

pointA.move( 45, 82 );

int col = 87;
int row = 55;
pointA.move( col, row );

You can put expressions into parameter lists as long as the expression evaluates to the type expected by the method. Of course, the expressions are evaluated (using the usual rules) before the method starts running.

pointB.move( 24-12, 30*3 + 5 );
pointB.move( col-4, row*2 + 34 );

QUESTION 3:

What is the location of pointB after the following:

pointB.move( 24-12, 30*3 + 5 );