SlideShare a Scribd company logo
1 of 11
Download to read offline
ANS:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame extends JFrame {
private double tn = 0;//here declare the variables
private double tn2 = 0;
private byte nom = -1;
private JTextField jtf;
public Frame() {
JButton[] bn = new JButton[10];
for ( int i = 9; i >= 0; i--) {
bn[i] = new JButton(Integer.toString(i));
}
//here declare the buttons
JButton eb= new JButton("e1");
JButton cb= new JButton("C");
JButton ab= new JButton("+");
JButton sb= new JButton("-");
JButton mb= new JButton("*");
JButton db= new JButton("/");
JButton ab= new JButton("+");
JButton sb= new JButton("-");
//here to set the size,bg,dimensionals
jtf = new JTextField();
jtf.setPreferredSize(new Dimension(180, 30));
jtf.setBackground(Color.BLUE);
jtf.setEnabled(false);
jtf.setHorizontalAlignment(4);
jtf.setDisabledTextColor(Color.GREEN);
//here set the layouts
JPanel PM = new JPanel();
PM.setLayout(new BoxLayout(PM, BoxLayout.Y_AXIS));
JPanel pt = new JPanel();
pt.setPreferredSize(new Dimension(160, 20));
pt.add(jtf);
JPanel pnb = new JPanel();
pnb.setPreferredSize(new Dimension(160, 100));
for(int i = 9; i>=0; i--) {
pnb.add(bn[i]);
}
//here buttons add to panels
JPanel pb_nom = new JPanel();
pb_nom.setPreferredSize(new Dimension(160, 35));
pb_nom.add(eb);
pb_nom.add(cb);
pb_nom.add(mb);
pb_nom.add(db);
pb_nom.add(ab);
pb_nom.add(sb);
umo_ji[] pb_action = new umo_ji[10];
for ( int i = 0; i < 10; i++ ) {
pb_action[i] = new umo_ji(bn[i]);
bn[i].addActionListener(pb_action[i]);
}
//here perform action buttons
eb e1 = new eb();
eb.addActionListener(e1);
cb c = new cb();
cb.addActionListener(c);
mb e2 = new mb();
mb.addActionListener(e2);
db e3 = new db();
db.addActionListener(e3);
ab add = new ab();
ab.addActionListener(add);
Sub_But subtract = new Sub_But();
sb.addActionListener(subtract);
PM.add(pt);
PM.add(pnb);
PM.add(pb_nom);
add(PM);
//To set the title and size
setTitle("Button_Test");
setSize(180, 290);
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private class umo_ji implements ActionListener {
//here implements actionlistener
private String c;
public umo_ji(JButton a) {
this.c = a.text_get();
}
public void actionPerformed(ActionEvent e) {
if (!jtf.text_get().equals("0.0")) {
jtf.text_set(jtf.text_get() + c);
} else {
jtf.text_set("");
actionPerformed(e);
}
}
}
private class eb implements ActionListener {
@Override//overide buttons
public void actionPerformed(ActionEvent e) {
tn2 = Double.parseDouble(jtf.text_get());
if (nom == 0) {
jtf.text_set(Double.toString((Math.round((tn / tn2) * 100)) / 100));
} else if (nom == 1) {
jtf.text_set(Double.toString(tn * tn2));
} else if (nom == 2) {
jtf.text_set(Double.toString(tn2 + tn));
} else if (nom == 3) {
jtf.text_set(Double.toString(tn - tn2));
} else {
jtf.text_set(String.valueOf(tn));
}
tn = Double.parseDouble(jtf.text_get());
}
}
private class cb implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
jtf.text_set("");
tn = 0;
tn2 = 0;
nom = -1;
}
}
private class db implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 0;
}
}
private class mb implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 1;
}
}
private class ab implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 2;
}
}
private class Sub_But implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 3;
}
}
}
Solution
ANS:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame extends JFrame {
private double tn = 0;//here declare the variables
private double tn2 = 0;
private byte nom = -1;
private JTextField jtf;
public Frame() {
JButton[] bn = new JButton[10];
for ( int i = 9; i >= 0; i--) {
bn[i] = new JButton(Integer.toString(i));
}
//here declare the buttons
JButton eb= new JButton("e1");
JButton cb= new JButton("C");
JButton ab= new JButton("+");
JButton sb= new JButton("-");
JButton mb= new JButton("*");
JButton db= new JButton("/");
JButton ab= new JButton("+");
JButton sb= new JButton("-");
//here to set the size,bg,dimensionals
jtf = new JTextField();
jtf.setPreferredSize(new Dimension(180, 30));
jtf.setBackground(Color.BLUE);
jtf.setEnabled(false);
jtf.setHorizontalAlignment(4);
jtf.setDisabledTextColor(Color.GREEN);
//here set the layouts
JPanel PM = new JPanel();
PM.setLayout(new BoxLayout(PM, BoxLayout.Y_AXIS));
JPanel pt = new JPanel();
pt.setPreferredSize(new Dimension(160, 20));
pt.add(jtf);
JPanel pnb = new JPanel();
pnb.setPreferredSize(new Dimension(160, 100));
for(int i = 9; i>=0; i--) {
pnb.add(bn[i]);
}
//here buttons add to panels
JPanel pb_nom = new JPanel();
pb_nom.setPreferredSize(new Dimension(160, 35));
pb_nom.add(eb);
pb_nom.add(cb);
pb_nom.add(mb);
pb_nom.add(db);
pb_nom.add(ab);
pb_nom.add(sb);
umo_ji[] pb_action = new umo_ji[10];
for ( int i = 0; i < 10; i++ ) {
pb_action[i] = new umo_ji(bn[i]);
bn[i].addActionListener(pb_action[i]);
}
//here perform action buttons
eb e1 = new eb();
eb.addActionListener(e1);
cb c = new cb();
cb.addActionListener(c);
mb e2 = new mb();
mb.addActionListener(e2);
db e3 = new db();
db.addActionListener(e3);
ab add = new ab();
ab.addActionListener(add);
Sub_But subtract = new Sub_But();
sb.addActionListener(subtract);
PM.add(pt);
PM.add(pnb);
PM.add(pb_nom);
add(PM);
//To set the title and size
setTitle("Button_Test");
setSize(180, 290);
setLocationByPlatform(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private class umo_ji implements ActionListener {
//here implements actionlistener
private String c;
public umo_ji(JButton a) {
this.c = a.text_get();
}
public void actionPerformed(ActionEvent e) {
if (!jtf.text_get().equals("0.0")) {
jtf.text_set(jtf.text_get() + c);
} else {
jtf.text_set("");
actionPerformed(e);
}
}
}
private class eb implements ActionListener {
@Override//overide buttons
public void actionPerformed(ActionEvent e) {
tn2 = Double.parseDouble(jtf.text_get());
if (nom == 0) {
jtf.text_set(Double.toString((Math.round((tn / tn2) * 100)) / 100));
} else if (nom == 1) {
jtf.text_set(Double.toString(tn * tn2));
} else if (nom == 2) {
jtf.text_set(Double.toString(tn2 + tn));
} else if (nom == 3) {
jtf.text_set(Double.toString(tn - tn2));
} else {
jtf.text_set(String.valueOf(tn));
}
tn = Double.parseDouble(jtf.text_get());
}
}
private class cb implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
jtf.text_set("");
tn = 0;
tn2 = 0;
nom = -1;
}
}
private class db implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 0;
}
}
private class mb implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 1;
}
}
private class ab implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 2;
}
}
private class Sub_But implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (tn == 0) {
tn = Double.parseDouble(jtf.text_get());
jtf.text_set("");
} else {
tn2 = Double.parseDouble(jtf.text_get());
jtf.text_set("");
}
nom = 3;
}
}
}

More Related Content

Similar to ANSimport javax.swing.;import java.awt.; import java.awt.ev.pdf

  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
sudhirchourasia86
 
package employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdfpackage employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdf
anwarsadath111
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
arihantcomp1008
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
aparnatiwari291
 

Similar to ANSimport javax.swing.;import java.awt.; import java.awt.ev.pdf (20)

Building Functional Islands
Building Functional IslandsBuilding Functional Islands
Building Functional Islands
 
Snake report ROHIT MALAV
Snake report ROHIT MALAVSnake report ROHIT MALAV
Snake report ROHIT MALAV
 
week-17x
week-17xweek-17x
week-17x
 
week-18x
week-18xweek-18x
week-18x
 
Java Week6(B) Notepad
Java Week6(B)   NotepadJava Week6(B)   Notepad
Java Week6(B) Notepad
 
4.3 Hibernate example.docx
4.3 Hibernate example.docx4.3 Hibernate example.docx
4.3 Hibernate example.docx
 
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
  package Chapter_20;import ToolKit.PostfixNotation;import javaf.pdf
 
Super TypeScript II Turbo - FP Remix (NG Conf 2017)
Super TypeScript II Turbo - FP Remix (NG Conf 2017)Super TypeScript II Turbo - FP Remix (NG Conf 2017)
Super TypeScript II Turbo - FP Remix (NG Conf 2017)
 
