SlideShare a Scribd company logo
Code :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*Designed and programmed by Srujana */
/*Designed and programmed by Srujana */
public class Calculator implements ActionListener{
char o;
int ctr=0;
String value="", cv="", oBtn;
Double answer, v1, v2;
Double NumberConverted;
Frame f;
Panel p1, p2, p3, p4, p5, p6;
private TextField tField;
private Menu EditMenu;
private MenuBar menuBar;
private MenuItem fmi1, fmi2, fmi3;
private Button num0, num1, num2, num3, num4, num5, num6, num7, num8,
num9;
private Button bAdd, bSub, bMul, bDiv, bPer, bSqrt, bFrac, bInt, bDot,
bCE, equals, backspace, clear;
Calculator(){
f = new Frame("Calculator");
menuBar = new MenuBar();
EditMenu = new Menu ("Edit");
fmi1 = new MenuItem(" Copy ");
fmi2 = new MenuItem(" Paste ");
fmi3 = new MenuItem(" Quit ");
EditMenu.add(fmi1);
EditMenu.add(fmi2);
EditMenu.addSeparator();
EditMenu.add(fmi3);
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
p6 = new Panel();
tField = new TextField(35);
num0 = new Button ("0");
num1 = new Button ("1");
num2 = new Button ("2");
num3 = new Button ("3");
num4 = new Button ("4");
num5 = new Button ("5");
num6 = new Button ("6");
num7 = new Button ("7");
num8 = new Button ("8");
num9 = new Button ("9");
bAdd = new Button ("+");
bSub = new Button ("-");
bMul = new Button ("x");
bDiv = new Button ("/");
bPer = new Button ("%");
bSqrt = new Button ("sqrt");
bFrac = new Button ("1/x");
bInt = new Button ("+/-");
bDot = new Button (".");
bCE = new Button ("CE");
equals = new Button ("=");
backspace = new Button ("Backspace");
clear = new Button ("C");
}
public void launchFrame(){
tField.setText("0.");
tField.setEnabled(false);
menuBar.add(EditMenu);
p2.add(backspace);
p2.add(bCE);
p2.add(clear);
p3.add(num7);
p3.add(num8);
p3.add(num9);
p3.add(bDiv);
p3.add(bSqrt);
p4.add(num4);
p4.add(num5);
p4.add(num6);
p4.add(bMul);
p4.add(bPer);
p5.add(num1);
p5.add(num2);
p5.add(num3);
p5.add(bSub);
p5.add(bFrac);
p6.add(num0);
p6.add(bInt);
p6.add(bDot);
p6.add(bAdd);
p6.add(equals);
p2.setLayout(new GridLayout (1, 3, 2, 2) );
p3.setLayout(new GridLayout (1, 3, 2, 2) );
p4.setLayout(new GridLayout (1, 3, 2, 2) );
p5.setLayout(new GridLayout (1, 3, 2, 2) );
p6.setLayout(new GridLayout (1, 3, 2, 2) );
f.setLayout(new GridLayout (6, 1) );
f.setResizable(false);
f.setSize(300,250);
f.add(tField);
f.add(p2);
f.add(p3);
f.add(p4);
f.add(p5);
f.add(p6);
f.setVisible(true);
f.setMenuBar(menuBar);
f.pack();
// ACTION LISTENERS
clear.addActionListener(this);
bCE.addActionListener(this);
num0.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
bAdd.addActionListener(this);
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bPer.addActionListener(this);
bInt.addActionListener(this);
bSqrt.addActionListener(this);
bFrac.addActionListener(this);
bDot.addActionListener(this);
equals.addActionListener(this);
backspace.addActionListener(this);
fmi1.addActionListener(this);
fmi2.addActionListener(this);
fmi3.addActionListener(this);
}
/* |------------ START OF ACTION EVENTS ------------| */
public void actionPerformed(ActionEvent a){
try{
/* |-------- Handling Exceptions ---------| */
if(a.getSource()==num0){
value+=0;
tField.setText(value);
}
if(a.getSource()==num1){
value+=1;
tField.setText(value);
}
if(a.getSource()==num2){
value+=2;
tField.setText(value);
}
if(a.getSource()==num3){
value+=3;
tField.setText(value);
}
if(a.getSource()==num4){
value+=4;
tField.setText(value);
}
if(a.getSource()==num5){
value+=5;
tField.setText(value);
}
if(a.getSource()==num6){
value+=6;
tField.setText(value);
}
if(a.getSource()==num7){
value+=7;
tField.setText(value);
}
if(a.getSource()==num8){
value+=8;
tField.setText(value);
}
if(a.getSource()==num9){
value+=9;
tField.setText(value);
}
if (a.getSource() == bAdd){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = '+';
value="";
tField.setText("" +value);
}
if (a.getSource() == bSub){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = '-';
value="";
tField.setText("" +value);
}
if (a.getSource() == bMul){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = 'x';
value="";
tField.setText("" +value);
}
if (a.getSource() == bDiv){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = '/';
value="";
tField.setText("" +value);
}
if (a.getSource() == bPer){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
value="";
answer = (v1/100);
tField.setText("" +answer);
}
/* |-- EQUALS ACTION --| */
if(a.getSource()==equals){
value="";
v2 = Double.parseDouble(tField.getText());
if(o=='+'){
ctr=0;
answer = v1 + v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='-'){
ctr=0;
answer = v1 - v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='x'){
ctr=0;
answer = v1 * v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='/'){
ctr=0;
answer = v1 / v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='%'){
ctr=0;
answer = v1 % v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else{}
}
/* |-- EQUALS ACTION --| */
/* |-- Clear --| */
if(a.getSource()==clear){
ctr=0;
v1=null;
v2=null;
value="";
answer=0.;
tField.setText("0.");
}
if(a.getSource()==bCE){
ctr=0;
value="";
tField.setText("0.");
}
/* |-- Point --| */
if(a.getSource() == bDot){
if(ctr==0){
value+=".";
ctr+=1;
tField.setText("" +value);
}
else{
System.out.print("");
}
}
/* |-- Back Space --| */
if(a.getSource() == backspace){
value = value.substring(0, value.length()-1 );
tField.setText("" +value);
}
/* |-- Square Root --| */
if(a.getSource() == bSqrt){
ctr=0;
value = "";
v1 = Math.sqrt( Double.parseDouble( tField.getText() ) );
tField.setText("" +v1);
}
/* |-- Integer --| */
if(a.getSource() == bInt){
ctr=0;
NumberConverted = ( Double.parseDouble(tField.getText()) * -1 );
value = "";
tField.setText("" +NumberConverted);
}
/* |-- Reciprocal --| */
if(a.getSource() == bFrac){
ctr=0;
value = "";
Double NumberContainer = ( 1 / Double.parseDouble(
tField.getText() ) );
tField.setText("" +NumberContainer);
}
// ------------ Menu Item Actions ------------ //
if(a.getSource() == fmi1){
cv = tField.getText();
}
if(a.getSource() == fmi2){
tField.setText("" +cv);
}
if(a.getSource() == fmi3){
System.exit(0);
}
} // End of Try
/* |-------- Attempting To Catch Runtime Errors ---------| */
catch(StringIndexOutOfBoundsException str){}
catch(NumberFormatException nfe){}
catch(NullPointerException npe){}
} // END OF ACTION EVENTS
public static void main (String args[]){
Calculator s = new Calculator();
s.launchFrame();
}
}
Solution
Code :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*Designed and programmed by Srujana */
/*Designed and programmed by Srujana */
public class Calculator implements ActionListener{
char o;
int ctr=0;
String value="", cv="", oBtn;
Double answer, v1, v2;
Double NumberConverted;
Frame f;
Panel p1, p2, p3, p4, p5, p6;
private TextField tField;
private Menu EditMenu;
private MenuBar menuBar;
private MenuItem fmi1, fmi2, fmi3;
private Button num0, num1, num2, num3, num4, num5, num6, num7, num8,
num9;
private Button bAdd, bSub, bMul, bDiv, bPer, bSqrt, bFrac, bInt, bDot,
bCE, equals, backspace, clear;
Calculator(){
f = new Frame("Calculator");
menuBar = new MenuBar();
EditMenu = new Menu ("Edit");
fmi1 = new MenuItem(" Copy ");
fmi2 = new MenuItem(" Paste ");
fmi3 = new MenuItem(" Quit ");
EditMenu.add(fmi1);
EditMenu.add(fmi2);
EditMenu.addSeparator();
EditMenu.add(fmi3);
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
p4 = new Panel();
p5 = new Panel();
p6 = new Panel();
tField = new TextField(35);
num0 = new Button ("0");
num1 = new Button ("1");
num2 = new Button ("2");
num3 = new Button ("3");
num4 = new Button ("4");
num5 = new Button ("5");
num6 = new Button ("6");
num7 = new Button ("7");
num8 = new Button ("8");
num9 = new Button ("9");
bAdd = new Button ("+");
bSub = new Button ("-");
bMul = new Button ("x");
bDiv = new Button ("/");
bPer = new Button ("%");
bSqrt = new Button ("sqrt");
bFrac = new Button ("1/x");
bInt = new Button ("+/-");
bDot = new Button (".");
bCE = new Button ("CE");
equals = new Button ("=");
backspace = new Button ("Backspace");
clear = new Button ("C");
}
public void launchFrame(){
tField.setText("0.");
tField.setEnabled(false);
menuBar.add(EditMenu);
p2.add(backspace);
p2.add(bCE);
p2.add(clear);
p3.add(num7);
p3.add(num8);
p3.add(num9);
p3.add(bDiv);
p3.add(bSqrt);
p4.add(num4);
p4.add(num5);
p4.add(num6);
p4.add(bMul);
p4.add(bPer);
p5.add(num1);
p5.add(num2);
p5.add(num3);
p5.add(bSub);
p5.add(bFrac);
p6.add(num0);
p6.add(bInt);
p6.add(bDot);
p6.add(bAdd);
p6.add(equals);
p2.setLayout(new GridLayout (1, 3, 2, 2) );
p3.setLayout(new GridLayout (1, 3, 2, 2) );
p4.setLayout(new GridLayout (1, 3, 2, 2) );
p5.setLayout(new GridLayout (1, 3, 2, 2) );
p6.setLayout(new GridLayout (1, 3, 2, 2) );
f.setLayout(new GridLayout (6, 1) );
f.setResizable(false);
f.setSize(300,250);
f.add(tField);
f.add(p2);
f.add(p3);
f.add(p4);
f.add(p5);
f.add(p6);
f.setVisible(true);
f.setMenuBar(menuBar);
f.pack();
// ACTION LISTENERS
clear.addActionListener(this);
bCE.addActionListener(this);
num0.addActionListener(this);
num1.addActionListener(this);
num2.addActionListener(this);
num3.addActionListener(this);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
bAdd.addActionListener(this);
bSub.addActionListener(this);
bMul.addActionListener(this);
bDiv.addActionListener(this);
bPer.addActionListener(this);
bInt.addActionListener(this);
bSqrt.addActionListener(this);
bFrac.addActionListener(this);
bDot.addActionListener(this);
equals.addActionListener(this);
backspace.addActionListener(this);
fmi1.addActionListener(this);
fmi2.addActionListener(this);
fmi3.addActionListener(this);
}
/* |------------ START OF ACTION EVENTS ------------| */
public void actionPerformed(ActionEvent a){
try{
/* |-------- Handling Exceptions ---------| */
if(a.getSource()==num0){
value+=0;
tField.setText(value);
}
if(a.getSource()==num1){
value+=1;
tField.setText(value);
}
if(a.getSource()==num2){
value+=2;
tField.setText(value);
}
if(a.getSource()==num3){
value+=3;
tField.setText(value);
}
if(a.getSource()==num4){
value+=4;
tField.setText(value);
}
if(a.getSource()==num5){
value+=5;
tField.setText(value);
}
if(a.getSource()==num6){
value+=6;
tField.setText(value);
}
if(a.getSource()==num7){
value+=7;
tField.setText(value);
}
if(a.getSource()==num8){
value+=8;
tField.setText(value);
}
if(a.getSource()==num9){
value+=9;
tField.setText(value);
}
if (a.getSource() == bAdd){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = '+';
value="";
tField.setText("" +value);
}
if (a.getSource() == bSub){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = '-';
value="";
tField.setText("" +value);
}
if (a.getSource() == bMul){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = 'x';
value="";
tField.setText("" +value);
}
if (a.getSource() == bDiv){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
o = '/';
value="";
tField.setText("" +value);
}
if (a.getSource() == bPer){
v1 = Double.parseDouble( tField.getText() );
ctr=0;
value="";
answer = (v1/100);
tField.setText("" +answer);
}
/* |-- EQUALS ACTION --| */
if(a.getSource()==equals){
value="";
v2 = Double.parseDouble(tField.getText());
if(o=='+'){
ctr=0;
answer = v1 + v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='-'){
ctr=0;
answer = v1 - v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='x'){
ctr=0;
answer = v1 * v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='/'){
ctr=0;
answer = v1 / v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else if(o=='%'){
ctr=0;
answer = v1 % v2;
tField.setText("" +answer);
value=""; v1=null; v2=null;
}
else{}
}
/* |-- EQUALS ACTION --| */
/* |-- Clear --| */
if(a.getSource()==clear){
ctr=0;
v1=null;
v2=null;
value="";
answer=0.;
tField.setText("0.");
}
if(a.getSource()==bCE){
ctr=0;
value="";
tField.setText("0.");
}
/* |-- Point --| */
if(a.getSource() == bDot){
if(ctr==0){
value+=".";
ctr+=1;
tField.setText("" +value);
}
else{
System.out.print("");
}
}
/* |-- Back Space --| */
if(a.getSource() == backspace){
value = value.substring(0, value.length()-1 );
tField.setText("" +value);
}
/* |-- Square Root --| */
if(a.getSource() == bSqrt){
ctr=0;
value = "";
v1 = Math.sqrt( Double.parseDouble( tField.getText() ) );
tField.setText("" +v1);
}
/* |-- Integer --| */
if(a.getSource() == bInt){
ctr=0;
NumberConverted = ( Double.parseDouble(tField.getText()) * -1 );
value = "";
tField.setText("" +NumberConverted);
}
/* |-- Reciprocal --| */
if(a.getSource() == bFrac){
ctr=0;
value = "";
Double NumberContainer = ( 1 / Double.parseDouble(
tField.getText() ) );
tField.setText("" +NumberContainer);
}
// ------------ Menu Item Actions ------------ //
if(a.getSource() == fmi1){
cv = tField.getText();
}
if(a.getSource() == fmi2){
tField.setText("" +cv);
}
if(a.getSource() == fmi3){
System.exit(0);
}
} // End of Try
/* |-------- Attempting To Catch Runtime Errors ---------| */
catch(StringIndexOutOfBoundsException str){}
catch(NumberFormatException nfe){}
catch(NullPointerException npe){}
} // END OF ACTION EVENTS
public static void main (String args[]){
Calculator s = new Calculator();
s.launchFrame();
}
}

More Related Content

More from anonaeon

y+y=x It is a linear ordinary differential eqn of the Bernouille.pdf
y+y=x It is a linear ordinary differential eqn of the Bernouille.pdfy+y=x It is a linear ordinary differential eqn of the Bernouille.pdf
y+y=x It is a linear ordinary differential eqn of the Bernouille.pdf
anonaeon
 
Electrophilic addition - hydrogenation H2 Electro.pdf
                     Electrophilic addition - hydrogenation H2 Electro.pdf                     Electrophilic addition - hydrogenation H2 Electro.pdf
Electrophilic addition - hydrogenation H2 Electro.pdf
anonaeon
 
True Since E union Ec is the whole sample space which has probabilit.pdf
True Since E union Ec is the whole sample space which has probabilit.pdfTrue Since E union Ec is the whole sample space which has probabilit.pdf
True Since E union Ec is the whole sample space which has probabilit.pdf
anonaeon
 
There are many ethical and legal issues surrounding the US human ser.pdf
There are many ethical and legal issues surrounding the US human ser.pdfThere are many ethical and legal issues surrounding the US human ser.pdf
There are many ethical and legal issues surrounding the US human ser.pdf
anonaeon
 
The rituals and ceremony that follows the death of a person in order.pdf
The rituals and ceremony that follows the death of a person in order.pdfThe rituals and ceremony that follows the death of a person in order.pdf
The rituals and ceremony that follows the death of a person in order.pdf
anonaeon
 
The primary objectivegoal of this paper is to study the effect of w.pdf
The primary objectivegoal of this paper is to study the effect of w.pdfThe primary objectivegoal of this paper is to study the effect of w.pdf
The primary objectivegoal of this paper is to study the effect of w.pdf
anonaeon
 
Symmetry It is the property where parts of things resembles each o.pdf
Symmetry  It is the property where parts of things resembles each o.pdfSymmetry  It is the property where parts of things resembles each o.pdf
Symmetry It is the property where parts of things resembles each o.pdf
anonaeon
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
anonaeon
 
Slide to depict the basic layout of the pages in the website. Usiing.pdf
Slide to depict the basic layout of the pages in the website. Usiing.pdfSlide to depict the basic layout of the pages in the website. Usiing.pdf
Slide to depict the basic layout of the pages in the website. Usiing.pdf
anonaeon
 
People who are mentally and emotionally healthy haveSolutionP.pdf
People who are mentally and emotionally healthy haveSolutionP.pdfPeople who are mentally and emotionally healthy haveSolutionP.pdf
People who are mentally and emotionally healthy haveSolutionP.pdf
anonaeon
 
Part A-iv) Parietal CellsExplanation - The lining of the stoma.pdf
Part A-iv) Parietal CellsExplanation - The lining of the stoma.pdfPart A-iv) Parietal CellsExplanation - The lining of the stoma.pdf
Part A-iv) Parietal CellsExplanation - The lining of the stoma.pdf
anonaeon
 
