Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Write an event driven program to perform the arithmetic operations as shown in the interface
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


krishna p nair
Write an event driven program to perform the arithmetic operations as shown in the interface
1 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners


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:


Advertisement
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
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
Advertisements
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
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
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
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
write a program
write a program  write a program add 2 no.s without use arithmetic,unary operaters in java
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
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
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
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
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
A Message-Driven Bean Example
-driven bean within a transaction context, so all operations within the onMessage... A Message-Driven Bean Example   ... messages asynchronously,  a Message-driven bean is used. Message driven
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
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
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
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
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 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 java program?
Write java program?  1 11 121 1331 14641 15101051
producer and concumer Interface program
producer and concumer Interface program  I need producer and concumer Interface program   Hi Friend, Try the following code: import java.util.*; class Multithreading { int val; boolean value = false; synchronized int
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 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 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
Advertisements
 

 

 

DMCA.com