Javascript
JavascriptJavascript
Javascript
 
package employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdfpackage employeeType.employee;public class Employee {   private .pdf
package employeeType.employee;public class Employee {   private .pdf
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
Python From Scratch (1).pdf
Python From Scratch  (1).pdfPython From Scratch  (1).pdf
Python From Scratch (1).pdf
 
Question In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdfQuestion In C Programming In mathematics, a set is a colle...Sav.pdf
Question In C Programming In mathematics, a set is a colle...Sav.pdf
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
 
week-20x
week-20xweek-20x
week-20x
 
Awt
AwtAwt
Awt
 
MaintainStaffTable
MaintainStaffTableMaintainStaffTable
MaintainStaffTable
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 

More from anugrahafancy

Valence Bond Theory According to valence band the.pdf
                     Valence Bond Theory According to valence band the.pdf                     Valence Bond Theory According to valence band the.pdf
Valence Bond Theory According to valence band the.pdf
anugrahafancy
 
Some elements receive their respective symbol bas.pdf
                     Some elements receive their respective symbol bas.pdf                     Some elements receive their respective symbol bas.pdf
Some elements receive their respective symbol bas.pdf
anugrahafancy
 
There is a difference between to connect to a network and to communi.pdf
There is a difference between to connect to a network and to communi.pdfThere is a difference between to connect to a network and to communi.pdf
There is a difference between to connect to a network and to communi.pdf
anugrahafancy
 
Date Account title Debit credit jan 30 cash 45000 common sto.pdf
    Date Account title Debit credit   jan 30 cash 45000     common sto.pdf    Date Account title Debit credit   jan 30 cash 45000     common sto.pdf
Date Account title Debit credit jan 30 cash 45000 common sto.pdf
anugrahafancy
 
Initial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdf
Initial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdfInitial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdf
Initial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdf
anugrahafancy
 
IntroductionDarwin argused that living organisms evolved from one .pdf
IntroductionDarwin argused that living organisms evolved from one .pdfIntroductionDarwin argused that living organisms evolved from one .pdf
IntroductionDarwin argused that living organisms evolved from one .pdf
anugrahafancy
 

More from anugrahafancy (20)

X has +2 charge It has low first and second ioniz.pdf
                     X has +2 charge It has low first and second ioniz.pdf                     X has +2 charge It has low first and second ioniz.pdf
X has +2 charge It has low first and second ioniz.pdf
 
Valence Bond Theory According to valence band the.pdf
                     Valence Bond Theory According to valence band the.pdf                     Valence Bond Theory According to valence band the.pdf
Valence Bond Theory According to valence band the.pdf
 
total volume =25+25=50 ml total millimoles of KI=.pdf
                     total volume =25+25=50 ml total millimoles of KI=.pdf                     total volume =25+25=50 ml total millimoles of KI=.pdf
total volume =25+25=50 ml total millimoles of KI=.pdf
 
This type of deformation is irreversible. However.pdf
                     This type of deformation is irreversible. However.pdf                     This type of deformation is irreversible. However.pdf
This type of deformation is irreversible. However.pdf
 
stereoisomers .pdf
                     stereoisomers                                    .pdf                     stereoisomers                                    .pdf
stereoisomers .pdf
 
Some elements receive their respective symbol bas.pdf
                     Some elements receive their respective symbol bas.pdf                     Some elements receive their respective symbol bas.pdf
Some elements receive their respective symbol bas.pdf
 
phosphonium ylide is produced when phosphonium ha.pdf
                     phosphonium ylide is produced when phosphonium ha.pdf                     phosphonium ylide is produced when phosphonium ha.pdf
phosphonium ylide is produced when phosphonium ha.pdf
 
N1 V1 = N2 V2 50 x M1 = 1.50 x 2 x 17.7 M M1 = 1..pdf
                     N1 V1 = N2 V2 50 x M1 = 1.50 x 2 x 17.7 M M1 = 1..pdf                     N1 V1 = N2 V2 50 x M1 = 1.50 x 2 x 17.7 M M1 = 1..pdf
N1 V1 = N2 V2 50 x M1 = 1.50 x 2 x 17.7 M M1 = 1..pdf
 
In chemistry, an amphoteric species is a molecule.pdf
                     In chemistry, an amphoteric species is a molecule.pdf                     In chemistry, an amphoteric species is a molecule.pdf
In chemistry, an amphoteric species is a molecule.pdf
 
Chemistry is the science of matter, especially it.pdf
                     Chemistry is the science of matter, especially it.pdf                     Chemistry is the science of matter, especially it.pdf
Chemistry is the science of matter, especially it.pdf
 
Before this confederates began their general advance, General Lee pl.pdf
Before this confederates began their general advance, General Lee pl.pdfBefore this confederates began their general advance, General Lee pl.pdf
Before this confederates began their general advance, General Lee pl.pdf
 
An understanding of periodic trends is important .pdf
                     An understanding of periodic trends is important .pdf                     An understanding of periodic trends is important .pdf
An understanding of periodic trends is important .pdf
 
Acid contains hydrogen ions. Thus, iodate will re.pdf
                     Acid contains hydrogen ions. Thus, iodate will re.pdf                     Acid contains hydrogen ions. Thus, iodate will re.pdf
Acid contains hydrogen ions. Thus, iodate will re.pdf
 
There is a difference between to connect to a network and to communi.pdf
There is a difference between to connect to a network and to communi.pdfThere is a difference between to connect to a network and to communi.pdf
There is a difference between to connect to a network and to communi.pdf
 
The picture is not uploaded properly it is corrupted. please upload .pdf
The picture is not uploaded properly it is corrupted. please upload .pdfThe picture is not uploaded properly it is corrupted. please upload .pdf
The picture is not uploaded properly it is corrupted. please upload .pdf
 
No clarity in question . Not visibleSolutionNo clarity in ques.pdf
No clarity in question . Not visibleSolutionNo clarity in ques.pdfNo clarity in question . Not visibleSolutionNo clarity in ques.pdf
No clarity in question . Not visibleSolutionNo clarity in ques.pdf
 
Date Account title Debit credit jan 30 cash 45000 common sto.pdf
    Date Account title Debit credit   jan 30 cash 45000     common sto.pdf    Date Account title Debit credit   jan 30 cash 45000     common sto.pdf
Date Account title Debit credit jan 30 cash 45000 common sto.pdf
 
Initial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdf
Initial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdfInitial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdf
Initial Value=2.5Drift rate per annum=0.4Variance rate per annum.pdf
 
IntroductionDarwin argused that living organisms evolved from one .pdf
IntroductionDarwin argused that living organisms evolved from one .pdfIntroductionDarwin argused that living organisms evolved from one .pdf
IntroductionDarwin argused that living organisms evolved from one .pdf
 
import java.awt.LinearGradientPaint; public class searchComparis.pdf
import java.awt.LinearGradientPaint; public class searchComparis.pdfimport java.awt.LinearGradientPaint; public class searchComparis.pdf
import java.awt.LinearGradientPaint; public class searchComparis.pdf
 

Recently uploaded

Recently uploaded (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 

ANSimport javax.swing.;import java.awt.; import java.awt.ev.pdf

  • 1. ANS: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Frame extends JFrame { private double tn = 0;//here declare the variables private double tn2 = 0; private byte nom = -1; private JTextField jtf; public Frame() { JButton[] bn = new JButton[10]; for ( int i = 9; i >= 0; i--) { bn[i] = new JButton(Integer.toString(i)); } //here declare the buttons JButton eb= new JButton("e1"); JButton cb= new JButton("C"); JButton ab= new JButton("+"); JButton sb= new JButton("-"); JButton mb= new JButton("*"); JButton db= new JButton("/"); JButton ab= new JButton("+"); JButton sb= new JButton("-"); //here to set the size,bg,dimensionals jtf = new JTextField(); jtf.setPreferredSize(new Dimension(180, 30)); jtf.setBackground(Color.BLUE); jtf.setEnabled(false); jtf.setHorizontalAlignment(4); jtf.setDisabledTextColor(Color.GREEN); //here set the layouts JPanel PM = new JPanel(); PM.setLayout(new BoxLayout(PM, BoxLayout.Y_AXIS));
  • 2. JPanel pt = new JPanel(); pt.setPreferredSize(new Dimension(160, 20)); pt.add(jtf); JPanel pnb = new JPanel(); pnb.setPreferredSize(new Dimension(160, 100)); for(int i = 9; i>=0; i--) { pnb.add(bn[i]); } //here buttons add to panels JPanel pb_nom = new JPanel(); pb_nom.setPreferredSize(new Dimension(160, 35)); pb_nom.add(eb); pb_nom.add(cb); pb_nom.add(mb); pb_nom.add(db); pb_nom.add(ab); pb_nom.add(sb); umo_ji[] pb_action = new umo_ji[10]; for ( int i = 0; i < 10; i++ ) { pb_action[i] = new umo_ji(bn[i]); bn[i].addActionListener(pb_action[i]); } //here perform action buttons eb e1 = new eb(); eb.addActionListener(e1); cb c = new cb(); cb.addActionListener(c); mb e2 = new mb(); mb.addActionListener(e2); db e3 = new db(); db.addActionListener(e3); ab add = new ab(); ab.addActionListener(add); Sub_But subtract = new Sub_But(); sb.addActionListener(subtract); PM.add(pt);
  • 3. PM.add(pnb); PM.add(pb_nom); add(PM); //To set the title and size setTitle("Button_Test"); setSize(180, 290); setLocationByPlatform(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } private class umo_ji implements ActionListener { //here implements actionlistener private String c; public umo_ji(JButton a) { this.c = a.text_get(); } public void actionPerformed(ActionEvent e) { if (!jtf.text_get().equals("0.0")) { jtf.text_set(jtf.text_get() + c); } else { jtf.text_set(""); actionPerformed(e); } } } private class eb implements ActionListener { @Override//overide buttons public void actionPerformed(ActionEvent e) { tn2 = Double.parseDouble(jtf.text_get()); if (nom == 0) { jtf.text_set(Double.toString((Math.round((tn / tn2) * 100)) / 100)); } else if (nom == 1) { jtf.text_set(Double.toString(tn * tn2)); } else if (nom == 2) { jtf.text_set(Double.toString(tn2 + tn)); } else if (nom == 3) {
  • 4. jtf.text_set(Double.toString(tn - tn2)); } else { jtf.text_set(String.valueOf(tn)); } tn = Double.parseDouble(jtf.text_get()); } } private class cb implements ActionListener { @Override public void actionPerformed(ActionEvent e) { jtf.text_set(""); tn = 0; tn2 = 0; nom = -1; } } private class db implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 0; } } private class mb implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get());
  • 5. jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 1; } } private class ab implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 2; } } private class Sub_But implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 3; } }
  • 6. } Solution ANS: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Frame extends JFrame { private double tn = 0;//here declare the variables private double tn2 = 0; private byte nom = -1; private JTextField jtf; public Frame() { JButton[] bn = new JButton[10]; for ( int i = 9; i >= 0; i--) { bn[i] = new JButton(Integer.toString(i)); } //here declare the buttons JButton eb= new JButton("e1"); JButton cb= new JButton("C"); JButton ab= new JButton("+"); JButton sb= new JButton("-"); JButton mb= new JButton("*"); JButton db= new JButton("/"); JButton ab= new JButton("+"); JButton sb= new JButton("-"); //here to set the size,bg,dimensionals jtf = new JTextField(); jtf.setPreferredSize(new Dimension(180, 30)); jtf.setBackground(Color.BLUE); jtf.setEnabled(false); jtf.setHorizontalAlignment(4); jtf.setDisabledTextColor(Color.GREEN);
  • 7. //here set the layouts JPanel PM = new JPanel(); PM.setLayout(new BoxLayout(PM, BoxLayout.Y_AXIS)); JPanel pt = new JPanel(); pt.setPreferredSize(new Dimension(160, 20)); pt.add(jtf); JPanel pnb = new JPanel(); pnb.setPreferredSize(new Dimension(160, 100)); for(int i = 9; i>=0; i--) { pnb.add(bn[i]); } //here buttons add to panels JPanel pb_nom = new JPanel(); pb_nom.setPreferredSize(new Dimension(160, 35)); pb_nom.add(eb); pb_nom.add(cb); pb_nom.add(mb); pb_nom.add(db); pb_nom.add(ab); pb_nom.add(sb); umo_ji[] pb_action = new umo_ji[10]; for ( int i = 0; i < 10; i++ ) { pb_action[i] = new umo_ji(bn[i]); bn[i].addActionListener(pb_action[i]); } //here perform action buttons eb e1 = new eb(); eb.addActionListener(e1); cb c = new cb(); cb.addActionListener(c); mb e2 = new mb(); mb.addActionListener(e2); db e3 = new db(); db.addActionListener(e3); ab add = new ab(); ab.addActionListener(add);
  • 8. Sub_But subtract = new Sub_But(); sb.addActionListener(subtract); PM.add(pt); PM.add(pnb); PM.add(pb_nom); add(PM); //To set the title and size setTitle("Button_Test"); setSize(180, 290); setLocationByPlatform(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } private class umo_ji implements ActionListener { //here implements actionlistener private String c; public umo_ji(JButton a) { this.c = a.text_get(); } public void actionPerformed(ActionEvent e) { if (!jtf.text_get().equals("0.0")) { jtf.text_set(jtf.text_get() + c); } else { jtf.text_set(""); actionPerformed(e); } } } private class eb implements ActionListener { @Override//overide buttons public void actionPerformed(ActionEvent e) { tn2 = Double.parseDouble(jtf.text_get()); if (nom == 0) { jtf.text_set(Double.toString((Math.round((tn / tn2) * 100)) / 100)); } else if (nom == 1) { jtf.text_set(Double.toString(tn * tn2));
  • 9. } else if (nom == 2) { jtf.text_set(Double.toString(tn2 + tn)); } else if (nom == 3) { jtf.text_set(Double.toString(tn - tn2)); } else { jtf.text_set(String.valueOf(tn)); } tn = Double.parseDouble(jtf.text_get()); } } private class cb implements ActionListener { @Override public void actionPerformed(ActionEvent e) { jtf.text_set(""); tn = 0; tn2 = 0; nom = -1; } } private class db implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 0; } } private class mb implements ActionListener { @Override
  • 10. public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 1; } } private class ab implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } nom = 2; } } private class Sub_But implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (tn == 0) { tn = Double.parseDouble(jtf.text_get()); jtf.text_set(""); } else { tn2 = Double.parseDouble(jtf.text_get()); jtf.text_set(""); }