Question

Must create a JFrame in JAVA that must convert both from BASE2 to BASE10 and also...

Must create a JFrame in JAVA that must convert both from BASE2 to BASE10 and also BASE10 to BASE2 in the same JFrame. NOTE: The results of the conversion must be displayed within the GUI - that is not in a popup or as console output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, I have answered similar questions multiple times before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Mode of operation: You enter a decimal value (base 10) in decimal text field, press "Convert Decimal" button, the resultant binary value (base 2) will be displayed in binary text field. You enter a binary value in binary text field and press "Convert Binary", the value will be converted to decimal and displayed in decimal text field.

// BinaryDecimalConverter.java

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class BinaryDecimalConverter extends JFrame implements ActionListener {

      private static final long serialVersionUID = 1L; // suppress warnings

      // attributes for Decimal conversion

      private JButton decButton;

      private JLabel decLabel;

      private JTextField decField;

      // atttributes for Binary conversion

      private JButton binButton;

      private JLabel binLabel;

      private JTextField binField;

      // button for clearing

      private JButton clearButton;

      // class Constructor

      BinaryDecimalConverter() {

            // Used to specify GUI component layout

            GridBagConstraints layoutConst = null;

            setLayout(new GridBagLayout());

            // Set frame's title

            setTitle("Binary-Decimal converter");

            // Create objects to convert decimal numbers

            // Decimal conversion

            // Label

            decLabel = new JLabel("Decimal number:");

            // Create grid and add label

            layoutConst = new GridBagConstraints();

            // padding space around components

            layoutConst.insets = new Insets(10, 10, 10, 10);

            layoutConst.gridx = 0; // row 0

            layoutConst.gridy = 0; // column 0

            add(decLabel, layoutConst);

            // Field

            // Create field for data entry

            decField = new JTextField();

            decField.setEditable(true);

            decField.setText("0");

            decField.setColumns(15); // Initial width of 15 units

            // Create grid and add field

            layoutConst = new GridBagConstraints();

            layoutConst.insets = new Insets(10, 10, 10, 10);

            layoutConst.gridx = 1;

            layoutConst.gridy = 0;

            add(decField, layoutConst);

            // Button

            // Create button and add action listener

            decButton = new JButton("Convert Decimal");

            decButton.addActionListener(this);

            decButton.setPreferredSize(new Dimension(200, 20));

            // Create grid and add button

            layoutConst = new GridBagConstraints();

            layoutConst.insets = new Insets(10, 10, 10, 10);

            layoutConst.gridx = 2;

            layoutConst.gridy = 0;

            add(decButton, layoutConst);

            // End decimal conversion

            // Binary conversion

            // Label

            binLabel = new JLabel("Binary number:");

            layoutConst.gridx = 0;

            layoutConst.gridy = 1;

            add(binLabel, layoutConst);

            // Field

            binField = new JTextField();

            binField.setEditable(true);

            binField.setText("0");

            binField.setColumns(15); // Initial width of 15 units

            layoutConst.gridx = 1;

            layoutConst.gridy = 1;

            add(binField, layoutConst);

            // Button

            // Create button and add action listener

            binButton = new JButton("Convert Binary");

            binButton.addActionListener(this);

            binButton.setPreferredSize(new Dimension(200, 20));

            layoutConst.gridx = 2;

            layoutConst.gridy = 1;

            add(binButton, layoutConst);

            // End binary conversion

            // initializing clear button

            clearButton = new JButton("Clear");

            clearButton.setPreferredSize(new Dimension(200, 20));

            clearButton.addActionListener(this);

            layoutConst.gridx = 2;

            layoutConst.gridy = 2;

            add(clearButton, layoutConst);

      }

      /*

      * Method is automatically called when an event occurs (e.g. mouse click on

      * button

      */

      @Override

      public void actionPerformed(ActionEvent event) {

            // finding which button is clicked

            if (event.getSource().equals(decButton)) {

                  // parsing integer value in decField as a binary string

                  String bin = Integer.toBinaryString(Integer.valueOf(decField

                              .getText()));

                  binField.setText(bin);

            } else if (event.getSource().equals(binButton)) {

                  // parsing String from binField from binary String to integer

                  int number = Integer.valueOf(binField.getText(), 2);

                  // displaying in decField

                  decField.setText(number + "");

            } else if (event.getSource().equals(clearButton)) {

                  //clearing text fields

                  binField.setText("");

                  decField.setText("");

            }

      }

      public static void main(String[] args) {

            // Creating a BinaryDecimalConverter and making it visible

            BinaryDecimalConverter myFrame = new BinaryDecimalConverter();

            myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            myFrame.pack();

            myFrame.setVisible(true);

      }

}

/*OUTPUT*/

Add a comment
Know the answer?
Add Answer to:
Must create a JFrame in JAVA that must convert both from BASE2 to BASE10 and also...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME,...

    Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME, as specified by the prompt! DO NOT use user input of 1, 2, 3 etc. User input must come from clicking the buttons. Please be sure to test your program. Thank you! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate...

  • I have to create a Java paint/drawing application. The JFrame app must support the following functions:...

    I have to create a Java paint/drawing application. The JFrame app must support the following functions:  Draw curves, specified by a mouse drag.  Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated).  Shape selection (line, rectangle or oval) selected by a combo box OR menu.  Color selection using radio buttons OR menu.  Line thickness using a combo...

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Project overview: Create a java graphics program that displays an order menu and bill from a...

    Project overview: Create a java graphics program that displays an order menu and bill from a Sandwich shop, or any other establishment you prefer. In this program the design is left up to the programmer however good object oriented design is required. Below are two images that should be used to assist in development of your program. Items are selected on the Order Calculator and the Message window that displays the Subtotal, Tax and Total is displayed when the Calculate...

  • java Convert the following expressions to both Prefix and Postfix / Infix and create the binary...

    java Convert the following expressions to both Prefix and Postfix / Infix and create the binary trees which represent them. (A B/C+D$E)* (F/ G) - H B. (A+B)+(C/ (D E)-F)/G H KL+AB+C DEF$/-/HI+* -

  • Write a Java program which takes a string as a user input. Create 2 functions. The...

    Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....

  • Java: can also use “if else” statements Write a program that can convert an integer between...

    Java: can also use “if else” statements Write a program that can convert an integer between 0 and 15 into hex number (including 0 and 15). The user enters an integer from the console and the program displays the corresponding hex number. If the user enters an integer out of range, the program displays a warning message about the invalid input. Table. Conversion between Decimal and Hexadecimal Decimal Hexadecimal 0 0 1 1 2 2 3 3 4 4 5...

  • (Must be in C#) (Console will be my choice) Create a program called NarwhalCalc that will...

    (Must be in C#) (Console will be my choice) Create a program called NarwhalCalc that will be used to compute a narwhal's weight in tons based on its length in feet. Your program must use a method to do this and you must write this method. Your method must be called FeetToTons and the method must accept a parameter called feet (type double). The method should then use the formula shown below to calculate the narwhal's weight, then RETURN (not...

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT