import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
public class assign23 extends Applet{
	
	private JButton ac, mod, clear,decimal,plus,minus,mult,divide,equals;
	private JButton b[] = new JButton[10];
	Font myfont=new Font("",Font.BOLD,20);
	
	String ops[]= new String[2];
	int i=0;
	String op="";
	float r,v1,v2;
	
	private Panel insidePanel1= new Panel();
	private Panel insidePanel2= new Panel();
	private Panel insidePanel3= new Panel();
	private Panel insidePanel4= new Panel();
	private Panel insidePanel5= new Panel();
	
	private JTextField disp=new JTextField("0");
	public void init(){
		int cols=4,gap=4;
		
		this.setLayout(new GridLayout(6,1,gap,gap));
		
		insidePanel1.setLayout(new GridLayout(1,cols,gap,gap));
		insidePanel2.setLayout(new GridLayout(1,cols,gap,gap));
		insidePanel3.setLayout(new GridLayout(1,cols,gap,gap));
		insidePanel4.setLayout(new GridLayout(1,cols,gap,gap));
		insidePanel5.setLayout(new GridLayout(1,3,gap,gap));
		this.add(disp);
		this.add(insidePanel1);
		this.add(insidePanel2);
		this.add(insidePanel3);
		this.add(insidePanel4);
		this.add(insidePanel5);
		
		disp.setEditable(false);
		disp.setBackground(Color.WHITE);
		disp.setHorizontalAlignment(SwingConstants.RIGHT);
		disp.setFont(myfont);
		
		b[9]=new JButton("9");
		b[8]=new JButton("8");
		b[7]=new JButton("7");
		divide=new JButton("/");
		b[9].setFont(myfont);
		insidePanel1.add(b[9]);
		b[8].setFont(myfont);
		insidePanel1.add(b[8]);
		b[7].setFont(myfont);
		insidePanel1.add(b[7]);
		divide.setFont(myfont);
		insidePanel1.add(divide);
		
		b[6]=new JButton("6");
		b[5]=new JButton("5");
		b[4]=new JButton("4");
		mult=new JButton("*");
		b[6].setFont(myfont);
		insidePanel2.add(b[6]);
		b[5].setFont(myfont);
		insidePanel2.add(b[5]);
		b[4].setFont(myfont);
		insidePanel2.add(b[4]);
		mult.setFont(myfont);
		insidePanel2.add(mult);
		
		b[3]=new JButton("3");
		b[2]=new JButton("2");
		b[1]=new JButton("1");
		minus=new JButton("-");
		b[3].setFont(myfont);
		insidePanel3.add(b[3]);
		b[2].setFont(myfont);
		insidePanel3.add(b[2]);
		b[1].setFont(myfont);
		insidePanel3.add(b[1]);
		minus.setFont(myfont);
		insidePanel3.add(minus);
		
		b[0]=new JButton("0");
		decimal=new JButton(".");
		mod=new JButton("Mod");
		plus=new JButton("+");
		b[0].setFont(myfont);
		insidePanel4.add(b[0]);
		decimal.setFont(myfont);
		insidePanel4.add(decimal);
		mod.setFont(myfont);
		insidePanel4.add(mod);
		plus.setFont(myfont);
		insidePanel4.add(plus);
		
			ac=new JButton("AC");
			clear=new JButton("Clear");
			equals=new JButton("=");
			ac.setFont(myfont);
			insidePanel5.add(ac);
			clear.setFont(myfont);
			insidePanel5.add(clear);
			equals.setFont(myfont);
			insidePanel5.add(equals);	
			
			handler hand = new handler();
			for(int i=0;i<10;i++){
				b[i].addActionListener(hand);
				b[i].addKeyListener(hand);
			}
			plus.addActionListener(hand);
			minus.addActionListener(hand);
			mult.addActionListener(hand);
			divide.addActionListener(hand);
			equals.addActionListener(hand);
			clear.addActionListener(hand);
			ac.addActionListener(hand);
			decimal.addActionListener(hand);
			mod.addActionListener(hand);
			
			this.addKeyListener(hand);
			plus.addKeyListener(hand);
			minus.addKeyListener(hand);
			mult.addKeyListener(hand);
			divide.addKeyListener(hand);
			equals.addKeyListener(hand);
			clear.addKeyListener(hand);
			ac.addKeyListener(hand);
			decimal.addKeyListener(hand);
			mod.addKeyListener(hand);
			disp.addKeyListener(hand);
			insidePanel1.addKeyListener(hand);
			insidePanel2.addKeyListener(hand);
			insidePanel3.addKeyListener(hand);
			insidePanel4.addKeyListener(hand);
			insidePanel5.addKeyListener(hand);
			
	}

	class handler implements ActionListener,KeyListener{
		public void actionPerformed(ActionEvent event) {
			showStatus("Calculator. Created by Amit.");
			String str=event.getActionCommand();
			
			
			if(numchk(str)){
				ops[i]=disp.getText()+str;
			disp.setText(ops[i]);
			}else if(str.equals(".")){
					dot();
				}else if(str.equals("+")){
				add();
			}else if(str.equals("-")){
				subtract();
			}else if(str.equals("*")){
				multiply();
			}else if(str.equals("/")){
				div();
			}else if(str.equals("Mod")){
				modulus();
			}else if(str.equals("AC")){
				aclear();
			}else if(str.equals("Clear")){
				clear();
			}else if(str.equals("=")){
				equals();
			}	
		}
		
			public void add(){
				try{
				disp.setText("+ ");
				if(i>0){
					 v1=Float.parseFloat(ops[0]);
					 ops[1]=ops[1].substring(2);
					 v2=Float.parseFloat(ops[1]);
					switch(op){
					case "+":
					r=v1+v2;
					break;
					case "-":
						r=v1-v2;
						break;
					case "*":
						r=v1*v2;
						break;
					case "/":
						try{
						r=v1/v2;}catch(Exception e){disp.setText("Math Error");
						i=0;}
						break;
					case "mod":
						try{
						r=v1%v2;}catch(Exception e){disp.setText("Math Error");
						i=0;}
						break;
					}
					ops[0]=String.valueOf(r);
				}
					i=1;
				op="+";
				}catch(Exception e){
					disp.setText("Syntax Error");
				}
			}
			
		public void subtract(){
			try{
			disp.setText("- ");
			if(i>0){
				 v1=Float.parseFloat(ops[0]);
				 ops[1]=ops[1].substring(2);
				 v2=Float.parseFloat(ops[1]);
				switch(op){
				case "+":
				r=v1+v2;
				break;
				case "-":
					r=v1-v2;
					break;
				case "*":
					r=v1*v2;
					break;
				case "/":
					try{
					r=v1/v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				case "mod":
					try{
					r=v1%v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				}
				ops[0]=String.valueOf(r);
			}
				i=1;
			op="-";
			}catch(Exception e){
				disp.setText("Syntax Error");
			}
		}
		public void multiply(){
			try{
			disp.setText("* ");
			if(i>0){
				 v1=Float.parseFloat(ops[0]);
				 ops[1]=ops[1].substring(2);
				 v2=Float.parseFloat(ops[1]);
				switch(op){
				case "+":
				r=v1+v2;
				break;
				case "-":
					r=v1-v2;
					break;
				case "*":
					r=v1*v2;
					break;
				case "/":
					try{
					r=v1/v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				case "mod":
					try{
					r=v1%v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				}
				ops[0]=String.valueOf(r);
			}
				i=1;
			op="*";
			}catch(Exception e){
				disp.setText("Syntax Error");
			}
		}
		public void div(){
			try{
			disp.setText("/ ");
			if(i>0){
				 v1=Float.parseFloat(ops[0]);
				 ops[1]=ops[1].substring(2);
				 v2=Float.parseFloat(ops[1]);
				switch(op){
				case "+":
				r=v1+v2;
				break;
				case "-":
					r=v1-v2;
					break;
				case "*":
					r=v1*v2;
					break;
				case "/":
					try{
					r=v1/v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				case "mod":
					try{
					r=v1%v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				}
				ops[0]=String.valueOf(r);
			}
				i=1;
			op="/";
			}catch(Exception e){
				disp.setText("Syntax Error");
			}
		}
		public void equals(){
			try{
			disp.setText("");
			if(i>0){
				float v1=Float.parseFloat(ops[0]);
				ops[1]=ops[1].substring(1);
				float v2=Float.parseFloat(ops[1]);
				switch(op){
				case "+":
				r=v1+v2;
				break;
				case "-":
					r=v1-v2;
					break;
				case "*":
					r=v1*v2;
					break;
				case "/":
					try{
					r=v1/v2;}catch(Exception e){disp.setText("Math Error");}
					break;
				case "mod":
					try{
					r=v1%v2;}catch(Exception e){disp.setText("Math Error");}
					break;
				}
				ops[0]=String.valueOf(r);
			}
			if(numchk(ops[0])==false)
				disp.setText("Math Error");
			disp.setText(ops[0]);
			i=0;
			}catch(Exception e){
				disp.setText("Syntax Error");
			}
		}
		public void modulus(){
			try{
			disp.setText("% ");
			if(i>0){
				 v1=Float.parseFloat(ops[0]);
				 ops[1]=ops[1].substring(2);
				 v2=Float.parseFloat(ops[1]);
				switch(op){
				case "+":
				r=v1+v2;
				break;
				case "-":
					r=v1-v2;
					break;
				case "*":
					r=v1*v2;
					break;
				case "/":
					try{
					r=v1/v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				case "mod":
					try{
					r=v1%v2;}catch(Exception e){disp.setText("Math Error");
					i=0;}
					break;
				}
				ops[0]=String.valueOf(r);
			}
				i=1;
			op="mod";
		}catch(Exception e){
			disp.setText("Syntax Error");
		}
		}
		public void aclear(){
			disp.setText("0");
			ops[0]=ops[1]=op="";
			i=0;
		}
		public void clear(){
			if(i==0)
				ops[0]="";
			else if(i==1){
				ops[1]=disp.getText();
				ops[1]=ops[1].substring(0,1);
		}
		disp.setText(ops[i]+" ");	
		}
		
		public boolean numchk(String input){
			try{
				Float.parseFloat(input);
				return true;
			}catch(Exception e){
				return false;
			}
		}
		public void dot(){
			String temp=disp.getText();
			boolean chk=dotchk(temp);
			if(chk){
				temp+=".";
				ops[i]=temp;
				disp.setText(temp);
			}
		}
	public boolean dotchk(String sen){
		char[] s=sen.toCharArray();
		int count=0;
		for(int i=0;i<sen.length();i++){
			if(s[i]=='.')
				count++;
		}
		if(count>0)
			return false;
		else
			return true;
	}
		
		public void keyPressed(KeyEvent event) {
			if(event.getKeyCode()==KeyEvent.VK_ENTER)
				equals();
			else if(event.getKeyCode()==KeyEvent.VK_BACK_SPACE)
				clear();
			else if(event.getKeyCode()==KeyEvent.VK_DELETE)
				aclear();
			else if(event.getKeyCode()==KeyEvent.VK_ADD)
				add();
			else if(event.getKeyCode()==KeyEvent.VK_SUBTRACT||event.getKeyCode()==KeyEvent.VK_MINUS)
				subtract();
			else if(event.getKeyCode()==KeyEvent.VK_MULTIPLY)
				multiply();
			else if(event.getKeyCode()==KeyEvent.VK_DIVIDE)
				div();
			
		}
		public void keyReleased(KeyEvent arg0) {
		}
		public void keyTyped(KeyEvent event) {
			showStatus("Calculator. Created by Amit.");
			char typ=event.getKeyChar();
			String type=String.valueOf(typ);
			if(numchk(type)){
				ops[i]=disp.getText()+type;
				disp.setText(ops[i]);
			}
			if(type.equals(".")){
				dot();
			}
		}
	}
	
	
}