COMP 310
Fall 201
6

HW02 Supplied Code

Home  Info  Canvas   Java Resources  Eclipse Resources  Piazza

Download the supplied code here (docs).    Descriptions of all the included code are below.


util.Dispatcher

A wrapper around Java's java.util.Observable to make it easier to use.

package util;
import java.util.*;

/**
 * A simplified Observable class that immediately notifies its Observers when its notifyAll() method is called.   The changed state of the Dispatcher does not need to be separately set.
 */
public class Dispatcher extends Observable {
	/**
	 * Immediately notifies all the Observers held.
	 * @param param An input parameter that is passed on to all the Observers.
	 */
	public void notifyAll(Object param) {
		setChanged();
		notifyObservers(param);
	}
}

 


util.IRandomizer

Top-level interface abstractly defining routines that provide randomized values.

package util;

import java.awt.*;

public interface IRandomizer {
	/**
	 * Generates a random location point subject to the constraint that 0<=X<=rect.width and 0<=Y<=rect.height.
	 * @param rect The bounds for the x and y values of the created Point
	 * @return A Point object whose x and y are subject to the given bounds
	 */
	public Point randomLoc( Rectangle rect);


	/**
	 * Generates a random location point subject to the constraint that 0<=X<=dim.width and 0<=Y<=dim.height.
	 * @param dim The bounds for the x and y values of the created Point
	 * @return A Point object whose x and y are subject to the given bounds
	 */
	public Point randomLoc( Dimension dim);

	/**
	 * Returns a random integer greater than or equal to min and less than or equal to max.
	 * @param min The minimum allowed value
	 * @param max The maximum allowed value
	 * @return an int subject to the given bounds
	 */
	public int randomInt(int min, int max);

	/**
	 * Returns a random double greater than or equal to min and less than max.
	 * @param min The minimum allowed value
	 * @param max The maximum allowed value
	 * @return a double subject to the given bounds
	 */
	public double randomDouble(double min, double max);
	
	/**
	 * Returns a random velocity (as a Point) subject to the constraint that the absolute value of the velocity (speed) 
	 * within the bound (inclusive) defined by rect.   Thus the resultant velocity may be negative.   The given Rectangle
	 * should use all positive values.
	 * @param rect The bounds for the absolute value of the velocity in the x and y directions.  
	 * @return a Point object with x and y subject to the given bounds.
	 */
	public Point randomVel( Rectangle rect);

	/**
	 * Returns a random square Dimension, whose width is maxDim.width/2<=width<=maxDim.width
	 * @param maxDim  The bounds on the side of the created Dimension.
	 * @return A random square Dimension subject to the given bound
	 */
	public Dimension randomDim( Dimension maxDim);

	/**
	 * Generates a randomly located and sized rectangle
	 * @param rect  The bounds for the location of the created rectangle
	 * @param maxDim  The bounds for the dimensions of the create rectangle
	 * @return A Rectangle with location and dimensions subject to the given bounds.
	 */
	public Rectangle randomBounds( Rectangle rect, Dimension maxDim);

	/**
	 * Generates a random color
	 * @return a random Color object
	 */
	public Color randomColor();

	/**
	 * Returns a random choice of one of two objects, x and y, where probX is the probability 
	 * that x will be picked (0<=x<=1).
	 * @param x The first of two choices
	 * @param y The second of two choices
	 * @param probX  The probability of the first choice
	 * @return Either x or y as per the probability of choosing them. 
	 */
	public Object randomChoice(Object x, Object y, double probX);
}

 

 


util.Randomizer

Concrete implementation of IRandomizer defining specific algorithms that provide randomized values.

package util;

import java.awt.*;

/**
 * Utility class that supplies  class routines for generating various random values
 */
public class Randomizer implements IRandomizer {
	public static Randomizer Singleton = new Randomizer();

	private Randomizer() {
	}

	/**
	 * Generates a random location point subject to the constraint that 0<=X<=rect.width and 0<=Y<=rect.height.
	 * @param rect The bounds for the x and y values of the created Point
	 * @return A Point object whose x and y are subject to the given bounds
	 */
	public Point randomLoc( Rectangle rect) {
		return (new Point( randomInt(0, rect.width), randomInt(0, rect.height)));
	}


	/**
	 * Generates a random location point subject to the constraint that 0<=X<=dim.width and 0<=Y<=dim.height.
	 * @param dim The bounds for the x and y values of the created Point
	 * @return A Point object whose x and y are subject to the given bounds
	 */
	public Point randomLoc( Dimension dim) {
		return (new Point( randomInt(0, dim.width), randomInt(0, dim.height)));
	}

