Tower.java


Below is the syntax highlighted version of Tower.java.


/************************************************************
  *  Name:    Donna Gabai
  *  Login:   dgabai 
  *  Precept: P01
  *  Fall 2011 exam 2
  * 
  *  Compilation:  javac Tower.java
  *  Execution:    java Tower
  *           (note: test main() not required part of exam)
  * 
  *  Description: Simulates the range of a cell phone tower
  ***********************************************************/

public class Tower {
    // instance variables
    private final double xc, yc;        // center of cell tower range
    private final double range;         // radius of cell tower range
    
    // constructor
    // Tower centered at (x, y), radius of range r
    public Tower(double x, double y, double r) {
        xc = x;
        yc = y;
        range = r;
    }
    
    // is point in range of this tower?
    public boolean inRange(double x0, double y0) {
        // distance between (x0, y0) and cell tower
        double dx = xc - x0;
        double dy = yc - y0;
        double dist = Math.sqrt(dx*dx + dy*dy);
        
        // is it in range?
        return (dist < range);
    }
    
    // return x coordinate
    public double getX() {
        return xc;
    }
    
    // return y coordinate
    public double getY() {
        return yc;
    }
    
    // return radius of tower range
    public double getR() {
        return range;
    }
    
    // test main.  Not a required part of the exam.
    public static void main(String[] args) {
        // set up cell tower at origin, unit range
        Tower test = new Tower(0.0, 0.0, 1.0);     
        StdOut.println(test.inRange(.707, .707));   // should be true
        StdOut.println(test.inRange(.707, .708));   // should be false
    }
}