SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Downloaden Sie, um offline zu lesen
JAVA WEEK7
/*
Week 7 :
Write a Java program that works as a simple calculator.
Use a grid layout to arrange buttons for the digits and
for the +, -,*, % operations. Add a text field to display
the result.
*/

import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;
/*
< applet code= LabPro14 Width=300 Height=300 >
< /applet >
*/

public class LabPro14 extends Applet implements ActionListener
{
        TextField t;String a;int p=0,tmp=0;
        Button bo,b1,b2,b3,b4,b5,b6,b7,b8,b9;
        Button badd,bsub,bmul,bdiv,bper,beql,bc;
        public void init()
        {
        t = new TextField(50);
        bo = new Button("0");
        b1 = new Button("1");
        b2 = new Button("2");
        b3 = new Button("3");
        b4 = new Button("4");
        b5 = new Button("5");
        b6 = new Button("6");
        b7 = new Button("7");
        b8 = new Button("8");
        b9 = new Button("9");
        badd = new Button("+");
        bsub = new Button("-");
        bmul = new Button("*");
        bdiv = new Button("/");
        bper = new Button("%");
        bc = new Button("c");
        beql = new Button("=");
        add(t);add(bo);add(b1);
        add(b2);add(b3);add(b4);
        add(b5);add(b6);add(b7);
        add(b8);add(b9);add(badd);
        add(bsub);add(bmul);add(bdiv);
        add(bper);add(bc);add(beql);
        bo.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        badd.addActionListener(this);
        bsub.addActionListener(this);
        bmul.addActionListener(this);
        bdiv.addActionListener(this);
        bper.addActionListener(this);
                                       Page 1
JAVA WEEK7
bc.addActionListener(this);
beql.addActionListener(this);
//setLayout(new GridLayout(4,4));
}
public void actionPerformed(ActionEvent ae)
{
  if(ae.getSource()==bc)
    {
  t.setText("0");
}
  if(ae.getSource()==bo)
{
  int k=Integer.parseInt(t.getText());
    k=k*10+0;
    t.setText(String.valueOf(k));
  }
  if(ae.getSource()==b1)
    {
  int k=Integer.parseInt(t.getText());
  k=k*10+1;
  t.setText(String.valueOf(k));
  }
if(ae.getSource()==b2)
  {
  int k=Integer.parseInt(t.getText());
  k=k*10+2;
  t.setText(String.valueOf(k));
}
if(ae.getSource()==b3)
{
  int k=Integer.parseInt(t.getText());
  k=k*10+3;
  t.setText(String.valueOf(k));
}
if(ae.getSource()==b4)
{
int k=Integer.parseInt(t.getText());
k=k*10+4;
t.setText(String.valueOf(k));
  }
if(ae.getSource()==b5)
{
int k=Integer.parseInt(t.getText());
  k=k*10+5;
t.setText(String.valueOf(k));
}
  if(ae.getSource()==b6)
  {
  int k=Integer.parseInt(t.getText());
k=k*10+6;
t.setText(String.valueOf(k));
}
  if(ae.getSource()==b7)
  {
int k=Integer.parseInt(t.getText());
  k=k*10+7;
  t.setText(String.valueOf(k));
  }
if(ae.getSource()==b8)
  {
int k=Integer.parseInt(t.getText());
  k=k*10+8;
  t.setText(String.valueOf(k));
                               Page 2
JAVA WEEK7
    }
     if(ae.getSource()==b9)
    {
      int k=Integer.parseInt(t.getText());
      k=k*10+9;
    t.setText(String.valueOf(k));
    }
         if(ae.getSource()==badd)
    {
    tmp=Integer.parseInt(t.getText());
    p=1;
      t.setText("0");
      }
    if(ae.getSource()==bsub)
    {
      tmp=Integer.parseInt(t.getText());
      p=2;
      t.setText("0");
      }
    if(ae.getSource()==bmul)
    {
      tmp=Integer.parseInt(t.getText());
        p=3;
      t.setText("0");
    }
    if(ae.getSource()==bdiv)
    {
    tmp=Integer.parseInt(t.getText());
      p=4;
      t.setText("0");
    }
    if(ae.getSource()==bper)
    {
    tmp=Integer.parseInt(t.getText());
      p=5;
      t.setText("0");
    }
    if(ae.getSource()==beql)
      {
      float newval=Integer.parseInt(t.getText());
    float res=0;
      switch(p)
      {
               case 1:
              res=tmp+newval;
               break;
               case 2:
        res=tmp-newval;
              break;
              case 3:
               res=tmp*newval;
              break;
               case 4:
              res=tmp/newval;
              break;
    case 5:
    res=tmp%newval;
    break;
      }
      t.setText(String.valueOf(res));
    }
}
}
                                   Page 3