Microplate capture and detection assay is used to detect the specifi.pdf
Microplate capture and detection assay is used to detect the specifi.pdfMicroplate capture and detection assay is used to detect the specifi.pdf
Microplate capture and detection assay is used to detect the specifi.pdf
anonaeon
 
C. Amide - Organic functional group found in Nylo.pdf
                     C. Amide - Organic functional group found in Nylo.pdf                     C. Amide - Organic functional group found in Nylo.pdf
C. Amide - Organic functional group found in Nylo.pdf
anonaeon
 
H2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdf
H2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdfH2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdf
H2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdf
anonaeon
 
First of all this program has compilation errors.Line 14 WilmaL.pdf
First of all this program has compilation errors.Line 14 WilmaL.pdfFirst of all this program has compilation errors.Line 14 WilmaL.pdf
First of all this program has compilation errors.Line 14 WilmaL.pdf
anonaeon
 
C and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdf
C and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdfC and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdf
C and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdf
anonaeon
 
Answer is,D. Greater activation of left hemisphere blood flow.Op.pdf
Answer is,D. Greater activation of left hemisphere blood flow.Op.pdfAnswer is,D. Greater activation of left hemisphere blood flow.Op.pdf
Answer is,D. Greater activation of left hemisphere blood flow.Op.pdf
anonaeon
 
Answer Real Estate Life Cycle is having 3 stages at Universal leve.pdf
Answer  Real Estate Life Cycle is having 3 stages at Universal leve.pdfAnswer  Real Estate Life Cycle is having 3 stages at Universal leve.pdf
Answer Real Estate Life Cycle is having 3 stages at Universal leve.pdf
anonaeon
 
2002ASolution2002A.pdf
2002ASolution2002A.pdf2002ASolution2002A.pdf
2002ASolution2002A.pdf
anonaeon
 
1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf
1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf
1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf
anonaeon
 

More from anonaeon (20)

y+y=x It is a linear ordinary differential eqn of the Bernouille.pdf
y+y=x It is a linear ordinary differential eqn of the Bernouille.pdfy+y=x It is a linear ordinary differential eqn of the Bernouille.pdf
y+y=x It is a linear ordinary differential eqn of the Bernouille.pdf
 
Electrophilic addition - hydrogenation H2 Electro.pdf
                     Electrophilic addition - hydrogenation H2 Electro.pdf                     Electrophilic addition - hydrogenation H2 Electro.pdf
Electrophilic addition - hydrogenation H2 Electro.pdf
 
True Since E union Ec is the whole sample space which has probabilit.pdf
True Since E union Ec is the whole sample space which has probabilit.pdfTrue Since E union Ec is the whole sample space which has probabilit.pdf
True Since E union Ec is the whole sample space which has probabilit.pdf
 
There are many ethical and legal issues surrounding the US human ser.pdf
There are many ethical and legal issues surrounding the US human ser.pdfThere are many ethical and legal issues surrounding the US human ser.pdf
There are many ethical and legal issues surrounding the US human ser.pdf
 
The rituals and ceremony that follows the death of a person in order.pdf
The rituals and ceremony that follows the death of a person in order.pdfThe rituals and ceremony that follows the death of a person in order.pdf
The rituals and ceremony that follows the death of a person in order.pdf
 
The primary objectivegoal of this paper is to study the effect of w.pdf
The primary objectivegoal of this paper is to study the effect of w.pdfThe primary objectivegoal of this paper is to study the effect of w.pdf
The primary objectivegoal of this paper is to study the effect of w.pdf
 
Symmetry It is the property where parts of things resembles each o.pdf
Symmetry  It is the property where parts of things resembles each o.pdfSymmetry  It is the property where parts of things resembles each o.pdf
Symmetry It is the property where parts of things resembles each o.pdf
 
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdfstruct procedure {    Date dateOfProcedure;    int procedureID.pdf
struct procedure {    Date dateOfProcedure;    int procedureID.pdf
 
Slide to depict the basic layout of the pages in the website. Usiing.pdf
Slide to depict the basic layout of the pages in the website. Usiing.pdfSlide to depict the basic layout of the pages in the website. Usiing.pdf
Slide to depict the basic layout of the pages in the website. Usiing.pdf
 
People who are mentally and emotionally healthy haveSolutionP.pdf
People who are mentally and emotionally healthy haveSolutionP.pdfPeople who are mentally and emotionally healthy haveSolutionP.pdf
People who are mentally and emotionally healthy haveSolutionP.pdf
 
Part A-iv) Parietal CellsExplanation - The lining of the stoma.pdf
Part A-iv) Parietal CellsExplanation - The lining of the stoma.pdfPart A-iv) Parietal CellsExplanation - The lining of the stoma.pdf
Part A-iv) Parietal CellsExplanation - The lining of the stoma.pdf
 
