
public class ThreeNumberCalculator {

   // The program allows the user to enter a command such as "sum"
   // followed by three numbers.  It then applies the command to
   // the three numbers and outputs the result.  This is repeated
   // until the user enters "end".
   // 
   // Currently, "sum" is the only command that is implemented.
   //
   // David Eck, January 25, 1998


   static Console console;  // Console window for input/output


   public static void main(String[] args) {

      console = new Console("Three Number Calculator");

      console.putln("Welcome to a simple calculator program that can apply");
      console.putln("Several basic operations to three input numbers.");
      console.putln("This program understands commands consisting of a word");
      console.putln("followed by three numbers.  For example:  sum 3.5 -6 4.87");
      console.putln("It also understands the command:  end");
      console.putln("(Right now, the only operation that is implement is sum.)");
 
      while (true) {

         console.putln();
         console.put("COMMAND>> ");
         String command = console.getWord();      // command word entered by user

         if (command.equalsIgnoreCase("end"))
            break;

         double firstNum =  console.getDouble();  // three number entered by user
         double secondNum = console.getDouble();
         double thirdNum  = console.getDouble();
         console.getln(); // read the end-of-line

         if (command.equalsIgnoreCase("sum")) {   // do "sum" command
            printSum(firstNum, secondNum, thirdNum);
         }
         else {
            console.putln("Sorry, I can't understand \"" + command + "\".");
         }

      }  // end of while loop

      console.putln();
      console.putln("Bye!");

      console.close();

   }


   static void printSum(double x, double y, double z) {
        // This method computes the sum of its three parameters,
        // x, y, and z.  It outputs the answer to the console.
      double sum;  // The sum of the three parameters.
      sum = x + y + z;
      console.putln("The sum of "      + x
                          +  ", "      + y
                          +  ", and "  + z
                          +  " is "    + sum);
   }  // end of printSum()


}  // end of class ThreeNumberCalculator
