Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

is this code working import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.Priority; import

is this code working
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
public class JavaFXCalculator extends Application {
private TextField tfDisplay;
private Button[] btns;
private Button btnDot;
private Button btnPercent;
private Button btnSqrt;
private Button btnBackspace;
private String[] btnLabels ={
"7","8","9","+",
"4","5","6","-",
"1","2","3","x",
"C","0","=","/"
};
private int result =0;
private String inStr ="0";
private char lastOperator ='';
EventHandler handler = evt ->{
String currentBtnLabel =((Button) evt.getSource()).getText();
switch (currentBtnLabel){
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
if (inStr.equals("0")){
inStr = currentBtnLabel;
} else {
inStr += currentBtnLabel;
}
tfDisplay.setText(inStr);
if (lastOperator =='='){
result =0;
lastOperator ='';
}
break;
case "+":
compute();
lastOperator ='+';
break;
case "-":
compute();
lastOperator ='-';
break;
case "x":
compute();
lastOperator ='*';
break;
case "/":
compute();
lastOperator ='/';
break;
case "=":
compute();
lastOperator ='=';
break;
case "C":
result =0;
inStr ="0";
lastOperator ='';
tfDisplay.setText("0");
break;
case ".":
if (!inStr.contains(".")){
inStr += currentBtnLabel;
tfDisplay.setText(inStr);
}
break;
case "%":
double percentage = Double.parseDouble(inStr)/100.0;
inStr = Double.toString(percentage);
tfDisplay.setText(inStr);
break;
case "":
double sqrtResult = Math.sqrt(Double.parseDouble(inStr));
inStr = Double.toString(sqrtResult);
tfDisplay.setText(inStr);
break;
case "":
if (inStr.length()>1){
inStr = inStr.substring(0, inStr.length()-1);
} else {
inStr ="0";
}
tfDisplay.setText(inStr);
break;
}
};
private void compute(){
int inNum = Integer.parseInt(inStr);
inStr ="0";
if (lastOperator ==''){
result = inNum;
} else if (lastOperator =='+'){
result += inNum;
} else if (lastOperator =='-'){
result -= inNum;
} else if (lastOperator =='*'){
result *= inNum;
} else if (lastOperator =='/'){
result /= inNum;
} else if (lastOperator =='='){
// Keep the result for the next operation
}
tfDisplay.setText(result +"");
}
@Override
public void start(Stage primaryStage){
tfDisplay = new TextField("0");
tfDisplay.setEditable(false);
tfDisplay.setAlignment(Pos.CENTER_RIGHT);
int numCols =4;
int numRows =5;
GridPane paneButton = new GridPane();
paneButton.setPadding(new Insets(15,0,15,0));
paneButton.setVgap(5);
paneButton.setHgap(5);
ColumnConstraints[] columns = new ColumnConstraints[numCols];
for (int i =0; i < numCols; ++i){
columns[i]= new ColumnConstraints();
columns[i].setHgrow(Priority.ALWAYS);
columns[i].setFillWidth(true);
paneButton.getColumnConstraints().add(columns[i]);
}
btns = new Button[16];
for (int i =0; i < btns.length; ++i){
btns[i]= new Button(btnLabels[i]);
btns[i].setOnAction(handler);
b

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

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions