   import java.util.*;

/******************************************************
 * Distance.java - Computes the distance between 2 points
 *
 * @author Lewis/Loftus and Nancy Harris
 * @version V4 - A more elegant solution
 *****************************************************/
    public class DistanceV4
   {
   /**************************************************
    * The main method computes the distance between 4 points
    *
    * @param args Command line arguments, unused
    **************************************************/
       public static void main (String[] args)
      {
         double x1; // coordinates of a point
         double y1; // ""
      
         double x2; // coordinates of a point
         double y2; // ""
      
         double x3; // coordinates of a point
         double y3; // ""
      
         double x4; // coordinates of a point
         double y4; // ""
      
         Scanner scan;
      
      // Read in the two points
         scan = new Scanner(System.in);
			
         System.out.print ("Enter the x and y coordinates of the first point (as two doubles): ");
         x1 = scan.nextDouble();
         y1 = scan.nextDouble();
      
         System.out.print ("Enter the x and y coordinates of the second point (as two doubles): ");
         x2 = scan.nextDouble();
         y2 = scan.nextDouble();
      
         scan = new Scanner(System.in);
         System.out.print ("Enter the x and y coordinates of the third point (as two doubles): ");
         x3 = scan.nextDouble();
         y3 = scan.nextDouble();
      
         System.out.print ("Enter the x and y coordinates of the fourth point (as two doubles): ");
         x4 = scan.nextDouble();
         y4 = scan.nextDouble();
      
         printDistance(x1, y1, x2, y2);      
         printDistance(x2, y2, x3, y3);
         printDistance(x3, y3, x4, y4);
         printDistance(x4, y4, x1, y1);
			
      } // end main
   
   /** calcDistance method will calculate the distance between any two points
    *
    *	@param px1 first point x coordinate
    * @param py1 first point y coordinate
    * @param px2 second point x coordinate
    * @param py2 second point y coordinate
    *
    * @return The distance between the two points
    *************************************************************************/
       public static double calcDistance(double px1, double py1, double px2, double py2)
      {
         double resultDistance;
			double xSqr;
			double ySqr;
      
      	// Compute the distance between 4 and 1
         xSqr = (px1 - px2) * (px1 - px2);
         ySqr = Math.pow(py1 - py2, 2);
      
         resultDistance = Math.sqrt(xSqr + ySqr);
      
         return resultDistance;
      } // end calcDistance
   
   /** printDistance method will print the distance between any two points
    *
    *	@param px1 first point x coordinate
    * @param py1 first point y coordinate
    * @param px2 second point x coordinate
    * @param py2 second point y coordinate
    *
    *************************************************************************/
       public static void printDistance(double px1, double py1, double px2, double py2)
      {
			double distance;
			
			distance = calcDistance(px1, py1, px2, py2);
			
         // Print out the answer to 4 decimal places
         System.out.printf("The distance between points (%f, %f) and (%f, %f) is %.4f.\n", 
            px1, py1, px2, py2, distance);	
				
      } // end printDistance
   
   } // end Distance
