In: Computer Science
Apply JavaFX and exception handling to design a CircleApp class using proper JavaFX GUI components, such as labels, text fields and buttons(Submit, Clear, and Exit) to compute the area of circle and display the radius user entered in the text field and computing result in a proper location in the application windows. It will also verify invalid data entries for radius(No letters and must be a postive real number) using expection handling. Your code will also have a custom-designed exception class named NegativeDoubleException to handle the negative data expection. Your validtion info may be displayed in the application window or use output method of JOptionPane to prompt user for the correct data entry. Use loop to allow user to reenter a radius if it is invalid util a correct data is entered. Run and test your code to meet the requirements(you don't need worry about the separation). Your validtion code will continue to run util the valid data is entered.
Must use meaningful names for fields, variables, methods and classes.
Must use GUI components(Label, TextField, 3 Buttons). You may use any layout manager in your code.
Must document each of your source code
Code:-
/// Circle.java ///
public class Circle
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
private double getRadius()
{
return radius;
}
public double area()
{
return Math.PI * this.radius * this.radius;
}
}
/// NegativeDoubleException.java ///
public class NegativeDoubleException extends Exception
{
public NegativeDoubleException()
{
super("Radius should be a positive real number.");
}
}
/// CircleApp.java ///
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class CircleApp extends Application implements
EventHandler<ActionEvent> {
private TextField radiusTxt;
private Button submitBtn;
private Button clearBtn;
private Button exitBtn;
public TextField areaTxt;
private Circle circle;
private Label errorLbl;
public CircleApp()
{
this.radiusTxt = new TextField();
this.submitBtn = new Button("Submit");
this.clearBtn = new Button("Clear");
this.exitBtn = new Button("Exit");
this.submitBtn.setPrefWidth(90);
this.clearBtn.setPrefWidth(90);
this.exitBtn.setPrefWidth(90);
this.submitBtn.setOnAction(this);
this.clearBtn.setOnAction(this);
this.exitBtn.setOnAction(this);
this.areaTxt = new TextField();
this.areaTxt.setEditable(false);
this.errorLbl = new Label();
this.errorLbl.setStyle("-fx-text-fill: red");
}
private double getRadius() throws NegativeDoubleException
{
if (this.radiusTxt.getText().matches("[0-9]+\\.?[0-9]+"))
return Double.parseDouble(this.radiusTxt.getText());
else
throw new NegativeDoubleException();
}
private GridPane getUIPane()
{
GridPane uiPane = new GridPane();
uiPane.setPadding(new Insets(10));
uiPane.setHgap(10);
uiPane.setVgap(10);
uiPane.add(new Label("Enter Radius:"), 0, 0);
uiPane.add(this.radiusTxt, 1, 0, 2, 1);
uiPane.add(new Label("Area:"), 0, 1);
uiPane.add(this.areaTxt, 1, 1, 2, 1);
uiPane.add(this.submitBtn, 0, 2);
uiPane.add(this.clearBtn, 1, 2);
uiPane.add(this.exitBtn, 2, 2);
uiPane.add(this.errorLbl, 0, 3, 3, 1);
return uiPane;
}
@Override
public void handle(ActionEvent ae)
{
Button btnClicked = (Button) ae.getSource();
if (btnClicked == this.submitBtn)
{
try
{
errorLbl.setText("");
double radius = getRadius();
circle = new Circle(radius);
this.areaTxt.setText(String.format("%.2f", circle.area()));
}
catch (NegativeDoubleException nde)
{
errorLbl.setText(nde.getMessage());
radiusTxt.requestFocus();
}
}
else if (btnClicked == this.clearBtn)
{
this.radiusTxt.setText("");
this.areaTxt.setText("");
errorLbl.setText("");
}
else if (btnClicked == this.exitBtn)
System.exit(0);
}
@Override
public void start(Stage primaryStage)
{
Scene scene = new Scene(getUIPane(), 400, 300);
primaryStage.setScene(scene);
primaryStage.setTitle("Circle App");
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Code Screenshots:-
Circle.java
NegativeDoubleException.java
CircleApp.java
Output:-
Please UPVOTE thank you...!!!