Weitere ähnliche Inhalte

Was ist angesagt?

Programa para leer letras
Programa para leer letrasPrograma para leer letras
Programa para leer letrasFacebook
 
Object Oriented Programing - Generic Programing
Object Oriented Programing - Generic ProgramingObject Oriented Programing - Generic Programing
Object Oriented Programing - Generic ProgramingBayu Firmawan Paoh
 
Python codigo graficas
Python codigo graficasPython codigo graficas
Python codigo graficasBrayan Kalaka
 
Tugas kelompok 3.3
Tugas kelompok 3.3Tugas kelompok 3.3
Tugas kelompok 3.31300018075
 
[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - templateSeok-joon Yun
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеPlatonov Sergey
 
Tugas kelompok 3.2
Tugas kelompok 3.2Tugas kelompok 3.2
Tugas kelompok 3.21300018075
 
I will be callback/JS同步與非同步
I will be callback/JS同步與非同步I will be callback/JS同步與非同步
I will be callback/JS同步與非同步ZenChou2
 
Java script.trend(spec)
Java script.trend(spec)Java script.trend(spec)
Java script.trend(spec)dynamis
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jqueryAdam Zygadlewicz
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 

Was ist angesagt? (18)

Zadatak
ZadatakZadatak
Zadatak
 
Dij
DijDij
Dij
 
Programa para leer letras
Programa para leer letrasPrograma para leer letras
Programa para leer letras
 
Object Oriented Programing - Generic Programing
Object Oriented Programing - Generic ProgramingObject Oriented Programing - Generic Programing
Object Oriented Programing - Generic Programing
 
Python codigo graficas
Python codigo graficasPython codigo graficas
Python codigo graficas
 
Tugas kelompok 3.3
Tugas kelompok 3.3Tugas kelompok 3.3
Tugas kelompok 3.3
 
[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template[KOSSA] C++ Programming - 14th Study - template
[KOSSA] C++ Programming - 14th Study - template
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
 
Tugas kelompok 3.2
Tugas kelompok 3.2Tugas kelompok 3.2
Tugas kelompok 3.2
 
Recursion prog (1)
Recursion prog (1)Recursion prog (1)
Recursion prog (1)
 
I will be callback/JS同步與非同步
I will be callback/JS同步與非同步I will be callback/JS同步與非同步
I will be callback/JS同步與非同步
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Java script.trend(spec)
Java script.trend(spec)Java script.trend(spec)
Java script.trend(spec)
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquery
 
Lector
LectorLector
Lector
 
Img1
Img1Img1
Img1
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
Stack demo - copy
 Stack demo - copy Stack demo - copy
Stack demo - copy
 

Andere mochten auch

Dr. William Kritsonis, Dissertation Chair for Dr. Clarence Johnson
Dr. William Kritsonis, Dissertation Chair for Dr. Clarence JohnsonDr. William Kritsonis, Dissertation Chair for Dr. Clarence Johnson
Dr. William Kritsonis, Dissertation Chair for Dr. Clarence Johnsonguestfa49ec
 
Journals Listings 2010
Journals  Listings   2010Journals  Listings   2010
Journals Listings 2010guestfa49ec
 
Dr. Robert L. Marshall & Ben C. DeSpain
Dr. Robert L. Marshall & Ben C. DeSpainDr. Robert L. Marshall & Ben C. DeSpain
Dr. Robert L. Marshall & Ben C. DeSpainguestfa49ec
 
Dr. William Kritsonis, Dissertation Chair
Dr. William Kritsonis, Dissertation ChairDr. William Kritsonis, Dissertation Chair
Dr. William Kritsonis, Dissertation Chairguestfa49ec
 
Dr. Kritsonis & Jalanda Greene
Dr. Kritsonis & Jalanda GreeneDr. Kritsonis & Jalanda Greene
Dr. Kritsonis & Jalanda Greeneguestfa49ec
 
Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...
Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...
Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...guestfa49ec
 
Dissertation Chair Dr. William Allan Kritsonis & Steven Norfleet
Dissertation Chair Dr. William Allan Kritsonis & Steven NorfleetDissertation Chair Dr. William Allan Kritsonis & Steven Norfleet
Dissertation Chair Dr. William Allan Kritsonis & Steven Norfleetguestfa49ec
 

Andere mochten auch (8)

Dr. William Kritsonis, Dissertation Chair for Dr. Clarence Johnson
Dr. William Kritsonis, Dissertation Chair for Dr. Clarence JohnsonDr. William Kritsonis, Dissertation Chair for Dr. Clarence Johnson
Dr. William Kritsonis, Dissertation Chair for Dr. Clarence Johnson
 
Alvaro Ortiz Works
Alvaro Ortiz WorksAlvaro Ortiz Works
Alvaro Ortiz Works
 
Journals Listings 2010
Journals  Listings   2010Journals  Listings   2010
Journals Listings 2010
 
Dr. Robert L. Marshall & Ben C. DeSpain
Dr. Robert L. Marshall & Ben C. DeSpainDr. Robert L. Marshall & Ben C. DeSpain
Dr. Robert L. Marshall & Ben C. DeSpain
 
Dr. William Kritsonis, Dissertation Chair
Dr. William Kritsonis, Dissertation ChairDr. William Kritsonis, Dissertation Chair
Dr. William Kritsonis, Dissertation Chair
 
Dr. Kritsonis & Jalanda Greene
Dr. Kritsonis & Jalanda GreeneDr. Kritsonis & Jalanda Greene
Dr. Kritsonis & Jalanda Greene
 
Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...
Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...
Greene J., & Kritsonis, W.A. A Snap Shot Investigation of the Academic Achiev...
 
Dissertation Chair Dr. William Allan Kritsonis & Steven Norfleet
Dissertation Chair Dr. William Allan Kritsonis & Steven NorfleetDissertation Chair Dr. William Allan Kritsonis & Steven Norfleet
Dissertation Chair Dr. William Allan Kritsonis & Steven Norfleet
 

Mehr von Chaitanya Rajkumar Limmala (16)

Java notes
Java notesJava notes
Java notes
 
Java Week9(B) Notepad
Java Week9(B)   NotepadJava Week9(B)   Notepad
Java Week9(B) Notepad
 
Java Week6(B) Notepad
Java Week6(B)   NotepadJava Week6(B)   Notepad
Java Week6(B) Notepad
 
Java Week4(B) Notepad
Java Week4(B)   NotepadJava Week4(B)   Notepad
Java Week4(B) Notepad
 
Java Week8(A) Notepad
Java Week8(A)   NotepadJava Week8(A)   Notepad
Java Week8(A) Notepad
 
Java Week3(A) Notepad
Java Week3(A)   NotepadJava Week3(A)   Notepad
Java Week3(A) Notepad
 
Java Week10 Notepad
Java Week10   NotepadJava Week10   Notepad
Java Week10 Notepad
 
Java Week9(A) Notepad
Java Week9(A)   NotepadJava Week9(A)   Notepad
Java Week9(A) Notepad
 
Java Week6(A) Notepad
Java Week6(A)   NotepadJava Week6(A)   Notepad
Java Week6(A) Notepad
 
Java Week4(A) Notepad
Java Week4(A)   NotepadJava Week4(A)   Notepad
Java Week4(A) Notepad
 
Java Week4(C) Notepad
Java Week4(C)   NotepadJava Week4(C)   Notepad
Java Week4(C) Notepad
 
Java Week1(B) Notepad
Java Week1(B)   NotepadJava Week1(B)   Notepad
Java Week1(B) Notepad
 
Java Week2(C) Notepad
Java Week2(C)   NotepadJava Week2(C)   Notepad
Java Week2(C) Notepad
 
Java Week2(B) Notepad
Java Week2(B)   NotepadJava Week2(B)   Notepad
Java Week2(B) Notepad
 
Java Week1(A) Notepad
Java Week1(A)   NotepadJava Week1(A)   Notepad
Java Week1(A) Notepad
 
Java Week2(A) Notepad
Java Week2(A)   NotepadJava Week2(A)   Notepad
Java Week2(A) Notepad
 

Java Week7 Notepad

  • 1. JAVA WEEK7 /* Week 7 : Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. */ import java.awt.event.*; import java.applet.Applet; import java.awt.*; /* < applet code= LabPro14 Width=300 Height=300 > < /applet > */ public class LabPro14 extends Applet implements ActionListener { TextField t;String a;int p=0,tmp=0; Button bo,b1,b2,b3,b4,b5,b6,b7,b8,b9; Button badd,bsub,bmul,bdiv,bper,beql,bc; public void init() { t = new TextField(50); bo = new Button("0"); b1 = new Button("1"); b2 = new Button("2"); b3 = new Button("3"); b4 = new Button("4"); b5 = new Button("5"); b6 = new Button("6"); b7 = new Button("7"); b8 = new Button("8"); b9 = new Button("9"); badd = new Button("+"); bsub = new Button("-"); bmul = new Button("*"); bdiv = new Button("/"); bper = new Button("%"); bc = new Button("c"); beql = new Button("="); add(t);add(bo);add(b1); add(b2);add(b3);add(b4); add(b5);add(b6);add(b7); add(b8);add(b9);add(badd); add(bsub);add(bmul);add(bdiv); add(bper);add(bc);add(beql); bo.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); badd.addActionListener(this); bsub.addActionListener(this); bmul.addActionListener(this); bdiv.addActionListener(this); bper.addActionListener(this); Page 1
  • 2. JAVA WEEK7 bc.addActionListener(this); beql.addActionListener(this); //setLayout(new GridLayout(4,4)); } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==bc) { t.setText("0"); } if(ae.getSource()==bo) { int k=Integer.parseInt(t.getText()); k=k*10+0; t.setText(String.valueOf(k)); } if(ae.getSource()==b1) { int k=Integer.parseInt(t.getText()); k=k*10+1; t.setText(String.valueOf(k)); } if(ae.getSource()==b2) { int k=Integer.parseInt(t.getText()); k=k*10+2; t.setText(String.valueOf(k)); } if(ae.getSource()==b3) { int k=Integer.parseInt(t.getText()); k=k*10+3; t.setText(String.valueOf(k)); } if(ae.getSource()==b4) { int k=Integer.parseInt(t.getText()); k=k*10+4; t.setText(String.valueOf(k)); } if(ae.getSource()==b5) { int k=Integer.parseInt(t.getText()); k=k*10+5; t.setText(String.valueOf(k)); } if(ae.getSource()==b6) { int k=Integer.parseInt(t.getText()); k=k*10+6; t.setText(String.valueOf(k)); } if(ae.getSource()==b7) { int k=Integer.parseInt(t.getText()); k=k*10+7; t.setText(String.valueOf(k)); } if(ae.getSource()==b8) { int k=Integer.parseInt(t.getText()); k=k*10+8; t.setText(String.valueOf(k)); Page 2
  • 3. JAVA WEEK7 } if(ae.getSource()==b9) { int k=Integer.parseInt(t.getText()); k=k*10+9; t.setText(String.valueOf(k)); } if(ae.getSource()==badd) { tmp=Integer.parseInt(t.getText()); p=1; t.setText("0"); } if(ae.getSource()==bsub) { tmp=Integer.parseInt(t.getText()); p=2; t.setText("0"); } if(ae.getSource()==bmul) { tmp=Integer.parseInt(t.getText()); p=3; t.setText("0"); } if(ae.getSource()==bdiv) { tmp=Integer.parseInt(t.getText()); p=4; t.setText("0"); } if(ae.getSource()==bper) { tmp=Integer.parseInt(t.getText()); p=5; t.setText("0"); } if(ae.getSource()==beql) { float newval=Integer.parseInt(t.getText()); float res=0; switch(p) { case 1: res=tmp+newval; break; case 2: res=tmp-newval; break; case 3: res=tmp*newval; break; case 4: res=tmp/newval; break; case 5: res=tmp%newval; break; } t.setText(String.valueOf(res)); } } } Page 3