title: JavaFX Calculator - How do I add a square and square root function?
body: <p>I am making a JavaFX Calculator. I am trying to develop a square and square root function, but I'm puzzled what to write in Controller.java for the method. I'm not sure if they're even working right, if I click them they return nothing. I think, "okay, is the value stored?" I attempt to add 0 to it to see if there's a result, and errors.</p>
  
  <p>App.java:</p>
  
  <pre><code>package com.honors.calc;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  
  public class App extends Application {
  
      @Override
      public void start(Stage primaryStage) throws Exception {
          Parent root = FXMLLoader.load(getClass().getResource("ui.fxml"));
  
          primaryStage.setScene(new Scene(root));
          primaryStage.show();
  
      }
  
      public static void main(String[] args) {
          launch(args);
  
      }
  
  }
  </code></pre>
  
  <p>Model.java:</p>
  
  <pre><code>package com.honors.calc;
  
  public class Model {
  
      public long calculate(long number1, long number2, String operator) {
          switch (operator) {
              case "+":
                  return number1 + number2;
              case "-":
                  return number1 - number2;
              case "*":
                  return number1 * number2;
              case "/":
                  if (number2 == 0)
                      return 0;
  
                  return number1 / number2;
              case "C":
                  number1 = 0;
                  number2 = 0;
                  return 0;
              case "CE":
                  number2 = 0;
                  return 0;
              case "x²":
                  return number1 * number1;
              case "√":
                  return (long) Math.sqrt(number1);
          }
          System.out.println("Unknown operator - " + operator);
          return 0;
      }
  }
  </code></pre>
  
  <p>Controller.java:</p>
  
  <pre><code>package com.honors.calc;
  
  import javafx.event.ActionEvent;
  import javafx.fxml.FXML;
  import javafx.scene.control.Button;
  import javafx.scene.text.Text;
  
  public class Controller {
      @FXML
      private Text output;
  
      private long number1 = 0;
      private String operator = "";
      private boolean start = true;
  
      private Model model = new Model();
  
      @FXML
      private void processNumpad(ActionEvent event) {
          if (start) {
              // clear screen
              output.setText("");
              start = false;
          }
          String value = ((Button)event.getSource()).getText();
          output.setText(output.getText() + value);
      }
  
      @FXML
      private void processOperator(ActionEvent event) {
          String value = ((Button)event.getSource()).getText();
  
          if(!"=".equals(value)) {
              if (!operator.isEmpty())
                  return;
  
              operator = value;
              number1 = Long.parseLong(output.getText());
              output.setText("");
          } else {
              // calculation
              if (operator.isEmpty())
                  return;
  
              output.setText(String.valueOf(model.calculate(number1, Long.parseLong(output.getText()), operator)));
              operator = "";
              // reset
              start = true;
          }
      }
  
      @FXML
      private void processClear(ActionEvent event) {
          output.setText("0");
      }
  
      @FXML
      private void processClearEntry(ActionEvent event) {
          output.setText("");
      }
  
      @FXML
      private void processSquare(ActionEvent event) {
  
  
      }
  
      @FXML
      private void processSquareRoot(ActionEvent event) {
  
      }
  }
  </code></pre>
  
  <p>ui.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import java.net.*?&gt;
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.beans.property.*?&gt;
  &lt;?import javafx.geometry.*?&gt;
  &lt;?import javafx.scene.shape.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.image.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.text.*?&gt;
  &lt;?import javafx.scene.paint.Color?&gt;
  
  &lt;VBox spacing="10" alignment="CENTER" prefWidth="300" prefHeight="320" fx:controller="com.honors.calc.Controller" xmlns:fx="http://javafx.com/fxml"&gt;
      &lt;fx:define&gt;
          &lt;Font fx:id="FONT" size="18" /&gt;
      &lt;/fx:define&gt;
  
      &lt;StackPane alignment="CENTER"&gt;
          &lt;Rectangle fill="TRANSPARENT" stroke="GRAY" width="230" height="50" /&gt;
          &lt;Text fx:id="output" font="$FONT" /&gt;
      &lt;/StackPane&gt;
  
      &lt;HBox spacing="10" alignment="CENTER"&gt;
          &lt;Button text="C" prefWidth="50" font="$FONT" onAction="#processClear" /&gt;
          &lt;Button text="CE" prefWidth="50" font="$FONT" onAction="#processClearEntry" /&gt;
          &lt;Button text="x²" prefWidth="50" font="$FONT" onAction="#processSquare" /&gt;
          &lt;Button text="√" prefWidth="50" font="$FONT" onAction="#processSquareRoot" /&gt;
      &lt;/HBox&gt;
  
      &lt;HBox spacing="10" alignment="CENTER"&gt;
          &lt;Button text="7" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="8" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="9" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="/" prefWidth="50" font="$FONT" onAction="#processOperator" /&gt;
      &lt;/HBox&gt;
  
      &lt;HBox spacing="10" alignment="CENTER"&gt;
          &lt;Button text="4" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="5" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="6" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="*" prefWidth="50" font="$FONT" onAction="#processOperator" /&gt;
      &lt;/HBox&gt;
  
      &lt;HBox spacing="10" alignment="CENTER"&gt;
          &lt;Button text="1" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="2" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="3" prefWidth="50" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="-" prefWidth="50" font="$FONT" onAction="#processOperator" /&gt;
      &lt;/HBox&gt;
  
      &lt;HBox spacing="10" alignment="CENTER"&gt;
          &lt;Button text="0" prefWidth="110" font="$FONT" onAction="#processNumpad" /&gt;
          &lt;Button text="=" prefWidth="50" font="$FONT" onAction="#processOperator" /&gt;
          &lt;Button text="+" prefWidth="50" font="$FONT" onAction="#processOperator" /&gt;
      &lt;/HBox&gt;
  
  &lt;/VBox&gt;
  </code></pre>