10 points CISC 3115-1BC Chapter 12: in class assignment Student Name: Step #1: In this in class assignment you will create a tip calculator application with a JavaFX user interface. The application will let you enter the amount of a restaurant charge, and it will display the amount of a 20% tip. The completed user interface will look like this: Restaurant Charge: 100.00 Calculate Tip Amount to Tip: $20.00 X In your code, you will create a Scene containing the following controls: ? Various Label controls to display text ? A TextField control to read the user’s input ? A Button control to calculate and display the tip All of these controls will be placed into a VBox layout container to achieve a vertical arrangement. The VBox will then become the root node in the Scene. You will also write an event handler that responds when the user clicks the Button control. Step #1 Write a \”skeleton\” Program Start a new Java source code file. Write import statements for the following JavaFX classes: /afx.application.Application ver.

The Correct Answer and Explanation is:

To complete the assignment, we’ll be creating a JavaFX program with a simple user interface for calculating a 20% tip based on a restaurant charge. The program will consist of several JavaFX components: labels, text fields, buttons, and layout containers like VBox. Below is a skeleton of the JavaFX program for this task.

Java Code for Skeleton Program

javaCopyEditimport javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TipCalculator extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Create the labels
        Label label1 = new Label("Restaurant Charge:");
        Label label2 = new Label("Amount to Tip:");

        // Create a TextField for user input
        TextField chargeField = new TextField();
        
        // Create a label to display the calculated tip
        Label tipAmountLabel = new Label("$0.00");

        // Create a button to trigger the calculation
        Button calculateButton = new Button("Calculate Tip");

        // Add an event handler for the button click
        calculateButton.setOnAction(e -> {
            try {
                double charge = Double.parseDouble(chargeField.getText());
                double tip = charge * 0.20;
                tipAmountLabel.setText("$" + String.format("%.2f", tip));
            } catch (NumberFormatException ex) {
                tipAmountLabel.setText("Invalid input");
            }
        });

        // Create a VBox layout and add controls
        VBox layout = new VBox(10);
        layout.getChildren().addAll(label1, chargeField, calculateButton, label2, tipAmountLabel);

        // Create a scene with the layout
        Scene scene = new Scene(layout, 300, 200);

        // Set the stage (window)
        primaryStage.setTitle("Tip Calculator");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Explanation

This program is a basic JavaFX application that allows a user to calculate a 20% tip based on a restaurant charge entered in a text field. Let’s go step by step to understand the code.

  1. Import Statements: We import necessary JavaFX classes such as Application, Scene, Button, Label, TextField, VBox, and Stage. These are the components that will make up the UI and handle the logic.
  2. TipCalculator Class: This class extends Application, the main class for JavaFX applications. The start method is overridden to set up the UI and event handling.
  3. User Interface (UI) Elements:
    • A Label (label1) prompts the user to enter the restaurant charge.
    • A TextField (chargeField) allows the user to input the charge.
    • A Button (calculateButton) is used to trigger the calculation of the tip.
    • Another Label (tipAmountLabel) will display the calculated tip.
  4. Event Handling: When the button is clicked, an event handler is triggered. It attempts to parse the user input into a double, calculates the 20% tip, and updates the tipAmountLabel. If the input is not a valid number, it shows “Invalid input”.
  5. VBox Layout: All UI elements are added to a VBox container, which arranges them vertically. The VBox is then added to a Scene, which is displayed on the primary Stage.
  6. Program Execution: The main method calls launch(args), starting the JavaFX application and displaying the window to the user.

This program demonstrates basic JavaFX controls and event handling, providing an interactive way to calculate and display the tip.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *