Question
Write a program that converts between decimal, hex, and binary numbers, as shown in Figure 16.37c. When you enter a decimal value in the decimal-
Write a program that converts between decimal, hex, and binary numbers, as shown in Figure 16.37c. When you enter a decimal value in the decimal- value text field and press the Enter key, its corresponding hex and binary numbers are displayed in the other two text fields. Likewise, you can enter values in the other fields and convert them accordingly.(Hint:Use the Integer.parseInt(s, radix) method to parse a string to a decimal and use Integer.toHexString(decimal) and Integer.toBinaryString(decimal) to obtain a hex number or a binary number from a decimal.) My code.
public class Conversion extends Application { TextField tfDecimal =new TextField(); TextField tfHex = new TextField(); TextField tfBinary = new TextField(); private double paneWidth = 250; private double paneHeight = 150;
@Override // Override the start method in the Application class public void start (Stage primaryStage) { tfDecimal.setAlignment(Pos.BOTTOM_RIGHT); tfHex.setAlignment(Pos.BOTTOM_RIGHT); tfBinary.setAlignment(Pos.BOTTOM_RIGHT); GridPane pane = new GridPane(); pane.setAlignment(Pos.CENTER); pane.setHgap(10); pane.setVgap(2); pane.add(new Label("Decimal"),0,0); pane.add(tfDecimal,1,0); pane.add(tfHex, 1,0); pane.add(new Label("Hex"),0,1); pane.add(tfHex, 1,1); pane.add(new Label("Binary"),0,2); pane.add(tfBinary, 1,2);
Scene scene = new Scene(pane,paneWidth,paneHeight); primaryStage.setTitle("Conversion"); primaryStage.setScene(scene); primaryStage.show(); tfDecimal.setOnAction(e-> { int decimal = Integer.parseInt(tfDecimal.getText()); tfHex.setText(Integer.toHexString(decimal)); tfBinary.setText(Integer.toBinaryString(decimal)); }); tfHex.setOnAction(e-> { int hex = Integer.parseInt(tfHex.getText(), 16); tfBinary.setText(Integer.toBinaryString(hex)); tfDecimal.setText(Integer.toString(hex)); }); tfBinary.setOnAction(e-> { int binary = Integer.parseInt(tfBinary.getText(), 2); tfDecimal.setText(Integer.toString(binary)); tfHex.setText(Integer.toHexString(binary)); }); } public static void main(String[] args) { Application.launch(args); } }
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