import java.util.StringTokenizer; import javax.swing.JOptionPane; import java.io.*; // for I/O public class TriAreaCalculator { //This function does NOT get called anymore. You could delete it, like //I deleted the Header() function. public static double ComputeS(double a, double b, double c) { double s = (a + b + c)/2; return s; } //This function does NOT get called anymore. You could delete it, like //I deleted the Header() function. public static double ComputeArea(double a, double b, double c, double s) { double area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); return area; } public static void main (String[] args) throws IOException { double a, b, c, s, area; StringTokenizer tokenizer; //In a dialog box, ask the user to enter numbers. String input = JOptionPane.showInputDialog("Have you ever wanted to compute the area " + "of a triangle given \nonly the length of the " + "three sides? Well, give me the 3 lengths and\n" + "I'll compute it for you (3 numbers separated " + "by spaces and then return): \n"); //tokenize the user input. tokenizer = new StringTokenizer(input); //parse the user input. a = Double.parseDouble(tokenizer.nextToken()); b = Double.parseDouble(tokenizer.nextToken()); c = Double.parseDouble(tokenizer.nextToken()); //This is where we might check and see whether the three numbers can be the //sides of a triangle. IF they cannot be, then we should inform the user, ELSE //we should perform the computation. //These two method calls can simply be replaced by the code from those methods. //s = ComputeS(a,b,c); //area = ComputeArea(a,b,c,s); s = (a + b + c)/2; area = Math.sqrt(s*(s-a)*(s-b)*(s-c)); //we construct the string that we want to output. String message = "A triangle with sides length " + a + ", " + b + ", and " + c + "\nhas an area of " + area + ".\n"; //show the message string in a messageDialog box. JOptionPane.showMessageDialog (null, message, "Your Age", JOptionPane.PLAIN_MESSAGE); System.exit(0); } }