Changing Data inside a Point

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

Answer:

Just as the program is about to close, how many objects have been created ?     Six — three Point objects and three temporary String objects

How many object references are there?     Three — each referencing a Point

Has any garbage been created?    Three objects — each temporary String object

Changing Data inside a Point

Look again at the description of class Point. One of the methods is:

public void move( int x, int y ) ;

This method is used to change the x and the y data inside a Point object. The modifier public means that it can be used anywhere in your program; void means that it does not return a value. This part of the description

( int x, int y )

says that when you use move, you need to supply two int parameters that give the new location of the point. A parameter is information you supply to a method.

Here is the example program, modified again:

import java.awt.*;
class PointEg4
{

  public static void main ( String arg[] )
  {
    Point pt = new Point( 12, 45 );   // construct a Point
    System.out.println( pt );     

    pt.move( -13, 49 ) ;              // change the x and y in the Point
    System.out.println( pt ); 

  }
}

Here is what it writes to the screen:

java.awt.Point[x=12,y=45]
java.awt.Point[x=-13,y=49]

QUESTION 12:

How many Point objects are created by this program?

How many temporary String objects are created by this program?