Answered step by step
Verified Expert Solution
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
x
C
;
private int result ;
private String inStr ;
private char lastOperator ;
EventHandler handler evt
String currentBtnLabel Button evt.getSourcegetText;
switch currentBtnLabel
case :
case :
case :
case :
case :
case :
case :
case :
case :
case :
if inStrequals
inStr currentBtnLabel;
else
inStr currentBtnLabel;
tfDisplay.setTextinStr;
if lastOperator
result ;
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 ;
inStr ;
lastOperator ;
tfDisplay.setText;
break;
case :
if inStr.contains
inStr currentBtnLabel;
tfDisplay.setTextinStr;
break;
case :
double percentage Double.parseDoubleinStr;
inStr Double.toStringpercentage;
tfDisplay.setTextinStr;
break;
case :
double sqrtResult Math.sqrtDoubleparseDoubleinStr;
inStr Double.toStringsqrtResult;
tfDisplay.setTextinStr;
break;
case :
if inStrlength
inStr inStr.substring inStr.length;
else
inStr ;
tfDisplay.setTextinStr;
break;
;
private void compute
int inNum Integer.parseIntinStr;
inStr ;
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.setTextresult ;
@Override
public void startStage primaryStage
tfDisplay new TextField;
tfDisplay.setEditablefalse;
tfDisplay.setAlignmentPosCENTERRIGHT;
int numCols ;
int numRows ;
GridPane paneButton new GridPane;
paneButton.setPaddingnew Insets;
paneButton.setVgap;
paneButton.setHgap;
ColumnConstraints columns new ColumnConstraintsnumCols;
for int i ; i numCols; i
columnsi new ColumnConstraints;
columnsisetHgrowPriorityALWAYS;
columnsisetFillWidthtrue;
paneButton.getColumnConstraintsaddcolumnsi;
btns new Button;
for int i ; i btnslength; i
btnsi new ButtonbtnLabelsi;
btnsisetOnActionhandler;
b
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started