Computer Science 210
Data Structures

Fall 2019, Siena College

Distance BlueJ Project

Click here to download a BlueJ project for Distance.


Distance Source Code

The Java source code for Distance is below. Click on a file name to download it.


Distance.java

/**
 * Example Distance -- includes a method that returns the distance
 * between two points in the plane
 *
 * @author Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 * Updated for Siena College, CSIS 210, Fall 2016, Fall 2019
 * @version Fall 2019
 */

import java.text.DecimalFormat;
import java.util.Random;

public class Distance {

    /**
       compute the distance between two points -- takes 4 parameters
       first two are (x,y) of one point, last two are (x,y) of the other

       @param x1 x coordinate of the first point
       @param y1 y coordinate of the second  point
       @param x2 x coordinate of the first point
       @param y2 y coordinate of the second point
       @return the distance between the two points
    */
    public static double distance(double x1, double y1, double x2, double y2) {

        double xDiff = x1 - x2;
        double yDiff = y1 - y2;

        return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
    }

    /**
       a main method to test the distance method.

       @param args not used
    */
    public static void main(String[] args) {

        // compute the distance between the origin and (1,0)
        double dist1 = distance(0,0,1,0);
        System.out.println("(0,0) to (1,0) is " + dist1);

        // compute the distance between the origin and (1,1)
        double dist2 = distance(0,0,1,1);
        System.out.println("(0,0) to (1,1) is " + dist2);

        // we can print the result immediately without storing it in a
        // variable
        System.out.println("(3,4) to (-2,9.7) is " + distance(3,4,-2,9.7));

        // setup for random number generation and result formatting
        Random randGen = new Random();
        DecimalFormat df = new DecimalFormat("0.000");
        
        // now we'll compute some random distances between points in
        // (0,0) to (1,1)
        for (int count = 0; count < 10; count++) {
            // it's important that we generate and save random numbers
            // in variables for our points, because we want to make
            // sure we use the SAME random numbers for both the
            // distance computation and the printout of the points!
            double x1 = randGen.nextDouble();
            double y1 = randGen.nextDouble();
            double x2 = randGen.nextDouble();
            double y2 = randGen.nextDouble();
            System.out.println("(" + df.format(x1) + "," + df.format(y1) + 
                               ") to (" + df.format(x2) + "," + 
			       df.format(y2) + ") is " +
                               df.format(distance(x1,y1,x2,y2)));
        }

    }
}