Question

Create a class Calculator.java that has two JTextFields, multiply JButton, divide JButton, equals JButton, and a...

Create a class Calculator.java that has two JTextFields, multiply JButton, divide JButton, equals JButton, and a JLabel that present the user with the results.

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

Program screenshot:

​Sample Output:

​Run 1

​Run 2

​Run 3

Program To copy:

/************************************************************************
*This program uses GUI and performs multiplication, division. *
*It also checks whether the two numbers are equal or not         *
************************************************************************/

//Include the header file.
import java.awt.event.*;
import javax.swing.*;

public class Calculator implements ActionListener {

//Define the text fields,buttons and label.
JTextField t1,t2;
JLabel l;
JButton b1,b2,b3;

//Class constructor.
Calculator()
{
  //Make the frame.
  JFrame fr = new JFrame();
  
  //Make the text field 1.
  t1 = new JTextField();
  t1.setBounds(50,50,100,20);
  
  //Make the text field 2.
  
  t2 = new JTextField();
  t2.setBounds(50, 100, 100, 20);
  
  //Make the button for multiplication.
  b1 = new JButton("*");
  b1.setBounds(30,150,50,50);
  
  //Make the button for division.
  b2 = new JButton("/");
  b2.setBounds(100,150,50,50);
  
  //Make the button for equal.
  b3 = new JButton("=");
  b3.setBounds(170,150,50,50);
  
  //Make the label for output.
  l = new JLabel();
  l.setBounds(100,220,250,50);
  
  
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  
  //Add all the above objects into the frame.
  fr.add(t1);fr.add(t2);
  fr.add(b1);fr.add(b2);
  fr.add(b3);
  fr.add(l);
  
  fr.setSize(400,400);
  fr.setLayout(null);
  fr.setVisible(true);
}

//Define the function actionPerformed.
public void actionPerformed(ActionEvent ae)
{
  //Prompt the user to enter the numbers
  String s1 = t1.getText();
  String s2 = t2.getText();
  
  int x,y;
  
  //convert the entered string into numbers.
  x = Integer.parseInt(s1);
  y = Integer.parseInt(s2);
  
  int z = 0;
  String p,q,r;
  
  //Try block for wrong input like divide by zero.
  try
  {
   //Perform the multiplication.
  if(ae.getSource() == b1)
  {
   z = x * y;
   p = String.valueOf(z);
   l.setText(p);
  }
  
  //Perform the division.
   if(ae.getSource() == b2)
  {
   z = x / y;
   q = String.valueOf(z);
   l.setText(q);
  }
  
   //Check whether two numbers are equal or not.
  if(ae.getSource() == b3)
  {
   if(x==y)
   {
    r = "True";
    l.setText(r);
   }
   else
   {
    r = "False";
    l.setText(r);
   }
  }
  }catch(Exception e)
  {
   l.setText("Wrong input " + e);
  }

}

//Main function.
public static void main(String[] args)
{
  new Calculator();
}
}

Add a comment
Know the answer?
Add Answer to:
Create a class Calculator.java that has two JTextFields, multiply JButton, divide JButton, equals JButton, and a...
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
  • Please help me the JAVA program - Choose Your Operation Write a program that uses a...

    Please help me the JAVA program - Choose Your Operation Write a program that uses a WidgetView object to do the following: Generate two random integers between 1 and 9 (inclusive). Name one of them x, the other y. Display them to the user using JLabel objects. Create a JLabel object displaying the text "Enter an operation number." Create a JTextField for the user's input. Create a JButton displaying the text "Press here when you've entered your operation." Use addAndWait...

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

  • So I am having probelms with figuring out how to do this program the part in...

    So I am having probelms with figuring out how to do this program the part in bold is the part I dont know how to do. This is in JAVA. What I have so far is shown below the question, this is right upto what I have to do next. Choose Your Operation Write a program that uses a WidgetViewer object to do the following: Generate two random integers between 1 and 9 (inclusive). Name one of them x, the...

  • Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event...

    Help with StringAnalysis Write a WidgetViewer application. Create a class StringAnalysis. This class has an event handler inner that extends WidgetViewerActionEvent it has instance variables StringSet sSet JTextField inputStr JLabel numStr JLabel numChar In the constructor, create a WidgetViewer object create sSet create a local variable JLabel prompt initialized to "Enter a String" create inputStr with some number of columns create a local variable JButton pushMe initialized to "Push to include String" create numStr initialized to "Number of Strings: 0"...

  • Create a C++ calculator that allows you to add, multiply, divide, or subtract, and allows you...

    Create a C++ calculator that allows you to add, multiply, divide, or subtract, and allows you to inlcude an infinite amount of inputted numbers for it.

  • 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

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

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

  • c++ Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two...

    c++ Q.2. a) Create a C++ class template called MathCalc, to perform arithmetic operations between two numbers. These arithmetic operations are addition, subtraction, multiplication and division. The MathCalc class template accepts two type parameters, which can be defined as: template <class T, class U>. This allows MathCalc to carry out arithmetic operations between two different data types. MathCalc has the following public methods, described here in pseudocode: i. Ret Sum(augend, addend); Ret is the return value of the method. ii....

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