title: How can I do one-operand math with my Java calculator?
body: <p>I have been trying to create a simple calculator in Java for a while now, and I have successfully been able to make the program work with two-operand equations (+, -, *, /, and ^). However, I was wondering how I would be able to do one-operand math problems - absolute value(using the symbol "|"), square root (using the symbol 'v'), rounding to the closest integer (using the symbol '~'), sin (s), cos (c), and tangent (t).</p>
  
  <p>I have attempted the absolute value operand which can be seen in:</p>
  
  <pre><code>if (operator == '|') {
              answer = Math.abs(numA);
  }
  // In the main class
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>double absolute(double a) {
              double answer = Math.abs(a);
              return answer;
  }
  // In the maths class
  </code></pre>
  
  <p>This code only works if you enter in values for example like this: <code>-3 | -3</code> (Note: I have noticed that it is only the first number that the absolute value operation is performed upon. The second number can be whatever you want (if you entered <code>-3 | -4</code> your answer would still be <code>3</code>) as long as it is, indeed, a number.</p>
  
  <p>Any help for solving this problem and help figuring out the other single-operand operations would be greatly appreciated!</p>
  
  <p>Thanks in advance!</p>
  
  <p>The source code for my program is below:</p>
  
  <pre><code>package calculator;
  import java.util.Scanner;
  
  public class Calculator {
  
  public static void main(String[] args) {
  
      System.out.println("Hello, welcome to my calculator");
      System.out.println("Enter in some stuff you want to me to calculate");
  
      Scanner scan = new Scanner(System.in);
      System.out.println("If you need help please type \"help\"");
      System.out.println("If at anytime you want to leave, type \"quit\"");
      System.out.println("Hit enter to continue.");
  
      String s1 = scan.nextLine();
  
      if (s1.equals("help")){
          System.out.println(" ");
  
          System.out.println("Double operand commands:");
          System.out.println("Addition: '+' (Ex: 'a + b' )");
          System.out.println("Subtraction: '-' (Ex: 'a - b' )");
          System.out.println("Multiplication: '*' (Ex: 'a * b' ) ");
          System.out.println("Division: '/' (Ex: 'a / b' )");
          System.out.println("Exponents: '^' (Ex: 'a ^ b' )");
  
          System.out.println(" ");
      }
  
      Scanner input = new Scanner(System.in);
  
      Maths maths = new Maths();
  
      double answer = 0;
      double numA, numB;
      char operator;
      boolean quit = false;
  
  
      while (true) {
  
      System.out.print("Please enter your equation: ");
  
      String s=input.next();
  
      if(s.equals("quit")){
          System.out.println("Thank you for using my program!");
          System.exit(0);
      }
  
      numA = Double.parseDouble(s);
      operator = input.next().charAt(0);
      numB = input.nextDouble();        
  
      if (operator == '+') {
          answer = maths.add(numA, numB);
      }
  
      if (operator == '-') {
          answer = maths.subtract(numA, numB);
      }
  
      if (operator == '*') {
          answer = maths.multiply(numA, numB);
      }
  
      if (operator == '/') {
          answer = maths.divide(numA, numB);
      }
  
      if (operator == '^') {
          answer = maths.power(numA, numB);
      }
  
      if (operator == '|') {
          answer = Math.abs(numA);
      }
  
          System.out.println(answer);        
  
  
  
          }
  
      }
  
  }
  
  class Maths {
  
      double add(double a, double b) {
          double answer = a+b;
          return answer;          
      }
  
      double subtract(double a, double b) {
          double answer = a-b;
          return answer;          
      }
  
      double multiply(double a, double b) {
          double answer = a*b;
          return answer;          
      }
  
      double divide(double a, double b) {
          double answer = a/b;
          return answer;          
      }
  
      double power(double a, double b){
          double answer =a;
  
          for (int x=2; x&lt;=b; x++){
              answer *= a;
          }
  
          return answer;
      }
  
      double absolute(double a) {
          double answer = Math.abs(a);
          return answer;
      }
  
  
  }
  </code></pre>