Question

In: Computer Science

Apply JavaFX and exception handling to design a CircleApp class using proper JavaFX GUI components, such...

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

Solutions

Expert Solution

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...!!!


Related Solutions

EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it. Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception....
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
I do not know how to: D. Apply exception handling using try-catch for System.OutOfMemoryException. the code...
I do not know how to: D. Apply exception handling using try-catch for System.OutOfMemoryException. the code i have in POWERSHELL: while($true) { $input= Read-Host "Enter the option 1-5" if ($input -eq 5) { break } #B. Create a “switch” statement that continues to prompt a user by doing each of the following activities, until a user presses key 5: switch ( $input ){ #Using a regular expression, list files within the Requirements1 folder, with the .log file extension and redirect...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be...
1) Design an implement a program that created and exception class called StringTooLongException, designed to be thrown when a string is discovered that has too many characters in it. Create a driver that reads in strings from the user until the user enters DONE. If a string that has more then 5 characters is entered, throw the exception. Allow the thrown exception to terminate the program 2) Create a class called TestScore that has a constructor that accepts an array...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use...
Given the following specification, design a class diagram using PlantUML. To design the class diagram, use abstract, static, package, namespace, association, and generalization on PlantUML Specification: A school has a principal, many students, and many teachers. Each of these persons has a name, birth date, and may borrow and return books. The book class must contain a title, abstract, and when it is available. Teachers and the principal are both paid a salary. A school has many playgrounds and rooms....
Using the design of a kitchen blender and mixer: Apply several of the progressive methods to...
Using the design of a kitchen blender and mixer: Apply several of the progressive methods to the product design and development. Explain how this benefits product development.
Using the principles and components of digital and linear logic circuits design a circuit that is...
Using the principles and components of digital and linear logic circuits design a circuit that is able to open and close the door of a garage of vehicles according to the following conditions: a) The door will open in the following cases: a.1) a manual push-button is activated and there is sunlight. a.2) the button is not activated, there is no sunlight and the light of a vehicle is detected. b) The door must close after 30 seconds of opening....
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT