Write an event driven program to perform the arithmetic operations as shown in the interface

Write an event driven program to perform the arithmetic operations as shown in the interface

hi sir i have an assignment question, but i don't know the java programming code for that question. my question is here: Q. Write an event driven program to perform the arithmetic operations as shown in the interface arithematic operators: this is the form heading enter n1 :textbox enter n2 :textbox here is some options like add, sub, div,mul,exit.
result :

in the form of a calculator

please help me:

View Answers

June 11, 2012 at 2:57 PM

Here is an event driven example that will perform arithmetic calculations. It allows the user to input two numbers into respective textboxes. As per the button option is selected, the result will be displayed into another textbox.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class SimpleCalculate
{
    public static void main(String[] args) 
    {
        JFrame f=new JFrame();
        f.setLayout(null);
        JLabel lab1=new JLabel("Enter Number 1: ");
        JLabel lab2=new JLabel("Enter Number 2: ");
        JLabel lab3=new JLabel("Result: ");

        final JTextField text1=new JTextField(20);
        final JTextField text2=new JTextField(20);
        final JTextField text3=new JTextField(20);

        JButton b1=new JButton("Add");
        JButton b2=new JButton("Subtract");
        JButton b3=new JButton("Multiply");
        JButton b4=new JButton("Division");

        lab1.setBounds(20,20,100,20);
        text1.setBounds(140,20,100,20);

        lab2.setBounds(20,50,100,20);
        text2.setBounds(140,50,100,20);

        lab3.setBounds(20,80,100,20);
        text3.setBounds(140,80,100,20);

        b1.setBounds(260,80,80,20);
        b2.setBounds(360,80,80,20);

        b3.setBounds(460,80,80,20);
        b4.setBounds(560,80,80,20);

        b1.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae){
                 int n1=Integer.parseInt(text1.getText());
                 int n2=Integer.parseInt(text2.getText());
                 int cal=n1+n2;
                 text3.setText(Integer.toString(cal));
             }
        });
         b2.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae){
                 int n1=Integer.parseInt(text1.getText());
                 int n2=Integer.parseInt(text2.getText());
                 int cal=0;
                 if(n1>n2){
                 cal=n1-n2;
                 }
                 else if(n2>n1){
                     cal=n2-n1;
                 }
                 text3.setText(Integer.toString(cal));
             }
        });
         b3.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae){
                 int n1=Integer.parseInt(text1.getText());
                 int n2=Integer.parseInt(text2.getText());
                 int cal=n1*n2;
                 text3.setText(Integer.toString(cal));
             }
        });
         b4.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae){
                  int n1=Integer.parseInt(text1.getText());
                 int n2=Integer.parseInt(text2.getText());
                 int cal=0;
                 if(n1>n2){
                 cal=n1/n2;
                 }
                 else if(n2>n1){
                     cal=n2/n1;
                 }
                 text3.setText(Integer.toString(cal));
             }
        });

        f.add(lab1);
        f.add(text1);
        f.add(lab2);    
        f.add(text2);
        f.add(lab3);    
        f.add(text3);
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);

        f.setVisible(true);
        f.setSize(700,250);
    }
}

For more information, visit the following link:

Simple Calculator

Scientific Calculator









