   import java.util.Scanner;
/**============================================================== 
 *  This program reads in a postfix expression consisting of two
 * one character operands followed by either the `+' or `-'
 * operator.  The operands are hexadecimal digits (lower case
 * only).  The program outputs the expression as an infix expression
 * with base 16 operands and its result as a base 10 integer.
 *
 * @author  Elizabeth Adams (modified code by A. Harris and N. Harris)
 * @version  3
 * 
//==============================================================  */
// Date: September 26, 2008 
// Section 12
// Lab 1

    public class Postfixed
   {
	    /** ========================================================
		  * 
		  *  main method - in general will only call other methods
		  *              - required by every java application
		  *
		  *  @param args   command line arguments not used in this application
		  */
		  
       public static void main (String args[])
      {
         char  operand1, operand2; // operands entered as characters
         char  operator;
         int   operand1Value, operand2Value; // operands in numeric form
         int   result;  
         Scanner keyboard;
         String line;  // will hold input
      
         keyboard = new Scanner(System.in);
      
             // prompt to user telling them what to do
         System.out.print("Enter 3-character postfix expression: ");
      
      // reads in data as a single line
         line = keyboard.nextLine();
			System.out.println (" Here is what you entered " + line);
         System.out.println();
      
      // separate out the three characters and print each with a label
         operand1 = line.charAt(0);
         System.out.println (" operand1 is " + operand1); // echo input
         operand2 = line.charAt(1);
         System.out.println (" operand2 is " + operand2);  // echo input
         operator = line.charAt(2);
         System.out.println (" operator is " + operator); // echo input
      
      // decode first operand (convert it to a number IF POSSIBLE 
         if (operand1 >= 'a' && operand1 <= 'f') // if operand1 between 'a' and 'f'
         { 
             // convert to number: hex a is 10 so add 10 & subtract ASCII 'a' value
            operand1Value = operand1 + 10 - 'a';
            System.out.println ("operand1Value is " + operand1Value); // show numeric value
         } 
         else if (operand1 >= '0' && operand1 <= '9') 
         {  
            // convert to number by subtracting ASCII '0' value
            operand1Value = operand1 - '0';
            System.out.println ("operand1Value is " + operand1Value); // show numeric value
         }
         else
         {
            operand1Value = 0;  // prevents message saying operand1Value may not have value 
            System.out.println ("operand1Value is " + operand1Value);
            System.out.println("Program Aborted: operand1 " + operand1 + 
               " not `0'-`9' or `a'-`f'.");   // operand1 wasn't in valid range
            System.exit(1);  // 1 indicates WHERE error occurred
         }
      
      // now decode 2nd operand (convert it to a number)	 
         if (operand2 >= 'a' && operand2 <= 'f')  // see above explanations
         {  
            operand2Value = 10 + operand2 - 'a';
            System.out.println ("operand2Value is " + operand2Value);
         }
         else if (operand2 >= '0' && operand2 <= '9')
         {  
            operand2Value = operand2 - '0';
            System.out.println ("operand2Value is " + operand2Value);
         }
         else
         {
            operand2Value = 0;
            System.out.println ("operand2Value is " + operand2Value);
            System.out.println ("Program Aborted: operand2 "
               + operand2 + " not `0'-`9' or `a'-`f'.");
            System.exit(2);  // shows WHERE error occurred
         }
      
      // check that operator is one of two allowed: ( + or -) and IF SO perform operation 
      // Note we're using operandValues
         if (operator == '+')  
            result = operand1Value + operand2Value; 
         else if (operator == '-')
            result = operand1Value - operand2Value;
         else
         {
            result = 0;
            System.out.println ("Program Aborted: operator " + operator + " not `+' or `-'.");
            System.exit(3);  // indicates WHERE program aborted
         }
      
      // note that we're printing original operands not their numeric values here 
         System.out.println (operand1 + " " + operator + " " + operand2 + " = " + result);
      }
   
   }
