From ae2f628e0c4eb830c94dd3209d57cc1f9e60ee3b Mon Sep 17 00:00:00 2001 From: pareshapraj <72244369+pareshapraj@users.noreply.github.com> Date: Thu, 7 Oct 2021 14:03:36 +0530 Subject: [PATCH 1/2] Create MyCalculator.java We can develop calculator in java with the help of AWT/Swing with event handling. Let's see the code of creating calculator in java --- MyCalculator.java | 320 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 320 insertions(+) create mode 100644 MyCalculator.java diff --git a/MyCalculator.java b/MyCalculator.java new file mode 100644 index 0000000..f68cac5 --- /dev/null +++ b/MyCalculator.java @@ -0,0 +1,320 @@ +import java.awt.*; +import java.awt.event.*; +/*********************************************/ + +public class MyCalculator extends Frame +{ + +public boolean setClear=true; +double number, memValue; +char op; + +String digitButtonText[] = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "+/-", "." }; +String operatorButtonText[] = {"/", "sqrt", "*", "%", "-", "1/X", "+", "=" }; +String memoryButtonText[] = {"MC", "MR", "MS", "M+" }; +String specialButtonText[] = {"Backspc", "C", "CE" }; + +MyDigitButton digitButton[]=new MyDigitButton[digitButtonText.length]; +MyOperatorButton operatorButton[]=new MyOperatorButton[operatorButtonText.length]; +MyMemoryButton memoryButton[]=new MyMemoryButton[memoryButtonText.length]; +MySpecialButton specialButton[]=new MySpecialButton[specialButtonText.length]; + +Label displayLabel=new Label("0",Label.RIGHT); +Label memLabel=new Label(" ",Label.RIGHT); + +final int FRAME_WIDTH=325,FRAME_HEIGHT=325; +final int HEIGHT=30, WIDTH=30, H_SPACE=10,V_SPACE=10; +final int TOPX=30, TOPY=50; +/////////////////////////// +MyCalculator(String frameText)//constructor +{ +super(frameText); + +int tempX=TOPX, y=TOPY; +displayLabel.setBounds(tempX,y,240,HEIGHT); +displayLabel.setBackground(Color.BLUE); +displayLabel.setForeground(Color.WHITE); +add(displayLabel); + +memLabel.setBounds(TOPX, TOPY+HEIGHT+ V_SPACE,WIDTH, HEIGHT); +add(memLabel); + +// set Co-ordinates for Memory Buttons +tempX=TOPX; +y=TOPY+2*(HEIGHT+V_SPACE); +for(int i=0; i0) + resText=resText.substring(0,resText.length()-2); +return resText; +} +//////////////////////////////////////// +public static void main(String []args) +{ +new MyCalculator("Calculator - JavaTpoint"); +} +} + +/*******************************************/ + +class MyDigitButton extends Button implements ActionListener +{ +MyCalculator cl; + +////////////////////////////////////////// +MyDigitButton(int x,int y, int width,int height,String cap, MyCalculator clc) +{ +super(cap); +setBounds(x,y,width,height); +this.cl=clc; +this.cl.add(this); +addActionListener(this); +} +//////////////////////////////////////////////// +static boolean isInString(String s, char ch) +{ +for(int i=0; i Date: Tue, 12 Oct 2021 22:14:50 +0530 Subject: [PATCH 2/2] Create OnlineTest.java --- OnlineTest.java | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 OnlineTest.java diff --git a/OnlineTest.java b/OnlineTest.java new file mode 100644 index 0000000..1c89a8d --- /dev/null +++ b/OnlineTest.java @@ -0,0 +1,187 @@ +/*Online Java Paper Test*/ + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +class OnlineTest extends JFrame implements ActionListener +{ + JLabel l; + JRadioButton jb[]=new JRadioButton[5]; + JButton b1,b2; + ButtonGroup bg; + int count=0,current=0,x=1,y=1,now=0; + int m[]=new int[10]; + OnlineTest(String s) + { + super(s); + l=new JLabel(); + add(l); + bg=new ButtonGroup(); + for(int i=0;i<5;i++) + { + jb[i]=new JRadioButton(); + add(jb[i]); + bg.add(jb[i]); + } + b1=new JButton("Next"); + b2=new JButton("Bookmark"); + b1.addActionListener(this); + b2.addActionListener(this); + add(b1);add(b2); + set(); + l.setBounds(30,40,450,20); + jb[0].setBounds(50,80,100,20); + jb[1].setBounds(50,110,100,20); + jb[2].setBounds(50,140,100,20); + jb[3].setBounds(50,170,100,20); + b1.setBounds(100,240,100,30); + b2.setBounds(270,240,100,30); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(null); + setLocation(250,100); + setVisible(true); + setSize(600,350); + } + public void actionPerformed(ActionEvent e) + { + if(e.getSource()==b1) + { + if(check()) + count=count+1; + current++; + set(); + if(current==9) + { + b1.setEnabled(false); + b2.setText("Result"); + } + } + if(e.getActionCommand().equals("Bookmark")) + { + JButton bk=new JButton("Bookmark"+x); + bk.setBounds(480,20+30*x,100,30); + add(bk); + bk.addActionListener(this); + m[x]=current; + x++; + current++; + set(); + if(current==9) + b2.setText("Result"); + setVisible(false); + setVisible(true); + } + for(int i=0,y=1;i