Triangles.java

 1: package com.wpollock.triangles;
 2: 
 3: import java.util.Arrays;
 4: 
 5: /**
 6:  * Triangles is a stand-alone console application that reads three integer
 7:  * values from the console. The three values represent the lengths of the sides
 8:  * of a triangle. The program prints a message that states if the triangle is
 9:  * equilateral (all sides the same length), isosceles (two sides the same
10:  * length), or scalene (all sides have different lengths).
11:  *
12:  * @author wpollock
13:  */
14: public class Triangles {
15: 
16:     /**
17:      * main method.  To facilitate testing, the work is done by a method
18:      * that returns a Sting result.
19:      *
20:      * @param args command line arguments
21:      */
22:     public static void main(String... args) {
23:         try {
24:             System.out.println(classify(args));
25:         } catch (Exception e) {
26:             System.err.println("Arguments must be three integers, "
27:                 + "representing the sides of a valid triange.");
28:         }
29:     }
30: 
31:     /**
32:      * Classify takes three strings representing integers, and determines the
33:      * type of triangle they make.
34:      * This is demo code only, and contains a deliberate (but subtle) error!
35:      *
36:      * @param args An array of three Strings, representing the integer sides
37:      *            of a valid triangle.
38:      * @return A String, one of "Equilateral", "Isosceles", or "Scalene".
39:      * @throws IllegalArgumentException If arguments are not three Strings
40:      *         representing the integer sides of a valid triangle.
41:      */
42:     public static String classify(String... args) {
43:         if (args.length != 3) {
44:             throw new IllegalArgumentException("Must have three arguments");
45:         }
46:         long[] side = new long[3];
47: 
48:         // Integer.parseUnsignedInt is new since Java 8:
49:         side[0] = Integer.parseUnsignedInt(args[0]);
50:         side[1] = Integer.parseUnsignedInt(args[1]);
51:         side[2] = Integer.parseUnsignedInt(args[2]);
52: 
53:         // Check for valid side lengths:
54:         if (side[0] <= 0 || side[1] <= 0 || side[2] <= 0) {
55:             throw new IllegalArgumentException(
56:                 "Arguments must be greater than zero");
57:         }
58: 
59:         // Check for valid triangle.  The sum of the two shorter sides must
60:         // be greater than the longer side (try to spot the logic error):
61:         Arrays.sort(side);
62:         if ( side[0] + side[1] <= side[2] ) {
63:             throw new IllegalArgumentException(
64:                 "No triangle is possible with sides of "
65:                     + Arrays.toString(side) );
66:         }
67: 
68:         // Determine the type (try to spot the minor logic error below):
69:         if (side[0] == side[1] && side[1] == side[2]) {
70:             return "Equilateral";
71:         } else if (side[0] == side[1]
72:             || side[1] == side[2]
73:             || side[0] == side[2]) {
74:             return "Isosceles";
75:         }
76:         return "Scalene";
77:     }
78: }