In: Computer Science
Design JavaFX application with 7 labels and one textfield where user enters input inches. When user enters his choice and presses enter key to complete input, program outputs resulting yards, feet, and inches.
private Label outYards, outFeet, outInches;
private TextField inputInches;
After entering 777, the program is the returning the largest whole number of yards which is 21. (Two yards take only 21 * 36 = 756 inches)
From the remainder (777 -756 which is 21) program determines the largest whole number of feet. In 21 inches there are 1 feet. (Two feet take only 2 *12 = 24 inches, only one group of 12 fits)
So 21-12 is 9 and that's how you find the remaining inches.
So for input of inches the program should return 2 yards, 2 feet, and 2 inches.
Follow these conversion facts:
1 yard has 36 inches.
1 foot has 12 inches.
Working code implemented in and appropriate comments provided for better understanding:
Here I am attaching code for these files:
Source code for P5.java:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
/**
* Program converts inches to yards, feet, and inches using
javafx.
*/
public class P5 extends Application
{
public void start(Stage stage)
{
Scene scene = new Scene(new P5Pane(), 300, 150);
stage.setTitle("Converting inches to yards,feet,inches");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Source code for P5Pane.java:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
/**
* Program converts inches to yards, feet, and inches using
javafx.
*/
public class P5Pane extends GridPane
{
private Label outYards,outFeet,outInches;
private TextField inputInches;
public P5Pane()
{
Label inputLabel = new Label("Input Inches in Range 0 to 100000
:");
GridPane.setHalignment(inputLabel, HPos.RIGHT);
Label yardsLabel = new Label("Yards:");
GridPane.setHalignment(yardsLabel, HPos.RIGHT);
Label feetLabel = new Label("Feet:");
GridPane.setHalignment(feetLabel, HPos.RIGHT);
Label inchesLabel = new Label("Inches:");
GridPane.setHalignment(inchesLabel, HPos.RIGHT);
outYards = new Label("---");
GridPane.setHalignment(outYards, HPos.RIGHT);
outFeet = new Label("---");
GridPane.setHalignment(outFeet, HPos.RIGHT);
outInches = new Label("---");
GridPane.setHalignment(outInches, HPos.RIGHT);
inputInches = new TextField();
inputInches.setPrefWidth(50);
inputInches.setAlignment(Pos.CENTER);
inputInches.setOnAction(this::calculate);
setAlignment(Pos.CENTER);
setHgap(20);
setVgap(10);
setStyle("-fx-background-color: limegreen");
add(inputLabel, 0, 0);
add(inputInches, 1, 0);
add(yardsLabel,0 ,1 );
add(feetLabel, 0, 2);
add(inchesLabel, 0, 3);
add(outYards, 1, 1);
add(outFeet, 1, 2);
add(outInches, 1, 3);
}
public void calculate(ActionEvent event)
{
int number = Integer.parseInt(inputInches.getText());
int yard = (int) (number/36);
int yard_rem = (int) (number%36);
int feet = (int) (yard_rem/12);
int feet_rem = (int) (yard_rem%12);
int inches = feet_rem;
outYards.setText(yard+"");
outFeet.setText(feet+"");
outInches.setText(inches+"");
}
}
UML Diagram:
Sample Output Screenshots:
Hope it helps, if you like the answer give it a thumbs up. Thank you.