Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Use KiloConverter.java 1. Create another converter to convert centimeters to inches (1 cm = 0.393701 inch) 2. Create another converter to convert Celsius to Fahrenheit.
Use KiloConverter.java
1. Create another converter to convert centimeters to inches (1 cm = 0.393701 inch)
2. Create another converter to convert Celsius to Fahrenheit. °C to °F: Multiply by 9, then divide by 5, then add 32
3. Create another converter to convert Fahrenheit to Celsius. °F to °C: Deduct 32, then multiply by 5, then divide by 9
KiloConverter.javaOpen this document with ReadSpeaker docReader
KiloConverter.java
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
/**
* Kilometer Converter application
*/
public class KiloConverter extends Application
{
// Fields
private TextField kiloTextField;
private Label resultLabel;
public static void main(String[] args)
{
// Launch the application.
launch(args);
}
@Override
public void start(Stage primaryStage)
{
// Create a Label to display a prompt.
Label promptLabel = new Label("Enter a distance in kilometers:");
// Create a TextField for input.
kiloTextField = new TextField();
// Create a Button to perform the conversion.
Button calcButton = new Button("Convert");
// Register the event handler.
calcButton.setOnAction(new CalcButtonHandler());
// Create an empty Label to display the result.
resultLabel = new Label();
// Put the promptLabel and the kiloTextField in an HBox.
HBox hbox = new HBox(10, promptLabel, kiloTextField);
// Put the HBox, calcButton, and resultLabel in a VBox.
VBox vbox = new VBox(10, hbox, calcButton, resultLabel);
// Set the VBox's alignment to center.
vbox.setAlignment(Pos.CENTER);
// Set the VBox's padding to 10 pixels.
vbox.setPadding(new Insets(10));
// Create a Scene.
Scene scene = new Scene(vbox);
// Add the Scene to the Stage.
primaryStage.setScene(scene);
// Set the stage title.
primaryStage.setTitle("Kilometer Converter");
// Show the window.
primaryStage.show();
}
/*
* Event handler class for calcButton
*/
class CalcButtonHandler implements EventHandler
{
@Override
public void handle(ActionEvent event)
{
// Get the kilometers.
Double kilometers = Double.parseDouble(kiloTextField.getText());
// Convert the kilometers to miles.
Double miles = kilometers * 0.6214;
// Display the results.
resultLabel.setText(String.format("%,.2f miles", miles));
}
}
}
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