/*
 * MathProblem.java      Authors: Koffman and Wolz
 * Class that can generate and solve math problems.
 */
public class MathProblem {

   // Data fields
   private int left;         // left operand
   private int right;        // right operand
   private char operator;    // *, /, +, or -

   // methods
   // constructor
   // postcondition: stores operator and difficulty level.
   //    Sets left and right based on value of difficulty level. 
   public MathProblem(char op, int diff) {
      operator = op;
      left = (int) (Math.pow(10, diff) * Math.random() + 1);
      right = (int) (Math.pow(10, diff) * Math.random() + 1);
   }

   // postcondition - returns the answer to the math problem
   public int solveProblem() {
     int result;
     switch (operator) {
       case '+' : result = left + right;  break;
       case '-' : result = left - right;  break;
       case '*' : result = left * right;  break;
       case '/' : result = left / right;  break; 
       case '%' : result = left % right;  break;
       default: result = -1;
     }
     return result;
   }

   // postcondition: returns true if the operator is valid
   public boolean validOp() {
     return (operator == '+') || (operator == '-') ||
            (operator == '*') || (operator == '/') ||
            (operator == '%');
   }

   // postcondition: returns the problem as a String
   public String toString() {
     return left + " " + operator + " " + right;
   }

   // postcondition: returns the problem as a question
   public String askProblem() {
     return toString() + " = ?";
   }
}

