In: Computer Science
Java
In a JavaFX application there is a TextField control(tfRadius) for taking input of a circle’s radius. A respective square whose diagonal is the circle's diameter can then be created. There are three other controls in this application: a button(btCalculate), two textareas(trCircleArea, trSquareArea). When the button is clicked, the circle's area is calculated and displayed on trCircleArea, furthermore, the square's area is also calculated and displayed on trSquareArea.
Please write the portions of codes which calculates the circle’s area and the square's area when the button(btCalculate) is clicked and both of the calculated areas are displayed on their respective textarea controls(trCircleArea, trSquareArea). Assuming the codes for layout setup are completed and you are only required to complete the calculation part and handling of button click event.
Here is the answer for your question in Java Programming Language.
Kindly upvote if you find the answer helpful.
##############################################################
CODE :
Only button handling event and calculation part
//Button event handing code btCalculate.setOnAction(e->{ //Use a try catch block to catch errors if any try{ //Store text from tfRadius field into a double variable double radius = Double.parseDouble(tfRadius.getText()); //Find diameter,circle area and square area double diameter = 2*radius; double circleArea = 3.14*radius*radius; double squareArea = 0.5*diameter*diameter; //Display them in respected text Areas trCircleArea.setText("Circle Area : " + circleArea); trSquareArea.setText("Square Area : " + squareArea); }catch(Exception ex){ System.out.println("ERROR OCCURED"); } }); |
####################################################################
Complete Code :
public class CircleNSquare extends Application{ @Override |
#########################################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
###########################################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)