Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help making my calculator, it say error in intitalize package calculator; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import

need help making my calculator, it say error in intitalize

package calculator;

import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField;

public class FXMLDocumentController implements Initializable{ @FXML private TextField displayTextField; @Override public void initialize(URL url,ResourceBundle rb) { //The following will force the field to be numeric only //Limits imposed are an optional minus sign, up to 7 significant //digits, an optional decimal point follwed by up to 4 digits. displayTextField.textProperty().addListener(( ObservableValue observable, String oldValue, String newValue)->{ if (!newValue.matches("(\\-{0,1})\\d{0,7}([\\.]\\d{0,4})?")) displayTextField.setText(oldValue)} }); }

@FXML private void seven(ActionEvent event){ displayTextField.setText(displayTextField.getText()+"7"); }

@FXML private void eight(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"8"); }

@FXML private void nine(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"9"); }

@FXML private void multiply(ActionEvent event) { double first = Double.parseDouble(firstOperand); String secondOperand = diplayTextField.getText(); double second = 0;

if (operandValid(secondOperand)) { second = Double.parseDouble(secondOperand); } double result = first * second; firstOperand = "" + result; displayTextField.setText(firstOperand); }

@FXML private void four(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"4"); }

@FXML private void five(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"5"); }

@FXML private void six(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"6"); }

@FXML private void divide(ActionEvent event) { double first = Double.parseDouble(firstOperand); String secondOperand= displayTextField.getText(); double seond = 0; if (second !=0){ double result = first / second; result = Math.round(result *4.0)/4.0 ; firstOperand = "" + result; displayTextField.setTExt(""+ firstOperand); } else { firstOperand = "0"; displayTextField.setText(""); }

}

@FXML private void one(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"1"); }

@FXML private void two(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"2"); }

@FXML private void three(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"3"); }

@FXML private void subtract(ActionEvent event) { double first = Double.parseDouble(firstOperand); String secondOperand = displayTextField.getText(); double second = 0; if (operandValid(secondOperand)) { second = Double.parseDouble(secondOperand); } double result = first - second; firstOperand = "" + result; displayTextField.setText(firstOperand); }

@FXML private void zero(ActionEvent event) { displayTextField.setText(displayTextField.getText()+"0"); }

@FXML private void point(ActionEvent event) { } private String firstOperand= "0"; @FXML private void enter(ActionEvent event) { firstOperand= displayTextField.getText(); displayTextField.setText(""); }

private boolean operandValid(String operand){ if (operand.contains("0") || operand.contains("1") || operand.contains("2") || operand.contains("3") || operand.contains("4") || operand.contains("5") || operand.contains("6") || operand.contains("7") || operand.contains("8") || operand.contains("9")) return true; return false; } @FXML private void add(ActionEvent event) { double first = Double.parseDouble(firstOperand); String secondOperand = displayTextField.getText(); double second = 0; if (operandValid(secondOperand)) { second = Double.parseDouble(secondOperand); } double result = first + second; firstOperand = "" + result; displayTextField.setText(firstOperand); }

@FXML private void changeSign(ActionEvent event) { String display = displayTextField.getText(); if (display.startsWith("-")) display = display.substring(1); else display = "-"+ display; displayTextField.setText(display); }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases In Action

Authors: Dave Bechberger, Josh Perryman

1st Edition

1617296376, 978-1617296376

More Books

Students also viewed these Databases questions

Question

=+a. Suppose that in 2009 the price of beans was

Answered: 1 week ago

Question

What were your most important educational experiences?

Answered: 1 week ago

Question

Which personal relationships influenced you the most?

Answered: 1 week ago