Microplate capture and detection assay is used to detect the specifi.pdf
Microplate capture and detection assay is used to detect the specifi.pdfMicroplate capture and detection assay is used to detect the specifi.pdf
Microplate capture and detection assay is used to detect the specifi.pdf
 
C. Amide - Organic functional group found in Nylo.pdf
                     C. Amide - Organic functional group found in Nylo.pdf                     C. Amide - Organic functional group found in Nylo.pdf
C. Amide - Organic functional group found in Nylo.pdf
 
H2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdf
H2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdfH2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdf
H2PO4^-ThereforeP has Oxidation state = +7H has oxidation stat.pdf
 
First of all this program has compilation errors.Line 14 WilmaL.pdf
First of all this program has compilation errors.Line 14 WilmaL.pdfFirst of all this program has compilation errors.Line 14 WilmaL.pdf
First of all this program has compilation errors.Line 14 WilmaL.pdf
 
C and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdf
C and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdfC and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdf
C and D. TCPIP, or Transmission Control ProtocolInternet Protocol,.pdf
 
Answer is,D. Greater activation of left hemisphere blood flow.Op.pdf
Answer is,D. Greater activation of left hemisphere blood flow.Op.pdfAnswer is,D. Greater activation of left hemisphere blood flow.Op.pdf
Answer is,D. Greater activation of left hemisphere blood flow.Op.pdf
 
Answer Real Estate Life Cycle is having 3 stages at Universal leve.pdf
Answer  Real Estate Life Cycle is having 3 stages at Universal leve.pdfAnswer  Real Estate Life Cycle is having 3 stages at Universal leve.pdf
Answer Real Estate Life Cycle is having 3 stages at Universal leve.pdf
 
2002ASolution2002A.pdf
2002ASolution2002A.pdf2002ASolution2002A.pdf
2002ASolution2002A.pdf
 
1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf
1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf
1.static int HORIZONTAL_WRAP2. Container view controllers are most.pdf
 

