Пример #1
0
 public void actionPerformed(ActionEvent ev) {
   CalculatorAlt cal = new CalculatorAlt();
   cal.addNum_working(" +/- ");
   cal =
       null; // setting object to null makes it more likely to be deleted by the Garbage
             // Collector
   // System.out.println("+/-");
 }
Пример #2
0
 public void actionPerformed(ActionEvent ev) {
   CalculatorAlt cal = new CalculatorAlt();
   setNum_memory(Double.parseDouble(cal.getNum_working()));
   cal =
       null; // setting object to null makes it more likely to be deleted by the Garbage
             // Collector
   // System.out.println("MS");
 }
Пример #3
0
 public void actionPerformed(ActionEvent ev) {
   CalculatorAlt cal = new CalculatorAlt();
   double temp_working = Double.parseDouble(cal.getNum_working());
   double temp_memory = cal.getNum_memory();
   double three = temp_working + temp_memory;
   setNum_memory(three);
   setNum_working(String.valueOf(three));
   cal =
       null; // setting object to null makes it more likely to be deleted by the Garbage
             // Collector
   // System.out.println("MA");
 }
Пример #4
0
 public void actionPerformed(ActionEvent ev) {
   // System.out.println("0");
   CalculatorAlt cal = new CalculatorAlt();
   String num_n = cal.getNum_working();
   if (num_n.length() > 0) {
     cal.addNum_working("0");
   } else {
     cal.addNum_working("");
   }
   cal =
       null; // setting object to null makes it more likely to be deleted by the Garbage
             // Collector
 }
Пример #5
0
    public void actionPerformed(ActionEvent ev) {
      double num_a = 0;
      String num_operator = "";
      double num_b = 0;
      int num_length = 0;

      double num_answer = 0;

      CalculatorAlt cal = new CalculatorAlt();

      // System.out.println("num_working = " + cal.getNum_working());
      // String num_working_last_3 = cal.getNum_working().substring(Math.max(0,
      // cal.getNum_working().length() - 3)); // get last 3 characters in string

      String delims = " ";
      String[] tokens = cal.getNum_working().split(delims);

      for (int i = 0; i < tokens.length; i++) {
        num_length++;
      }

      if (num_length == 2) {
        num_a = Double.parseDouble(tokens[0]);
        num_operator = tokens[1];
      } else {
        num_a = Double.parseDouble(tokens[0]);
        num_operator = tokens[1];
        num_b = Double.parseDouble(tokens[2]);
      }

      System.out.println(num_a);
      System.out.println(num_operator);
      System.out.println(num_b);

      // first we check when "=" is pushed what the num_operator is
      // then compare num_operator and do stuff
      // sqrt
      if (num_operator.equals("+")) {
        num_answer = num_a + num_b;

      } else if (num_operator.equals("-")) {
        num_answer = num_a - num_b;

      } else if (num_operator.equals("*")) {
        num_answer = num_a * num_b;

      } else if (num_operator.equals("/")) {
        num_answer = num_a / num_b;

      } else if (num_operator.equals("sqrt")) {
        if (num_length == 2) {
          num_answer = num_a * num_a;
        } else {
          double num_c = num_a;
          for (int i = 1; i < num_b; i++) {
            num_c = num_c * num_a;
          }
          num_answer = num_c;
        }

      } else if (num_operator.equals("1/x")) {
        num_answer = Math.sqrt(num_a);

      } else if (num_operator.equals("%")) {
        num_answer = (num_b / 100) * num_a;

      } else if (num_operator.equals("+/-")) {
        if (num_a < 0) {
          num_answer = Math.abs(num_a);
        } else {
          num_answer = -num_a;
        }
      }

      // reset the whole lot
      setNum_working("");
      setTextInput(String.valueOf(num_answer));
      num_length = 0;
      // setTextFunction(" = ");
    }