Class Universe

java.lang.Object
  extended by Universe

public class Universe
extends java.lang.Object

This class creates a universe of locations


Field Summary
private  double density
          The density of the universe.
private  Location[][] universe
          The 2d array of locations in the universe, indexed by row-col coordinates.
 
Constructor Summary
Universe()
          The default constructor, it initializes the Universe to default values.
Universe(int height, int width, double density)
          The specific constructor, in initializes the fields to the passed values.
 
Method Summary
 Location findLocationByName(java.lang.String dest)
          Finds and returns the location specified by the location name passed
 double getDensity()
          Get the Density value.
 Location getLocationAt(int row, int col)
          Gets the location at the position specified by the int values passed
 int getNumCols()
          Returns the width of the Universe
 int getNumRows()
          Returns the height of the Universe
 Location getRandomLocation()
          Randomly chooses a location to return
 Location[][] getUniverse()
          Get the Universe value.
 void populate()
          Creates different and new locations, and place at positions in the universe depending on the universe density
 void setDensity(double newDensity)
          Set the Density value.
 void setLocationAt(int row, int col, Location loc)
          Sets the location at the specified coordinates to the passed value
 void setUniverse(Location[][] newUniverse)
          Set the Universe value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

universe

private Location[][] universe
The 2d array of locations in the universe, indexed by row-col coordinates.


density

private double density
The density of the universe. Roughly, this is the probability that a Location exists at a particular X-Y coordinate of the Universe. Note that the semantics of this field is that if a given Universe has a density of "0.1", then it is NOT necessary that precisely 10% of the coordinates contain an actual instance of a Location. This is like saying that if I flip a coin 100,000 times, then "heads" will come up approximately 50,000 times. But if I flip a coin only 4 times, then it's possible that "heads" would happen twice, although I shouldn't be surprise if I get three "heads".

Constructor Detail

Universe

public Universe(int height,
                int width,
                double density)
The specific constructor, in initializes the fields to the passed values. This method also initializes the universe array.

Parameters:
height - The height of the universe
width - The width of the universe
density - the density of the universe

Universe

public Universe()
The default constructor, it initializes the Universe to default values. The default values are 10 rows, 10 columns, and a density of 0.1. This method also initializes the universe array.

Method Detail

getDensity

public double getDensity()
Get the Density value.

Returns:
the Density value.

setDensity

public void setDensity(double newDensity)
Set the Density value.

Parameters:
newDensity - The new Density value.

getUniverse

public Location[][] getUniverse()
Get the Universe value.

Returns:
the Universe value.

setUniverse

public void setUniverse(Location[][] newUniverse)
Set the Universe value.

Parameters:
newUniverse - The new Universe value.

getNumCols

public int getNumCols()
Returns the width of the Universe

Returns:
The width of the Universe

getNumRows

public int getNumRows()
Returns the height of the Universe

Returns:
The height of the Universe

setLocationAt

public void setLocationAt(int row,
                          int col,
                          Location loc)
Sets the location at the specified coordinates to the passed value

Parameters:
row - The row of the location in the universe
col - The column of the location in the universe
loc - The location to set

populate

public void populate()
Creates different and new locations, and place at positions in the universe depending on the universe density


getLocationAt

public Location getLocationAt(int row,
                              int col)
Gets the location at the position specified by the int values passed

Parameters:
row - The row of the location in the universe
col - The column of the location in the universe
Returns:
the location at the position (i,j)

getRandomLocation

public Location getRandomLocation()
Randomly chooses a location to return

Returns:
a randomly chosen location

findLocationByName

public Location findLocationByName(java.lang.String dest)
Finds and returns the location specified by the location name passed

Parameters:
dest - the name of the location to find and return
Returns:
the location with name dest, or null if there is no location with the name dest