Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assuming that C is a Celsius temperatures the following formula converts the tem

ID: 3587354 • Letter: A

Question

Assuming that C is a Celsius temperatures the following formula converts the temperature to Fahrenheit: F= 1.8 * C + 32 Assuming that F is a Fahrenheit temperatures, the following formula converts the temperature to Celsius: C = (5/9)*(F-32) Crate a JavaFX application that allows the user to enter a temperature.The application should have Button components described as follows: A button that reads Converts to Fahrenheit. if the user clicks this button, the application should treat the temperature that is entered as a Celsius temperature and convert it to Fahrenheit. A button that reads Convert to Celsius. If the user clicks this button the application should treat the temperature that is entered as a Fahrenheit temperature, and convert it to Celsius.

//This is some code

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
This is the main application class for the
Celisus and Fahrenheit Temperature Converter
programming challege.
*/

public class TemperatureConverter extends Application
{
public void start(Stage stage) throws Exception
{
// Load the FXML file.
Parent parent = FXMLLoader.load(
getClass().getResource("TemperatureConverter.fxml"));

// Build the scene graph.
Scene scene = new Scene(parent);

// Display our window, using the scene graph.
stage.setTitle("Temperature Converter");
stage.setScene(scene);
stage.show();
}

public static void main(String[] args)
{
// Launch the application.
launch(args);
}
}

Explanation / Answer

Steps :

1. Create New JavaFx FXML Application project in Netbeans

prroject name - JavaFXApplication2 (you can refactor file name if required)

2. Three files will be created 1.Controller 2.java file 3.fxml file

Replace the contents of the files with following code and rename file according to your convinient

file1 - FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController">
<children>
<Button layoutX="16" layoutY="90" text="Converts to Fahrenheit" fx:id="button1" />
<Button layoutX="175" layoutY="90" text="Convert to Celsius" fx:id="button2" />
<TextField layoutX="80" layoutY="30" text="100" fx:id="text"/>
<Label layoutX="16" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>

file2 -

FXMLDocumentController.java

package javafxapplication2;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class FXMLDocumentController implements Initializable {
  
@FXML
public Label label;
public TextField text;
  
@FXML
public void convertToF(ActionEvent event) {
System.out.println("You clicked me!");
double num = Double.parseDouble(text.getText());
Double f = 1.8 * num + 32.0;
label.setText("Temperature in Fahrenheit is :" + Double.toString(f));
}
public void convertToC(ActionEvent event) {
System.out.println("You clicked me!");
double num = Double.parseDouble(text.getText());
Double f = (5.0/9.0) * (num -32.0);
label.setText("Temperature in Celsius is :" + Double.toString(f));
}
  
@Override
public void initialize(URL url, ResourceBundle rb) {
}   
}

file3 - JavaFXApplication2


package javafxapplication2;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXApplication2 extends Application {
  
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
  
Scene scene = new Scene(root, 300, 200);
stage.setTitle("Welcome");
stage.setScene(scene);
stage.show();
}

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

after copying these contents to files run the application.