CMSI 185
Homework #5
Partial Answers
  1. The generator:
    /**
     * An arithmetic sequence generator.  An arithmetic sequence has
     * a constant different between terms, for example [3, 7, 11, 15,
     * 19, ...].
     */
    public class ArithmeticSequenceGenerator {
    
        private int start;
        private int delta;
        private int current;
    
        /**
         * Creates a sequence with the given starting value and
         * given difference between successive values.
         */
        public ArithmeticSequenceGenerator(int start, int delta) {
            this.start = start;
            this.delta = delta;
            this.current = start - delta;
        }
    
        /**
         * Returns the next value in the sequence.  The first time this
         * is called, the starting value is returned.
         */
        public int next() {
            return current += delta;
        }
    }
    
  2. The generator demo:
    /**
     * A small demonstration of arithmetic sequences.  This really
     * is a rather useless application; real programmers would have
     * made a unit test instead.
     */
    public class ArithmeticSequenceGeneratorDemo {
    
        /**
         * Creates a sequence generator with the given start and delta values
         * and shows its first five values.
         */
        private static void showInitialSequenceValues(int start, int delta) {
            ArithmeticSequenceGenerator g = new ArithmeticSequenceGenerator(start, delta);
            for (int i = 0; i < 5; i++) {
                System.out.print(g.next() + " ");
            }
            System.out.println();
        }
    
        /**
         * Displays the initial values of three arbitrary sequences.
         */
        public static void main(String[] args) {
            showInitialSequenceValues(-3, 10);
            showInitialSequenceValues(50, -27);
            showInitialSequenceValues(2, 0);
        }
    }
    
  3. The point class:
    /**
     * An (immutable) point on a sphere.  Since is earth is roughly
     * spherical you could use this class for an approximation to a point
     * on the Earth's surface.
     */
    public class SpherePoint {
    
        private double latitude;
        private double longitude;
    
        /**
         * Constructs a point.
         */
        public SpherePoint(double latitude, double longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }
    
        /**
         * Returns the latitude.
         */
        public double getLatitude() {
            return latitude;
        }
    
        /**
         * Returns the longitude.
         */
        public double getLongitude() {
            return longitude;
        }
    
        /**
         * Returns the point opposite this point.  This is found by negating
         * the latitude, and, if the longitude is positive, subtracting 180,
         * but if the latitude is negative, subtracting 180.
         */
        public SpherePoint antipode() {
           return new SpherePoint(-latitude,
                   longitude - Math.signum(longitude) * 180);
        }
    
        /**
         * Returns the angular distance in radians from this point to another
         * point on the sphere.  To get the actual distance, multiply by the
         * radius of your sphere.  Adapted from
         * http://www.movable-type.co.uk/scripts/latlong.html,
         * but not yet tested.
         */
        public double distanceTo(SpherePoint that) {
            double lat1 = Math.toRadians(this.latitude);
            double lon1 = Math.toRadians(this.longitude);
            double lat2 = Math.toRadians(that.latitude);
            double lon2 = Math.toRadians(that.longitude);
            double deltaLat = lat2 - lat1;
            double deltaLon = lon2 - lon1;
            double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
                    Math.cos(lat1) * Math.cos(lat2) *
                    Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
            return 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        }
    
        /**
         * Returns a description of the point in a form similar to
         * "(-22.5N, 60W)".
         */
        public String toString() {
            return "(" + latitude +
                (latitude >= 0 ? 'N' : 'S') +
                ", " + longitude +
                (longitude >= 0 ? 'E' : 'W') +
                ")";
        }
    }