	/**
	 * Returns a random integer greater than or equal to min and less than or equal to max.
	 * @param min The minimum allowed value
	 * @param max The maximum allowed value
	 * @return an int subject to the given bounds
	 */
	public int randomInt(int min, int max) {
		return (int)Math.floor((Math.random()*(1+max-min))+min);
	}

	/**
	 * Returns a random double greater than or equal to min and less than max.
	 * @param min The minimum allowed value
	 * @param max The maximum allowed value
	 * @return a double subject to the given bounds
	 */
	public double randomDouble(double min, double max) {
		return (Math.random()*(max-min))+min;
	}

	/**
	 * Returns a random velocity (as a Point) subject to the constraint that the absolute value of the velocity (speed) 
	 * within the bound (inclusive) defined by rect.   Thus the resultant velocity may be negative.   The given Rectangle
	 * should use all positive values.
	 * @param rect The bounds for the absolute value of the velocity in the x and y directions.  
	 * @return a Point object with x and y subject to the given bounds.
	 */
	public Point randomVel( Rectangle rect) {
		return (new Point (randomInt(-rect.width, rect.width), randomInt(-rect.height, rect.height)));
	}

	/**
	 * Returns a random square Dimension, whose width is maxDim.width/2<=width<=maxDim.width
	 * @param maxDim  The bounds on the side of the created Dimension.
	 * @return A random square Dimension subject to the given bound
	 */
	public Dimension randomDim( Dimension maxDim) {
		int x =  randomInt(maxDim.width/2,maxDim.width);
		return new Dimension(x,x);
	}

	/**
	 * Generates a randomly located and sized rectangle
	 * @param rect  The bounds for the location of the created rectangle
	 * @param maxDim  The bounds for the dimensions of the create rectangle
	 * @return A Rectangle with location and dimensions subject to the given bounds.
	 */
	public Rectangle randomBounds( Rectangle rect, Dimension maxDim) {
		return new Rectangle(randomLoc(rect), randomDim(maxDim));
	}

	/**
	 * Generates a random color
	 * @return a random Color object
	 */
	public Color randomColor() {
		return new Color( randomInt(0,255),randomInt(0,255),randomInt(0,255));
	}

	/**
	 * Returns a random choice of one of two objects, x and y, where probX is the probability 
	 * that x will be picked (0<=x<=1).
	 * @param x The first of two choices
	 * @param y The second of two choices
	 * @param probX  The probability of the first choice
	 * @return Either x or y as per the probability of choosing them. 
	 */
	public Object randomChoice(Object x, Object y, double probX) {
		return (Math.random()<probX) ? x: y;

	}
}



util.SineMaker

Utility that provide sinusoidally-varying values.

package util;

/**
 * A utility class that can be used to create smoothly varying sinusoidal
 * numerical data.
 * Note that this is not a singleton class nor does it have static methods as
 * its behavior depends on the minimum, maximum and delta values given to 
 * its constructor.
 */
public class SineMaker {
	/**
	 * The midpoint of the min and max values.
	 */
	private double _mid;    
	/**
	 * The amplitude of the sinusoidal output
	 */
	private double _ampl;  
	/**
	 * The amount that _theta is incremented each time, in radians.
	 */
	private double _delta; 
	/**
	 * The current angle used to generate the current
	 * sinusoidal value.  Initialized to produce the minimum value.
	 */
	private double _theta = -Math.PI/2.0;  

	/**
	 * The constructor takes several values to control the object's behavior.
	 * @param min The minimum and initial value to produce.
	 * @param max The maximum value to produce.
	 * @param delta The amount in radians that the generating angle is incremented each time
	 * */
	public SineMaker(double min, double max, double delta) {
		_mid = (max+min)/2.0;
		_ampl = (max-min)/2.0;
		_delta = delta;
	}


	/**
	 * Returns a different value each time it is called.  The value varies smoothly in a 
	 * sinusoidal fashion, incrementing each time as per the above specified delta 
	 * angle increase.
	 * @return the next value as a double
	 */
	public double getDblVal() {
		double result = _mid + _ampl*Math.sin(_theta);
		_theta += _delta; // shorthand for _theta = _theta + _delta
		return result;
	}

	/**
	 * Same as getDblVal, but returns the result rounded to the nearest integer.
	 * Note that getDblVal and getIntVal are not independent as getIntVal merely calls
	 * getDblVal.
	 * @return the next value as an int
	 */
	public int getIntVal() {
		return (int) Math.round(getDblVal());
	}
}

 

 

 


© 2016 by Stephen Wong