Question

Calculator = 0.01 Calculate . Add O subtract O Multiply ODvide uttons on the right determine the operation that is performed

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

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. Thanks

// Calculator.java

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JRadioButton;

import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener {

      // declaring needed UI components

      private JTextField op1, op2;

      private JLabel operator, resultLabel;

      private JButton calculate;

      private JRadioButton addBtn, subBtn, mulBtn, divBtn;

      // constructor initializing GUI

      public Calculator() {

            // will exit on close

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            // initializing text fields for operands and labels for operator and

            // result

            op1 = new JTextField(10);

            operator = new JLabel("+");

            op2 = new JTextField(10);

            resultLabel = new JLabel("0.0");

            // initializing calculate button

            calculate = new JButton("Calculate");

            // initializing radio buttons

            addBtn = new JRadioButton("Add");

            subBtn = new JRadioButton("Subtract");

            mulBtn = new JRadioButton("Multiply");

            divBtn = new JRadioButton("Divide");

            // creating a button group and adding all radio buttons to it, so that

            // only one can be selected at a time

            ButtonGroup group = new ButtonGroup();

            group.add(addBtn);

            group.add(subBtn);

            group.add(mulBtn);

            group.add(divBtn);

            // selecting add button by default

            addBtn.setSelected(true);

            // using flow layout

            setLayout(new FlowLayout());

            // adding each component in order

            add(op1);

            add(operator);

            add(op2);

            add(new JLabel(" = "));

            add(resultLabel);

            add(calculate);

            add(addBtn);

            add(subBtn);

            add(mulBtn);

            add(divBtn);

            // setting window size and title, displaying

            setSize(800, 100);

            setTitle("Calculator");

            setVisible(true);

            // addig action listeners to the buttons

            calculate.addActionListener(this);

            addBtn.addActionListener(this);

            subBtn.addActionListener(this);

            mulBtn.addActionListener(this);

            divBtn.addActionListener(this);

      }

      public static void main(String[] args) {

            // invoking the constructor

            new Calculator();

      }

      @Override

      public void actionPerformed(ActionEvent e) {

            // finding source of action

            if (e.getSource().equals(calculate)) {

                  // calculate btn is pressed

                  try {

                        // fetching values, performing operation and displaying the

                        // result

                        double num1 = Double.parseDouble(op1.getText());

                        double num2 = Double.parseDouble(op2.getText());

                        double result = 0;

                        if (addBtn.isSelected()) {

                              result = num1 + num2;

                        } else if (subBtn.isSelected()) {

                              result = num1 - num2;

                        } else if (mulBtn.isSelected()) {

                              result = num1 * num2;

                        } else if (divBtn.isSelected()) {

                              result = num1 / num2;

                        }

                        resultLabel.setText("" + result);

                  } catch (Exception ex) {

                        resultLabel.setText("Invalid!");

                  }

            } else {

                  // a radio button is clicked, updating operator label text

                  if (addBtn.isSelected()) {

                        operator.setText("+");

                  } else if (subBtn.isSelected()) {

                        operator.setText("-");

                  } else if (mulBtn.isSelected()) {

                        operator.setText("x");

                  } else if (divBtn.isSelected()) {

                        operator.setText("/");

                  }

            }

      }

}

/*OUTPUT*/

의 Calculator 1.0625 CalculateAdd SubtractMultiply Divide 14.4 15.3

Add a comment
Know the answer?
Add Answer to:
Calculator = 0.01 Calculate . Add O subtract O Multiply ODvide uttons on the right determine the operation that is perf...
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
  • Could you code this question on Matlab? :) Question: Design a GUI based application, named Calculator, which perform basic arithmetic operations ie., add, subtract, multiply, and divide Your design m...

    Could you code this question on Matlab? :) Question: Design a GUI based application, named Calculator, which perform basic arithmetic operations ie., add, subtract, multiply, and divide Your design must have 1) Two separate input fields for reading the two operands (let's say a and b) 2) 4 Four button (labeled as +, -, *, /) to let the user select an operation. 3) An output field for displaying the result Following table shows operations performed on pushing each of...

  • Create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two...

    Create a 'simple' calculator. The calculator should be able to add, subtract, multiply, and divide two numbers. You can use whatever format you want to receive the numbers (such as textboxes). You also can use whatever method (such as a button for each operation +, -, *, /) to determine what operation the user wants to accomplish. The output should be displayed in a similar format to "1 + 2 = 3" or "1 - 2 = -1". Make sure...

  • Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit...

    Write a calculator that will give the user this menu options: 1)Add 2)Subtract 3)Multiply 4)Divide 5)Exit . Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Please answer in python and make it simple. Please implement proper indentation, not just...

  • IN PYTHON! Create a calculator GUI. The GUI should let the user add, subtract, multiply, and...

    IN PYTHON! Create a calculator GUI. The GUI should let the user add, subtract, multiply, and divide two numbers they input. All user input and the result displayed to the user should be seen in the GUI itself (i.e. the terminal should not show anything). Thank you very much! PLEASE PASTE IN HERE THE RIGHT INDENTS

  • Write a calculator program using JavaScript in HTML in the same HTML file. (There will only...

    Write a calculator program using JavaScript in HTML in the same HTML file. (There will only be 1 HTML file containing everything that you use for this program.) *************JavaScript Functions should be written in the HTML <head> .............. </head> tag, **************** (Please be mindful of the formatting of the text of your program. Meaning that do not copy paste everything in a single line. Add clear comments for a better understanding of your program) as follows Assignment: create a form...

  • 7- (50 points) Write a simple calculator in Python which supports four basic mathematical operators (1.e....

    7- (50 points) Write a simple calculator in Python which supports four basic mathematical operators (1.e. + - / *). You need to define a module which supports addition, deletion, multiplication and subtraction. Then import your module to your calculator file and use it in your code. The following example shows how your basic calculator needs to work. Select operation: 1.Add 2.Subtract 3.Multiply 4.Divide Enter your choice(1/2/3/4): 3 Enter first number: 12 Enter second number: 14 12 * 14 =...

  • C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to...

    C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

  • Create a simple calculator. Your interface should contain two input boxes in which the user can...

    Create a simple calculator. Your interface should contain two input boxes in which the user can put two numbers. There should be four buttons: add, subtract, multiply, and divide. When the user inputs two number and clicks on an operation button, the result should be displayed in the label. Can some please help my code that i came up with just displays a pop box with no words or nothing! I'm not the best at coding. Any tips or advice...

  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

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