Code import java.awt.; import javax.swing.; import java.aw.pdf

  • 1. Code : import java.awt.*; import javax.swing.*; import java.awt.event.*; /*Designed and programmed by Srujana */ /*Designed and programmed by Srujana */ public class Calculator implements ActionListener{ char o; int ctr=0; String value="", cv="", oBtn; Double answer, v1, v2; Double NumberConverted; Frame f; Panel p1, p2, p3, p4, p5, p6; private TextField tField; private Menu EditMenu; private MenuBar menuBar; private MenuItem fmi1, fmi2, fmi3; private Button num0, num1, num2, num3, num4, num5, num6, num7, num8, num9; private Button bAdd, bSub, bMul, bDiv, bPer, bSqrt, bFrac, bInt, bDot, bCE, equals, backspace, clear; Calculator(){ f = new Frame("Calculator"); menuBar = new MenuBar(); EditMenu = new Menu ("Edit"); fmi1 = new MenuItem(" Copy "); fmi2 = new MenuItem(" Paste "); fmi3 = new MenuItem(" Quit "); EditMenu.add(fmi1); EditMenu.add(fmi2); EditMenu.addSeparator(); EditMenu.add(fmi3);
  • 2. p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); p5 = new Panel(); p6 = new Panel(); tField = new TextField(35); num0 = new Button ("0"); num1 = new Button ("1"); num2 = new Button ("2"); num3 = new Button ("3"); num4 = new Button ("4"); num5 = new Button ("5"); num6 = new Button ("6"); num7 = new Button ("7"); num8 = new Button ("8"); num9 = new Button ("9"); bAdd = new Button ("+"); bSub = new Button ("-"); bMul = new Button ("x"); bDiv = new Button ("/"); bPer = new Button ("%"); bSqrt = new Button ("sqrt"); bFrac = new Button ("1/x"); bInt = new Button ("+/-"); bDot = new Button ("."); bCE = new Button ("CE"); equals = new Button ("="); backspace = new Button ("Backspace"); clear = new Button ("C"); } public void launchFrame(){ tField.setText("0."); tField.setEnabled(false); menuBar.add(EditMenu); p2.add(backspace);
  • 3. p2.add(bCE); p2.add(clear); p3.add(num7); p3.add(num8); p3.add(num9); p3.add(bDiv); p3.add(bSqrt); p4.add(num4); p4.add(num5); p4.add(num6); p4.add(bMul); p4.add(bPer); p5.add(num1); p5.add(num2); p5.add(num3); p5.add(bSub); p5.add(bFrac); p6.add(num0); p6.add(bInt); p6.add(bDot); p6.add(bAdd); p6.add(equals); p2.setLayout(new GridLayout (1, 3, 2, 2) ); p3.setLayout(new GridLayout (1, 3, 2, 2) ); p4.setLayout(new GridLayout (1, 3, 2, 2) ); p5.setLayout(new GridLayout (1, 3, 2, 2) ); p6.setLayout(new GridLayout (1, 3, 2, 2) ); f.setLayout(new GridLayout (6, 1) ); f.setResizable(false); f.setSize(300,250); f.add(tField); f.add(p2); f.add(p3); f.add(p4); f.add(p5); f.add(p6);
  • 4. f.setVisible(true); f.setMenuBar(menuBar); f.pack(); // ACTION LISTENERS clear.addActionListener(this); bCE.addActionListener(this); num0.addActionListener(this); num1.addActionListener(this); num2.addActionListener(this); num3.addActionListener(this); num4.addActionListener(this); num5.addActionListener(this); num6.addActionListener(this); num7.addActionListener(this); num8.addActionListener(this); num9.addActionListener(this); bAdd.addActionListener(this); bSub.addActionListener(this); bMul.addActionListener(this); bDiv.addActionListener(this); bPer.addActionListener(this); bInt.addActionListener(this); bSqrt.addActionListener(this); bFrac.addActionListener(this); bDot.addActionListener(this); equals.addActionListener(this); backspace.addActionListener(this); fmi1.addActionListener(this); fmi2.addActionListener(this); fmi3.addActionListener(this); } /* |------------ START OF ACTION EVENTS ------------| */ public void actionPerformed(ActionEvent a){
  • 5. try{ /* |-------- Handling Exceptions ---------| */ if(a.getSource()==num0){ value+=0; tField.setText(value); } if(a.getSource()==num1){ value+=1; tField.setText(value); } if(a.getSource()==num2){ value+=2; tField.setText(value); } if(a.getSource()==num3){ value+=3; tField.setText(value); } if(a.getSource()==num4){ value+=4; tField.setText(value); } if(a.getSource()==num5){ value+=5; tField.setText(value); } if(a.getSource()==num6){ value+=6; tField.setText(value); } if(a.getSource()==num7){ value+=7; tField.setText(value); } if(a.getSource()==num8){
  • 6. value+=8; tField.setText(value); } if(a.getSource()==num9){ value+=9; tField.setText(value); } if (a.getSource() == bAdd){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = '+'; value=""; tField.setText("" +value); } if (a.getSource() == bSub){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = '-'; value=""; tField.setText("" +value); } if (a.getSource() == bMul){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = 'x'; value=""; tField.setText("" +value); } if (a.getSource() == bDiv){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = '/'; value=""; tField.setText("" +value); } if (a.getSource() == bPer){
  • 7. v1 = Double.parseDouble( tField.getText() ); ctr=0; value=""; answer = (v1/100); tField.setText("" +answer); } /* |-- EQUALS ACTION --| */ if(a.getSource()==equals){ value=""; v2 = Double.parseDouble(tField.getText()); if(o=='+'){ ctr=0; answer = v1 + v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='-'){ ctr=0; answer = v1 - v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='x'){ ctr=0; answer = v1 * v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='/'){ ctr=0; answer = v1 / v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='%'){ ctr=0;
  • 8. answer = v1 % v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else{} } /* |-- EQUALS ACTION --| */ /* |-- Clear --| */ if(a.getSource()==clear){ ctr=0; v1=null; v2=null; value=""; answer=0.; tField.setText("0."); } if(a.getSource()==bCE){ ctr=0; value=""; tField.setText("0."); } /* |-- Point --| */ if(a.getSource() == bDot){ if(ctr==0){ value+="."; ctr+=1; tField.setText("" +value); } else{ System.out.print(""); } } /* |-- Back Space --| */ if(a.getSource() == backspace){
  • 9. value = value.substring(0, value.length()-1 ); tField.setText("" +value); } /* |-- Square Root --| */ if(a.getSource() == bSqrt){ ctr=0; value = ""; v1 = Math.sqrt( Double.parseDouble( tField.getText() ) ); tField.setText("" +v1); } /* |-- Integer --| */ if(a.getSource() == bInt){ ctr=0; NumberConverted = ( Double.parseDouble(tField.getText()) * -1 ); value = ""; tField.setText("" +NumberConverted); } /* |-- Reciprocal --| */ if(a.getSource() == bFrac){ ctr=0; value = ""; Double NumberContainer = ( 1 / Double.parseDouble( tField.getText() ) ); tField.setText("" +NumberContainer); } // ------------ Menu Item Actions ------------ // if(a.getSource() == fmi1){ cv = tField.getText(); } if(a.getSource() == fmi2){ tField.setText("" +cv); } if(a.getSource() == fmi3){ System.exit(0); }
  • 10. } // End of Try /* |-------- Attempting To Catch Runtime Errors ---------| */ catch(StringIndexOutOfBoundsException str){} catch(NumberFormatException nfe){} catch(NullPointerException npe){} } // END OF ACTION EVENTS public static void main (String args[]){ Calculator s = new Calculator(); s.launchFrame(); } } Solution Code : import java.awt.*; import javax.swing.*; import java.awt.event.*; /*Designed and programmed by Srujana */ /*Designed and programmed by Srujana */ public class Calculator implements ActionListener{ char o; int ctr=0; String value="", cv="", oBtn; Double answer, v1, v2; Double NumberConverted; Frame f; Panel p1, p2, p3, p4, p5, p6; private TextField tField; private Menu EditMenu; private MenuBar menuBar; private MenuItem fmi1, fmi2, fmi3;
  • 11. private Button num0, num1, num2, num3, num4, num5, num6, num7, num8, num9; private Button bAdd, bSub, bMul, bDiv, bPer, bSqrt, bFrac, bInt, bDot, bCE, equals, backspace, clear; Calculator(){ f = new Frame("Calculator"); menuBar = new MenuBar(); EditMenu = new Menu ("Edit"); fmi1 = new MenuItem(" Copy "); fmi2 = new MenuItem(" Paste "); fmi3 = new MenuItem(" Quit "); EditMenu.add(fmi1); EditMenu.add(fmi2); EditMenu.addSeparator(); EditMenu.add(fmi3); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); p4 = new Panel(); p5 = new Panel(); p6 = new Panel(); tField = new TextField(35); num0 = new Button ("0"); num1 = new Button ("1"); num2 = new Button ("2"); num3 = new Button ("3"); num4 = new Button ("4"); num5 = new Button ("5"); num6 = new Button ("6"); num7 = new Button ("7"); num8 = new Button ("8"); num9 = new Button ("9"); bAdd = new Button ("+"); bSub = new Button ("-"); bMul = new Button ("x"); bDiv = new Button ("/");
  • 12. bPer = new Button ("%"); bSqrt = new Button ("sqrt"); bFrac = new Button ("1/x"); bInt = new Button ("+/-"); bDot = new Button ("."); bCE = new Button ("CE"); equals = new Button ("="); backspace = new Button ("Backspace"); clear = new Button ("C"); } public void launchFrame(){ tField.setText("0."); tField.setEnabled(false); menuBar.add(EditMenu); p2.add(backspace); p2.add(bCE); p2.add(clear); p3.add(num7); p3.add(num8); p3.add(num9); p3.add(bDiv); p3.add(bSqrt); p4.add(num4); p4.add(num5); p4.add(num6); p4.add(bMul); p4.add(bPer); p5.add(num1); p5.add(num2); p5.add(num3); p5.add(bSub); p5.add(bFrac); p6.add(num0); p6.add(bInt); p6.add(bDot); p6.add(bAdd);
  • 13. p6.add(equals); p2.setLayout(new GridLayout (1, 3, 2, 2) ); p3.setLayout(new GridLayout (1, 3, 2, 2) ); p4.setLayout(new GridLayout (1, 3, 2, 2) ); p5.setLayout(new GridLayout (1, 3, 2, 2) ); p6.setLayout(new GridLayout (1, 3, 2, 2) ); f.setLayout(new GridLayout (6, 1) ); f.setResizable(false); f.setSize(300,250); f.add(tField); f.add(p2); f.add(p3); f.add(p4); f.add(p5); f.add(p6); f.setVisible(true); f.setMenuBar(menuBar); f.pack(); // ACTION LISTENERS clear.addActionListener(this); bCE.addActionListener(this); num0.addActionListener(this); num1.addActionListener(this); num2.addActionListener(this); num3.addActionListener(this); num4.addActionListener(this); num5.addActionListener(this); num6.addActionListener(this); num7.addActionListener(this); num8.addActionListener(this); num9.addActionListener(this); bAdd.addActionListener(this); bSub.addActionListener(this); bMul.addActionListener(this); bDiv.addActionListener(this); bPer.addActionListener(this);
  • 14. bInt.addActionListener(this); bSqrt.addActionListener(this); bFrac.addActionListener(this); bDot.addActionListener(this); equals.addActionListener(this); backspace.addActionListener(this); fmi1.addActionListener(this); fmi2.addActionListener(this); fmi3.addActionListener(this); } /* |------------ START OF ACTION EVENTS ------------| */ public void actionPerformed(ActionEvent a){ try{ /* |-------- Handling Exceptions ---------| */ if(a.getSource()==num0){ value+=0; tField.setText(value); } if(a.getSource()==num1){ value+=1; tField.setText(value); } if(a.getSource()==num2){ value+=2; tField.setText(value); } if(a.getSource()==num3){ value+=3; tField.setText(value); } if(a.getSource()==num4){ value+=4;
  • 15. tField.setText(value); } if(a.getSource()==num5){ value+=5; tField.setText(value); } if(a.getSource()==num6){ value+=6; tField.setText(value); } if(a.getSource()==num7){ value+=7; tField.setText(value); } if(a.getSource()==num8){ value+=8; tField.setText(value); } if(a.getSource()==num9){ value+=9; tField.setText(value); } if (a.getSource() == bAdd){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = '+'; value=""; tField.setText("" +value); } if (a.getSource() == bSub){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = '-'; value=""; tField.setText("" +value); }
  • 16. if (a.getSource() == bMul){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = 'x'; value=""; tField.setText("" +value); } if (a.getSource() == bDiv){ v1 = Double.parseDouble( tField.getText() ); ctr=0; o = '/'; value=""; tField.setText("" +value); } if (a.getSource() == bPer){ v1 = Double.parseDouble( tField.getText() ); ctr=0; value=""; answer = (v1/100); tField.setText("" +answer); } /* |-- EQUALS ACTION --| */ if(a.getSource()==equals){ value=""; v2 = Double.parseDouble(tField.getText()); if(o=='+'){ ctr=0; answer = v1 + v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='-'){ ctr=0; answer = v1 - v2; tField.setText("" +answer); value=""; v1=null; v2=null;
  • 17. } else if(o=='x'){ ctr=0; answer = v1 * v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='/'){ ctr=0; answer = v1 / v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else if(o=='%'){ ctr=0; answer = v1 % v2; tField.setText("" +answer); value=""; v1=null; v2=null; } else{} } /* |-- EQUALS ACTION --| */ /* |-- Clear --| */ if(a.getSource()==clear){ ctr=0; v1=null; v2=null; value=""; answer=0.; tField.setText("0."); } if(a.getSource()==bCE){ ctr=0; value=""; tField.setText("0.");
  • 18. } /* |-- Point --| */ if(a.getSource() == bDot){ if(ctr==0){ value+="."; ctr+=1; tField.setText("" +value); } else{ System.out.print(""); } } /* |-- Back Space --| */ if(a.getSource() == backspace){ value = value.substring(0, value.length()-1 ); tField.setText("" +value); } /* |-- Square Root --| */ if(a.getSource() == bSqrt){ ctr=0; value = ""; v1 = Math.sqrt( Double.parseDouble( tField.getText() ) ); tField.setText("" +v1); } /* |-- Integer --| */ if(a.getSource() == bInt){ ctr=0; NumberConverted = ( Double.parseDouble(tField.getText()) * -1 ); value = ""; tField.setText("" +NumberConverted); } /* |-- Reciprocal --| */ if(a.getSource() == bFrac){ ctr=0; value = "";
  • 19. Double NumberContainer = ( 1 / Double.parseDouble( tField.getText() ) ); tField.setText("" +NumberContainer); } // ------------ Menu Item Actions ------------ // if(a.getSource() == fmi1){ cv = tField.getText(); } if(a.getSource() == fmi2){ tField.setText("" +cv); } if(a.getSource() == fmi3){ System.exit(0); } } // End of Try /* |-------- Attempting To Catch Runtime Errors ---------| */ catch(StringIndexOutOfBoundsException str){} catch(NumberFormatException nfe){} catch(NullPointerException npe){} } // END OF ACTION EVENTS public static void main (String args[]){ Calculator s = new Calculator(); s.launchFrame(); } }