Jonie
2005-12-21 18:31:31 UTC
Hi,
when I compile Model file (see below) with other classess I get the
message that says "Model.java uses unchecked or unsafe operations". It
seems to me that program runs fine, but I want to know why this happens
and, if possible, fix it. Thank you.
- Jonie
--------------------------------------------------------------------------
import java.util.*;
import java.util.Scanner;
import java.awt.Point;
import uchicago.src.sim.space.*;
import uchicago.src.sim.gui.*;
public class Model {
private int numberOfAnts = 8;
private int numberOfAntEaters = 2;
private ArrayList <Ant> antList;
private ArrayList <AntEater> antEaterList;
private ArrayList allList;
private int worldSizeX = 10, worldSizeY = 10;
private Object2DGrid world;
private Object2DDisplay worldDisplay;
private DisplaySurface dsurf;
private java.util.Random rng;
public void initializeModel () {
System.out.printf( "-> Entering initalizeModel...\n" );
rng = new java.util.Random();
Animal.setRNG( rng );
Animal.setModel( this );
// create a world for the Ants to live in, and a list to keep track of
them
world = new Object2DGrid( worldSizeX, worldSizeY );
antList = new ArrayList <Ant> ();
antEaterList = new ArrayList <AntEater> ();
allList = new ArrayList ();
// lets create a bunch of ants, adding them to the antList
// and placing them randomly in the world ( <= 1 per cell ).
System.out.printf( "\nCreate %d ants...", numberOfAnts );
for ( int i = 0; i < numberOfAnts; ++i ) {
Ant anAnt = new Ant( );
int x, y;
do { // keep sampling spots until we find an empty one
x = rng.nextInt( worldSizeX );
y = rng.nextInt( worldSizeY );
} while ( world.getObjectAt( x, y ) != null );
anAnt.setX( x );
anAnt.setY( y );
world.putObjectAt( x, y, anAnt );
antList.add( anAnt );
allList.add( anAnt );
}
// lets create a bunch of antEaters, adding them to the antEaterList
// and placing them randomly in the world ( <= 1 per cell ).
System.out.printf( "\nCreate %d ants...", numberOfAnts );
for ( int i = 0; i < numberOfAntEaters; ++i ) {
AntEater anAntEater = new AntEater( );
int x, y;
do { // keep sampling spots until we find an empty one
x = rng.nextInt( worldSizeX );
y = rng.nextInt( worldSizeY );
} while ( world.getObjectAt( x, y ) != null );
anAntEater.setX( x );
anAntEater.setY( y );
world.putObjectAt( x, y, anAntEater );
antEaterList.add( anAntEater );
allList.add( anAntEater );
}
// Lets create the objects to display the ants on the screen
dsurf = new DisplaySurface( null, "Ant Display" );
worldDisplay = new Object2DDisplay( world );
worldDisplay.reSize( 200, 200 );
dsurf.addDisplayable( worldDisplay, "Ants");
dsurf.display();
System.out.printf( "The initial ants:\n" );
printAntList( antList );
System.out.printf( "The initial anteaters:\n" );
printAntEaterList( antEaterList );
System.out.printf( "The initial objects in the world:\n" );
printWorld();
System.out.printf( "\n<- Leaving initalizeModel.\n" );
}
// run
// repeatedly ask the user for a number of "time steps" to run the
model.
// (prompt in the terminal window where the program was started).
// If 0, stop the program.
// Otherwise execute the Model step() message the requested number
of steps,
// and ask again.
// NB: no real checking on the input (eg is it an int, etc).
//
public void run ( ) {
//... Initialize Scanner to read from console.
Scanner input = new Scanner(System.in);
int steps = 0;
do {
System.out.printf( "Enter int number of steps (0->stop): " );
steps = input.nextInt();
for ( int s = 0; s < steps; ++s ) {
step();
}
} while ( steps != 0 );
}
// step
// Execute all activity for one "time step" of the model
// In this simple model, just:
// - tell each Ant to execute its step() method
// - tell the displaySurface to update itself
//
public void step () {
System.out.printf( " -> Take a Model step...\n" );
for ( int i = 0; i < allList.size(); ++i ) {
Object obj = allList.get(i);
if (obj.getClass() == Ant.class )
((Ant) obj).step();
else
((AntEater) obj).step();
}
dsurf.updateDisplay();
System.out.printf( "- antList now:\n" );
printAntList( antList );
System.out.printf( "- antEaterList now:\n" );
printAntEaterList( antEaterList );
printWorld();
System.out.printf( "\n" );
}
// moveObjectInWorld
// try to move object to the requested dx,dy .
// note we must check to be sure the new location is "legal",
// i.e., cell is empty and cell is not off the edge of the world.
// NB: if either new* values is illegal, don't move.
// If move is ok:
// - tell the world the object is not where it was
// - tell the world the object is now at the new* location
// Return: null if not moved, otherwise return a Point object
// with new x,y values.
//
public Point moveObjectInWorld ( Drawable obj, int dX, int dY ) {
int newX = obj.getX() + dX; // get location it wants to move to
int newY = obj.getY() + dY;
// first check to be sure new location is in the world!
if ( newX < 0 || newY < 0 || newX >= worldSizeX || newY >= worldSizeY
) {
System.out.printf( " - obj tried to move offworld (%d,%d).\n",
newX, newY );
return null;
}
// see if new cell is empty.
if ( world.getObjectAt( newX, newY ) != null ) {
System.out.printf( " - obj tried to move to occupied cell
(%d,%d).\n",
newX, newY );
return null;
}
// its ok to move, so tell the world and the object
world.putObjectAt( obj.getX(), obj.getY(), null ); // old cell empty
now
world.putObjectAt( newX, newY, obj ); // obj in new cell now
Point loc = new Point( newX, newY );
return loc;
}
// printAntList
// a method to print the contents of an Ant list
public static void printAntList ( ArrayList <Ant> list ) {
for ( Ant a : list ) {
a.printSelf();
}
}
// printAntEaterList
// a method to print the contents of an Ant list
public static void printAntEaterList ( ArrayList <AntEater> list )
{
for ( AntEater a : list ) {
a.printSelf();
}
}
// printWorld
// print places where objects are in the world, and total
public void printWorld () {
System.out.printf( "-> The world contents:\n" );
int numObjs = 0;
for ( int x = 0; x < worldSizeX; ++x ) {
for ( int y = 0; y < worldSizeY; ++y ) {
if ( world.getObjectAt( x, y ) != null ) {
System.out.printf( " object at %d,%d.\n", x, y );
++numObjs;
}
}
}
System.out.printf( "<- %d objects in the world.\n", numObjs );
}
}
when I compile Model file (see below) with other classess I get the
message that says "Model.java uses unchecked or unsafe operations". It
seems to me that program runs fine, but I want to know why this happens
and, if possible, fix it. Thank you.
- Jonie
--------------------------------------------------------------------------
import java.util.*;
import java.util.Scanner;
import java.awt.Point;
import uchicago.src.sim.space.*;
import uchicago.src.sim.gui.*;
public class Model {
private int numberOfAnts = 8;
private int numberOfAntEaters = 2;
private ArrayList <Ant> antList;
private ArrayList <AntEater> antEaterList;
private ArrayList allList;
private int worldSizeX = 10, worldSizeY = 10;
private Object2DGrid world;
private Object2DDisplay worldDisplay;
private DisplaySurface dsurf;
private java.util.Random rng;
public void initializeModel () {
System.out.printf( "-> Entering initalizeModel...\n" );
rng = new java.util.Random();
Animal.setRNG( rng );
Animal.setModel( this );
// create a world for the Ants to live in, and a list to keep track of
them
world = new Object2DGrid( worldSizeX, worldSizeY );
antList = new ArrayList <Ant> ();
antEaterList = new ArrayList <AntEater> ();
allList = new ArrayList ();
// lets create a bunch of ants, adding them to the antList
// and placing them randomly in the world ( <= 1 per cell ).
System.out.printf( "\nCreate %d ants...", numberOfAnts );
for ( int i = 0; i < numberOfAnts; ++i ) {
Ant anAnt = new Ant( );
int x, y;
do { // keep sampling spots until we find an empty one
x = rng.nextInt( worldSizeX );
y = rng.nextInt( worldSizeY );
} while ( world.getObjectAt( x, y ) != null );
anAnt.setX( x );
anAnt.setY( y );
world.putObjectAt( x, y, anAnt );
antList.add( anAnt );
allList.add( anAnt );
}
// lets create a bunch of antEaters, adding them to the antEaterList
// and placing them randomly in the world ( <= 1 per cell ).
System.out.printf( "\nCreate %d ants...", numberOfAnts );
for ( int i = 0; i < numberOfAntEaters; ++i ) {
AntEater anAntEater = new AntEater( );
int x, y;
do { // keep sampling spots until we find an empty one
x = rng.nextInt( worldSizeX );
y = rng.nextInt( worldSizeY );
} while ( world.getObjectAt( x, y ) != null );
anAntEater.setX( x );
anAntEater.setY( y );
world.putObjectAt( x, y, anAntEater );
antEaterList.add( anAntEater );
allList.add( anAntEater );
}
// Lets create the objects to display the ants on the screen
dsurf = new DisplaySurface( null, "Ant Display" );
worldDisplay = new Object2DDisplay( world );
worldDisplay.reSize( 200, 200 );
dsurf.addDisplayable( worldDisplay, "Ants");
dsurf.display();
System.out.printf( "The initial ants:\n" );
printAntList( antList );
System.out.printf( "The initial anteaters:\n" );
printAntEaterList( antEaterList );
System.out.printf( "The initial objects in the world:\n" );
printWorld();
System.out.printf( "\n<- Leaving initalizeModel.\n" );
}
// run
// repeatedly ask the user for a number of "time steps" to run the
model.
// (prompt in the terminal window where the program was started).
// If 0, stop the program.
// Otherwise execute the Model step() message the requested number
of steps,
// and ask again.
// NB: no real checking on the input (eg is it an int, etc).
//
public void run ( ) {
//... Initialize Scanner to read from console.
Scanner input = new Scanner(System.in);
int steps = 0;
do {
System.out.printf( "Enter int number of steps (0->stop): " );
steps = input.nextInt();
for ( int s = 0; s < steps; ++s ) {
step();
}
} while ( steps != 0 );
}
// step
// Execute all activity for one "time step" of the model
// In this simple model, just:
// - tell each Ant to execute its step() method
// - tell the displaySurface to update itself
//
public void step () {
System.out.printf( " -> Take a Model step...\n" );
for ( int i = 0; i < allList.size(); ++i ) {
Object obj = allList.get(i);
if (obj.getClass() == Ant.class )
((Ant) obj).step();
else
((AntEater) obj).step();
}
dsurf.updateDisplay();
System.out.printf( "- antList now:\n" );
printAntList( antList );
System.out.printf( "- antEaterList now:\n" );
printAntEaterList( antEaterList );
printWorld();
System.out.printf( "\n" );
}
// moveObjectInWorld
// try to move object to the requested dx,dy .
// note we must check to be sure the new location is "legal",
// i.e., cell is empty and cell is not off the edge of the world.
// NB: if either new* values is illegal, don't move.
// If move is ok:
// - tell the world the object is not where it was
// - tell the world the object is now at the new* location
// Return: null if not moved, otherwise return a Point object
// with new x,y values.
//
public Point moveObjectInWorld ( Drawable obj, int dX, int dY ) {
int newX = obj.getX() + dX; // get location it wants to move to
int newY = obj.getY() + dY;
// first check to be sure new location is in the world!
if ( newX < 0 || newY < 0 || newX >= worldSizeX || newY >= worldSizeY
) {
System.out.printf( " - obj tried to move offworld (%d,%d).\n",
newX, newY );
return null;
}
// see if new cell is empty.
if ( world.getObjectAt( newX, newY ) != null ) {
System.out.printf( " - obj tried to move to occupied cell
(%d,%d).\n",
newX, newY );
return null;
}
// its ok to move, so tell the world and the object
world.putObjectAt( obj.getX(), obj.getY(), null ); // old cell empty
now
world.putObjectAt( newX, newY, obj ); // obj in new cell now
Point loc = new Point( newX, newY );
return loc;
}
// printAntList
// a method to print the contents of an Ant list
public static void printAntList ( ArrayList <Ant> list ) {
for ( Ant a : list ) {
a.printSelf();
}
}
// printAntEaterList
// a method to print the contents of an Ant list
public static void printAntEaterList ( ArrayList <AntEater> list )
{
for ( AntEater a : list ) {
a.printSelf();
}
}
// printWorld
// print places where objects are in the world, and total
public void printWorld () {
System.out.printf( "-> The world contents:\n" );
int numObjs = 0;
for ( int x = 0; x < worldSizeX; ++x ) {
for ( int y = 0; y < worldSizeY; ++y ) {
if ( world.getObjectAt( x, y ) != null ) {
System.out.printf( " object at %d,%d.\n", x, y );
++numObjs;
}
}
}
System.out.printf( "<- %d objects in the world.\n", numObjs );
}
}