Related Tutorials/Questions & Answers:
Write an event driven program to perform the arithmetic operations as shown in the interface
Write an event driven program to perform the arithmetic operations as shown... an event driven program to perform the arithmetic operations as shown... help me:   Here is an event driven example that will perform arithmetic
write a program
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
Advertisements
MySQL Arithmetic
MySQL Arithmetic       This example illustrates how to use arithmetic operator in the MySQL. Arithmetic operators are used to perform mathematical operations on two expressions
Arithmetic Operators
Arithmetic Operators       Arithmetic Operators are used to perform some mathematical operations like addition, subtraction, multiplication, division, and modulo
Java JMenuItem to perform file operations
Java JMenuItem to perform file operations In this tutorial, you will learn how to use JMenuItem class to perform file operations. Java Swing introduced... example in java swing through which you can perform open and save operations
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes  import java.io.*; interface... is your code, we have modified it. import java.io.*; interface arith { int a=5
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes
i have created interface program for simple arithmetic operation but its show some error can i get reason and mistakes  import java.io.*; interface... is your code, we have modified it. import java.io.*; interface arith { int a=5
How to perform CRUD operations using gwt on Restlet server 2.0
How to perform CRUD operations using gwt on Restlet server 2.0  I want to perform CRUD operation using gwt on restlet server 2.0. The CRUD operations are like create, read, update, delete operations.Any generic code
event-driven programming paradigm of Java. - Java Beginners
event-driven programming paradigm of Java.  Design a scientific calculator using event-driven programming paradigm of Java.  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/java/swing
program that uses while loops to perform the following steps :
program that uses while loops to perform the following steps :  Write a program that uses while loops to perform the following steps : a. Prompt the use to input two integers : FirstNum and secondNum.( FirstNum must be less
write a java program
write a java program  write a program to print '*' in a circular form
How to write Java Program
How to write Java Program  how to write a program to find average of 5 student marks
Retrive data from database and perform binary tree operations on that data in jsp or java
Retrive data from database and perform binary tree operations on that data in jsp or java  To develop code for MLM project. I want to retrieve data from database dynamically and perform operations on that. Just like calculate
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  Write program which accepts five students id,name,age,department,courses, taken and its grade. a. calculate its GPA . display all student information in ascending order
Write java program?
Write java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051
write the java program?
write the java program?  1 11 121 1331 14641 15101051
Write java program?
Write java program?  1 11 121 1331 14641 15101051
Write a java application program........?
Write a java application program........?  Welcome every One : I have Q in Java? Q : Write a java application program that reads 20 integer numbers input by the user and prints how many numbers > 100 and how many numbers
write a program to print
write a program to print   write a program to print following 1 2 3 4 5 4 3 2 1 1 2 3 4 4 3 2 1 1 2 3 3 2 1 1 2 2 1 1 1
Write a program in java...
Write a program in java...  Hi, friends Please, can you help me? Q1: Write a program in java to simulate a calculator. Your program should take two... to enter an integer number. Write a program in java to find the factorial
how to write this program
how to write this program  Write a program in java which calculates... or numerical grade. You will be submitting TWO versions of this program. Write one...?¢s GPA. This program should take a studentââ?¬â?¢s First Name, Last Name a GPA
write a program - Java Beginners
write a program  1. write a program that reads a string composed of 6 words then generate and print a word composed of the first letters of the 6 words.  Hi friend, Code to solve the problem : class FirstLetter
write following program
or an interface to a package. Explain with examples? Q14 Write a short note...write following program  Q1 What is the difference between single...-threading. Explain with examples Q2 Write a java applet that takes your name
WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  How do I write a program in Java, have the program display a message with your name in it and a number (Hello john Smith!1) The number must increment from zero to nine, or decrement fron nine to zero. You
program and Interface builder
program and Interface builder  Hi, I'm just getting started in iPhone development, If someone could please suggest a good criterion for when to use a nib to lay out UI elemennts and when to layout the UI elements
write program - Java Beginners
write program  write a java program that will read the values of matrix A and B give its multiplication in matrix C  Hi friend, Code for Matrix Multiplication : class MatrixMultiplication{ public static void
producer and concumer Interface program
producer and concumer Interface program  I need producer and concumer Interface program   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.util.*; class Multithreading { int val; boolean value = false
write a program in java.
write a program in java.  arrange the natural n umber in 5x5 matrix as 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 i at centerd position and remaining arrange in anticlockwise direction.  
A Message-Driven Bean Example
to a message-driven bean within a transaction context, so all operations within.... These methods write to the JRun server's event log. Portable  Use one... A Message-Driven Bean Example   
Write a program to print using for loop
Write a program to print using for loop  Write a program to print using for loop * * * *  
How to write calculator in J2ME program?
How to write calculator in J2ME program?  How to write calculator in J2ME program
how to write servlet program in eclipse?
how to write servlet program in eclipse?  how to write servlet program in eclipse
write a java program to solve the infix expression
write a java program to solve the infix expression  ==>> solve the below expression using java program: 2+5*2-5+6/2 using stack operations or any other process in java
Event handler for Visual Component in Flex 4
Event Handler for Visual Components in flex4: Flex are provide event driven application. When a programmer interact with the application interface... or change in the interface. This is alled Event. Flex provide the ability
write a program to create a user defined
write a program to create a user defined   plz send me code   Hi Friend, Please specify what do you want to create?ADS_TO_REPLACE_1 Thanks
how to write this program? - Java Beginners
how to write this program?  (game:scissor,rock ,paper) A program that plays the sciccor-rock-paper game.Revise the program to let the user continuously play until either the user or the computer wins more than 4 times
code for a simple java program which performs basic arithmetic operation addition,subtraction,multiplication,division........
code for a simple java program which performs basic arithmetic operation addition,subtraction,multiplication,division........  code for a simple java program which performs basic arithmetic operation addition,subtraction
to write a program to display the employee details..
to write a program to display the employee details..  To dipslay... running this program the new window has to be opened asking us to enter all...   not this one actually i wnat a very small program that displays all
how to write the program - Java Beginners
how to write the program  WAP to create the report card user input Name, Class, Division, Roll no., Marks obtained in following subjects Lang Hindi History Geography Math Phy Chem. Bio Eve CSTA Also has a back up
Arithmetic Testing for School Students
Arithmetic Testing for School Students In this section, we have created an application that helps an elementary school student to learn arithmetic operations... that the student wishes to study. The student chooses arithmetic operations from
write a program in C to print following triangles on the screen
write a program in C to print following triangles on the screen  write a program in C to print following triangles on the screen
write a program to demonstrate wrapper class and its methods......
write a program to demonstrate wrapper class and its methods......  write a program to demonstrate wrapper class and its methods
write a program to use for loop print **********========== in c language
write a program to use for loop print **********========== in c language  write a program to use for loop print **========== in c language
Write a program to get student details and store in a database
Write a program to get student details and store in a database  Write a program to get student details and store in a database
The Arithmetic Operators
Java Example Using Arithmetic Operators  ... to calculate arithmetic operation. The java programming tutorial provide operators that perform the mathematical values addition, subtraction, multiplication
Arithmetic Operators
Arithmetic Operators Operator is used to perform operation on variable and values. Arithmetic Operators is used to perform arithmetic operations(like... the list of available arithmetic operators : OPERATOR WORK USE
how to write a program to search a record using mvc2
how to write a program to search a record using mvc2   how can we write a code to search a record in table by using java bean as model, servlet as contoller and jsp as view
Write a program that takes the marks of user as input
Write a program that takes the marks of user as input  Write a program that takes the marks of user as input. If marks are greater then 60... not understand how to write a program like this and thank you
write a program for complete JDBC based application
write a program for complete JDBC based application   thank you sir giving answer for my question. my another question is: Write a complete JDBC... and the program should have the facilities of Adding, Modifying, Viewing and Deleting

Ads