Perimeter.java


Below is the syntax highlighted version of Perimeter.java.


/* read integers xA, yA, xB, yB from stdin
 * print perimeter of triangle with corners
 * O=(0, 0), A=(xA, yA), B=(xB, yB) 
 *
 * E.g.: if run with input 0 3 4 0, outputs 12.0 
 *        visualization: http://goo.gl/bSZoVH     */
public class Perimeter {
   
    // compute square
    public static int sqr(int x) {
        return x * x;
    }
   
    // compute distance of (x, y) from origin
    public static double hypot(int x, int y) {
        double sum = sqr(x);
        sum += sqr(y);
        return Math.sqrt(sum);
    }
   
    // first method to execute
    public static void main(String[] args) {
        int xA = StdIn.readInt();
        int yA = StdIn.readInt();
        int xB = StdIn.readInt();
        int yB = StdIn.readInt();
        double perim = 0;
        perim += hypot(xA-xB, yA-yB); // length of side AB
        perim += hypot(xA, yA); // length of side OA
        perim += hypot(xB, yB); // length of side OB
        StdOut.println(perim);
    }
}