import java.util.Scanner; import java.io.IOException; /** A user interface for calculating the area of a Triangle This user interface doesn't "do" the calculation of the area. The knoweldge of how to "do" the calculation is embedded in the Triangle class (i.e. the Model class.) @author Phill Conrad @version 2011.01.01 @see Wikipedia article on YAGNI */ public class TriangleConsoleUI { public static double argToDouble(String arg) { try { return Double.parseDouble(arg); } catch (NumberFormatException nfe) { System.err.println("Sorry: " + arg + " could not be interpreted as a number. Please try again"); System.exit(1); } return 0.0; // unreachable, but needed to satisfy compiler } public static void main(String [] args) { Scanner s = new Scanner(System.in); double base,height; if (args.length == 2) { base = argToDouble(args[0]); height = argToDouble(args[1]); } else { System.out.print("Please enter base of Triangle: "); base = argToDouble(s.nextLine()); System.out.print("Please enter height of Triangle: "); height = argToDouble(s.nextLine()); } Triangle t = new Triangle(base,height); System.out.println("Area = " + t.getArea()); } } // class Triangle