import java.io.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends JApplet implements ActionListener
{
int choice;
double a,b, result = 0;
String str;
JTextField jt;
JButton btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
JButton btnAdd, btnSub, btnMul, btnDiv, btnEqual, btnDot, btnCl;
JPanel pn1, pn2;
Container cp;
public void init() // Initializing Applet & its Components
{
cp = getContentPane();
cp.setLayout ( new BorderLayout() );
jt = new JTextField(10);
jt.setHorizontalAlignment(4);
pn1 = new JPanel();
pn2 = new JPanel();
pn1.setLayout ( new FlowLayout() );
pn2.setLayout ( new GridLayout(4, 4) );
btnCl = new JButton ("Clear");
btn0 = new JButton ("0");
btn1 = new JButton ("1");
btn2 = new JButton ("2");
btn3 = new JButton ("3");
btn4 = new JButton ("4");
btn5 = new JButton ("5");
btn6 = new JButton ("6");
btn7 = new JButton ("7");
btn8 = new JButton ("8");
btn9 = new JButton ("9");
btnAdd = new JButton ("+");
btnSub = new JButton ("-");
btnMul = new JButton ("*");
btnDiv = new JButton ("/");
btnEqual = new JButton ("=");
btnDot = new JButton (".");
addButtons();
addListeners();
pn1.add (jt);
pn1.add (btnCl);
cp.add (pn1, BorderLayout.NORTH);
cp.add (pn2, BorderLayout.CENTER);
jt.setText("");
}
public void addListeners() // Adding Listeners to all Components
{
btn0.addActionListener ( this );
btn1.addActionListener ( this );
btn2.addActionListener ( this );
btn3.addActionListener ( this );
btn4.addActionListener ( this );
btn5.addActionListener ( this );
btn6.addActionListener ( this );
btn7.addActionListener ( this );
btn8.addActionListener ( this );
btn9.addActionListener ( this );
btnDot.addActionListener ( this );
btnCl.addActionListener ( this );
btnAdd.addActionListener ( this );
btnSub.addActionListener ( this );
btnMul.addActionListener ( this );
btnDiv.addActionListener ( this );
btnEqual.addActionListener ( this );
}
public void addButtons() // Adding all Components to JPanel
{
pn2.add ( btn7 );
pn2.add ( btn8 );
pn2.add ( btn9 );
pn2.add ( btnDiv );
pn2.add ( btn4 );
pn2.add ( btn5 );
pn2.add ( btn6 );
pn2.add ( btnMul );
pn2.add ( btn1 );
pn2.add ( btn2 );
pn2.add ( btn3 );
pn2.add ( btnSub );
pn2.add ( btn0 );
pn2.add ( btnDot );
pn2.add ( btnAdd );
pn2.add ( btnEqual );
}
public void operation(double a, double b, int op)
{
switch ( choice )
{
case 1 :
{ result = a + b; break; }
case 2 :
{ result = a - b; break; }
case 3 :
{ result = a * b; break; }
case 4 :
{ result = a / b; break; }
default :
{ jt.setText("0"); break; }
}
}
public void actionPerformed(ActionEvent e) // Actions to be performed
{ // whenever event is triggered
try
{
str = (String)e.getActionCommand();
if ( str = = "+" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 1;
}
else if ( str = = "-" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 2;
}
else if ( str = = "*" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 3;
}
else if ( str = = "/" )
{
a=Double.parseDouble (jt.getText());
jt.setText("");
choice = 4;
}
else if ( str = = "=" )
{
b = Double.parseDouble (jt.getText());
operation(a, b, choice);
jt.setText("");
System.out.println(result);
}
else if ( str = = "Clear" )
{
jt.setText("");
choice = 0;
a = b = result = 0;
}
else
{
jt.setText( jt.getText() +str );
}
}
catch(Exception ex)
{
jt.setText("Invalid Operation");
System.out.println("Invalid Operation");
}
}
}
//<applet code="Calculator.class" width=200 height=200></applet>
Related Post:
computer
- Take a better selfie with Lily
- Free Lecture The Psychology of Computer Insecurity
- MOOC Research and Innovation
- Calculating Ada The Countess of Computing
- When can Quantum Annealing win
- Creating a templated Binary Search Tree Class in C
- Projecting without a projector sharing your smartphone content onto an arbitrary display
- Will a robot take your job
- Facebook Introduces ‘Hack ’ the programming language of the future
- High Resolution Scary Haunted House Wallpapers for Desktop
- TYBSC IT Sem V Question Papers 2009 Mumbai University
- Home automation update
- Very easy to download youtube videos audio mp3 format
- HD Dark Desktop Background Wallpapers Download
- Launching the Quantum Artificial Intelligence Lab
- Syrias children learn to code with the Raspberry Pi
- Running omxplayer from the command line easily using alias
- Largest collection of Google Logos on the web Set 7
- Collection of SQL queries with Answer and Output Set 2
- Prevent access to specific partition or drive
- Summer Games Learn to Program
- PiAUISuite Update and Voicecommand v3 1
- Sign in to edx org with Google and Facebook and
- Large Scale Machine Learning for Drug Discovery
- Hacker Tricks from Insiders A Threat to ERP Systems
for
- High Resolution Scary Haunted House Wallpapers for Desktop
- Large Scale Machine Learning for Drug Discovery
- The Next Chapter for Flu Trends
- HD Windows Logo Wallpaper for Desktop
- Apple is building a car
- Voice Command v3 0 for the Raspberry Pi
- Voice Command v2 0 for the Raspberry Pi
- Mac Apple Logo HD Wallpapers for Desktop
- IT Laws and Patents notes for BSc IT Mumbai University
- Space Wallpapers for your Desktop Set 1
- Beyond Short Snippets Deep Networks for Video Classification
- The Plan to Build a Massive Online Brain for All the World’s Robots
- 20 Resources for Teaching Kids How to Program Code
- Influential Papers for 2013
- Open Access for Publications
- Free Language Lessons for Computers
- Information sharing for more efficient network utilization and management
- Reference books for B Sc IT Mumbai V VI sem
- computer tips some useful tips for your compele online security
- DeepDream a code example for visualizing Neural Networks
- Getting your fridge to order food for you with a RPi camera and a hacked up Instacart API
- How to delete all your computer Virus for free
- Recycle Bin for your portable devices
- Amazon at 20 what has the online giant ever done for retail
calculator
0 comments:
Post a Comment