import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class cal extends JFrame implements ActionListener
{
	JMenuBar mb;
	JMenu file, help;
	JMenuItem esc, about;
    JButton btn[];
    JLabel lab1 = new JLabel("0");
	JPanel pan1;
	int i;
	final int MAX_INPUT_LENGTH = 20;
	final int INPUT_MODE = 0;
	final int RESULT_MODE = 1;
	final int ERROR_MODE = 2;
	int displayMode;

	boolean clearOnNextDigit;
	double lastNumber;
	String lastOperator;
	
	public cal()
	{
      		super("Calculator");
      		setLayout(new BorderLayout());

           	//creating the menu bar
      		mb = new JMenuBar();
      		setJMenuBar(mb);
      		
      		file = new JMenu("File");
      		esc = new JMenuItem("Exit");
      		file.add(esc);
      		
      		help = new JMenu("Help");
      		about = new JMenuItem("About");
      		help.add(about);
      		
      		mb.add(file);
      		mb.add(help);
      		
      		esc.addActionListener(this);
       	    about.addActionListener(this);
            
		    lab1.setBackground(Color.WHITE);
	        lab1.setOpaque(true);
		    add(lab1,BorderLayout.NORTH);
		    
		    pan1 = new JPanel(new GridLayout(5,4,2,3));
		    btn = new JButton[20];
			     
		    // Create numeric Jbuttons
		    for(i = 0; i < 10 ; i++)
		    {
		    	btn[i] = new JButton(String.valueOf(i));
		    }
		    btn[10] = new JButton("+/-");
		    btn[11] = new JButton(".");
		    btn[12] = new JButton("=");
		    btn[13] = new JButton("/");
		    btn[14] = new JButton("*");
		    btn[15] = new JButton("-");
		    btn[16] = new JButton("+");
		    btn[17] = new JButton("BS");
		    btn[18] = new JButton("C");
		    btn[19] = new JButton("CE");
		    
		    for (i=0; i < btn.length; i++)
		     {
			// set each Jbutton label to the value of index
		      btn[i].addActionListener(this);
		      pan1.add(btn[i]);
		     }
		     add(pan1,BorderLayout.CENTER);
		     
		     for(i=0 ; i < btn.length; i++)
		     {
		     	if(i < 10)
		     	{
		     		btn[i].setBackground(Color.LIGHT_GRAY);
		     		btn[i].setForeground(new Color(64,128,128));
		     	}
		     	else
		     	{
		     		btn[i].setBackground(Color.GRAY);
		     		btn[i].setForeground(Color.BLACK);
		     	}
		     }
		//Add buttons to keypad panel starting at top left
		// First row BS,CE,C,Equal
		     pan1.add(btn[17]);
	       	 pan1.add(btn[19]);
		     pan1.add(btn[18]);
		     pan1.add(btn[12]);
		//second row 1,2,3,Plus
		     for(i=1; i<=3; i++)		
			  {
			    pan1.add(btn[i]);
		      }
		     pan1.add(btn[16]);
		     for(i=4; i<=6; i++)
		      {
			   pan1.add(btn[i]);
		      }
		       pan1.add(btn[15]);
		// Third row 4,5,6,Subtract
		     for(i=7; i<=9; i++)
		      {
			   pan1.add(btn[i]);
		      }  
		       pan1.add(btn[14]);
		//Fourth Row 0,ChangeSign,DecimalPoint,Division
		       pan1.add(btn[0]);
		       pan1.add(btn[10]);
		       pan1.add(btn[11]);
		       pan1.add(btn[13]);	         

      		 setSize(225,269);
      	     pack();
      		 setLocation(150,150);
      		 setVisible(true);
      		 setResizable(false);
      		 setDefaultCloseOperation(3);  		
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		double resulr = 0;
		if(ae.getSource() == esc)
			System.exit(0);
		else if(ae.getSource() == about)
			JOptionPane.showMessageDialog(null,"This is My First Project in OOP II",""
				                          ,JOptionPane.INFORMATION_MESSAGE);
   
        	for(i = 0; i < btn.length; i++)
        	{
        	  if(ae.getSource() == btn[i])
        	  	switch(i)
        	  		{
        	  		case 0:
						addDigitToDisplay(i);
						break;

					case 1:
						addDigitToDisplay(i);
						break;

					case 2:
						addDigitToDisplay(i);
						break;

					case 3:
						addDigitToDisplay(i);
						break;

					case 4:
						addDigitToDisplay(i);
						break;

					case 5:
						addDigitToDisplay(i);
						break;

					case 6:
						addDigitToDisplay(i);
						break;

					case 7:
						addDigitToDisplay(i);
						break;

					case 8:
						addDigitToDisplay(i);
						break;

					case 9:
						addDigitToDisplay(i);
						break;
					
					case 10:	// ChangeSign
                        processSignChange();
						break;

					case 11:	// Decimal point
						addDecimalPoint();
						break;

					case 12:	// Equal
						processEquals();
						break;

					case 13:	// Divide
						processOperator("/");
						break;

					case 14:	// Multiply
						processOperator("*");
						break;

					case 15:	// Minus
						processOperator("-");
						break;

					case 16:	// plus
						processOperator("+");
						break;
        	  		
					case 17:	// Backspace
						if (displayMode != ERROR_MODE)
							{
							setDisplayString(getDisplayString().substring(0,getDisplayString().length() - 1));
							
							if (getDisplayString().length() < 1)
								setDisplayString("0");
						    }
						break;

					case 18:	// CE
						clearExisting();
						break;

					case 19:	// C
						clearAll();
						break;
        	  	    }
        	  	   
        	}	


	}

	void setDisplayString(String s)
		{
		lab1.setText(s);
	    }

	String getDisplayString ()
		{
		return lab1.getText();
	    }

	void addDigitToDisplay(int digit)
		{
		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();
		
		if (inputString.indexOf("0") == 0)
			{
			inputString = inputString.substring(1);
		    }

		if ((!inputString.equals("0") || digit > 0) &&(inputString.length() < MAX_INPUT_LENGTH))
			{
			setDisplayString(inputString + digit);
		    }
		displayMode = INPUT_MODE;
		clearOnNextDigit = false;
	}

	void addDecimalPoint()
		{
		displayMode = INPUT_MODE;

		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();
	
		// If the input string already contains a decimal point, don't
		//  do anything to it.
		if (inputString.indexOf(".") < 0)
			setDisplayString(new String(inputString + "."));
	   }

	void processSignChange()
		{
		if (displayMode == INPUT_MODE)
		{
			String input = getDisplayString();

			if (input.length() > 0 && !input.equals("0"))
			{
				if (input.indexOf("-") == 0)
					setDisplayString(input.substring(1));

				else
					setDisplayString("-" + input);
			}
			
		}

		else if (displayMode == RESULT_MODE)
		{
			double numberInDisplay = getNumberInDisplay();
		
			if (numberInDisplay != 0)
				displayResult(-numberInDisplay);
		}
	}

	void clearAll()	
		{
		setDisplayString("0");
		lastOperator = "0";
		lastNumber = 0;
		displayMode = INPUT_MODE;
		clearOnNextDigit = true;
	    }

	void clearExisting()
		{
		setDisplayString("0");
		clearOnNextDigit = true;
		displayMode = INPUT_MODE;
	    }

	double getNumberInDisplay()	
		{
		String input = lab1.getText();
		return Double.parseDouble(input);
    	}

	void processOperator(String op) 
	    {
			double numberInDisplay = getNumberInDisplay();
			lastNumber = numberInDisplay;	
			clearOnNextDigit = true;
			lastOperator = op;
		}

	void processEquals()
		{
		double result = 0;

		if (displayMode != ERROR_MODE)
			{
			try			
			{
				result = processLastOperator();
				displayResult(result);
			}
			
			catch (DivideByZeroException e)	{
				displayError("Cannot divide by zero!");
			}

			lastOperator = lastOperator;
		}
	}

	double processLastOperator() throws DivideByZeroException 
		{
		double result = 0;
		double numberInDisplay = getNumberInDisplay();

		if (lastOperator.equals("/"))
		{
			if (numberInDisplay == 0)
				throw (new DivideByZeroException());
			result = lastNumber / numberInDisplay;
		}
			
		if (lastOperator.equals("*"))
			result = lastNumber * numberInDisplay;

		if (lastOperator.equals("-"))
			result = lastNumber - numberInDisplay;

		if (lastOperator.equals("+"))
			result = lastNumber + numberInDisplay;

		return result;
	    }

	void displayResult(double result)
		{
		setDisplayString(Double.toString(result));
		lastNumber = result;
		displayMode = RESULT_MODE;
		clearOnNextDigit = true;
	    }

	void displayError(String errorMessage){
		setDisplayString(errorMessage);
		lastNumber = 0;
		displayMode = ERROR_MODE;
		clearOnNextDigit = true;
	}
	public static void main(String arg[])
	{
		new cal();
	}
    }

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class cal extends JFrame implements ActionListener
{
	JMenuBar mb;
	JMenu file, help;
	JMenuItem esc, about;
    JButton btn[];
    JLabel lab1 = new JLabel("0");
	JPanel pan1;
	int i;
	final int MAX_INPUT_LENGTH = 20;
	final int INPUT_MODE = 0;
	final int RESULT_MODE = 1;
	final int ERROR_MODE = 2;
	int displayMode;

	boolean clearOnNextDigit;
	double lastNumber;
	String lastOperator;
	
	public cal()
	{
      		super("Calculator");
      		setLayout(new BorderLayout());

           	//creating the menu bar
      		mb = new JMenuBar();
      		setJMenuBar(mb);
      		
      		file = new JMenu("File");
      		esc = new JMenuItem("Exit");
      		file.add(esc);
      		
      		help = new JMenu("Help");
      		about = new JMenuItem("About");
      		help.add(about);
      		
      		mb.add(file);
      		mb.add(help);
      		
      		esc.addActionListener(this);
       	    about.addActionListener(this);
            
		    lab1.setBackground(Color.WHITE);
	        lab1.setOpaque(true);
		    add(lab1,BorderLayout.NORTH);
		    
		    pan1 = new JPanel(new GridLayout(5,4,2,3));
		    btn = new JButton[20];
			     
		    // Create numeric Jbuttons
		    for(i = 0; i < 10 ; i++)
		    {
		    	btn[i] = new JButton(String.valueOf(i));
		    }
		    btn[10] = new JButton("+/-");
		    btn[11] = new JButton(".");
		    btn[12] = new JButton("=");
		    btn[13] = new JButton("/");
		    btn[14] = new JButton("*");
		    btn[15] = new JButton("-");
		    btn[16] = new JButton("+");
		    btn[17] = new JButton("BS");
		    btn[18] = new JButton("C");
		    btn[19] = new JButton("CE");
		    
		    for (i=0; i < btn.length; i++)
		     {
			// set each Jbutton label to the value of index
		      btn[i].addActionListener(this);
		      pan1.add(btn[i]);
		     }
		     add(pan1,BorderLayout.CENTER);
		     
		     for(i=0 ; i < btn.length; i++)
		     {
		     	if(i < 10)
		     	{
		     		btn[i].setBackground(Color.LIGHT_GRAY);
		     		btn[i].setForeground(new Color(64,128,128));
		     	}
		     	else
		     	{
		     		btn[i].setBackground(Color.GRAY);
		     		btn[i].setForeground(Color.BLACK);
		     	}
		     }
		//Add buttons to keypad panel starting at top left
		// First row BS,CE,C,Equal
		     pan1.add(btn[17]);
	       	 pan1.add(btn[19]);
		     pan1.add(btn[18]);
		     pan1.add(btn[12]);
		//second row 1,2,3,Plus
		     for(i=1; i<=3; i++)		
			  {
			    pan1.add(btn[i]);
		      }
		     pan1.add(btn[16]);
		     for(i=4; i<=6; i++)
		      {
			   pan1.add(btn[i]);
		      }
		       pan1.add(btn[15]);
		// Third row 4,5,6,Subtract
		     for(i=7; i<=9; i++)
		      {
			   pan1.add(btn[i]);
		      }  
		       pan1.add(btn[14]);
		//Fourth Row 0,ChangeSign,DecimalPoint,Division
		       pan1.add(btn[0]);
		       pan1.add(btn[10]);
		       pan1.add(btn[11]);
		       pan1.add(btn[13]);	         

      		 setSize(225,269);
      	     pack();
      		 setLocation(150,150);
      		 setVisible(true);
      		 setResizable(false);
      		 setDefaultCloseOperation(3);  		
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		double resulr = 0;
		if(ae.getSource() == esc)
			System.exit(0);
		else if(ae.getSource() == about)
			JOptionPane.showMessageDialog(null,"This is My First Project in OOP II",""
				                          ,JOptionPane.INFORMATION_MESSAGE);
   
        	for(i = 0; i < btn.length; i++)
        	{
        	  if(ae.getSource() == btn[i])
        	  	switch(i)
        	  		{
        	  		case 0:
						addDigitToDisplay(i);
						break;

					case 1:
						addDigitToDisplay(i);
						break;

					case 2:
						addDigitToDisplay(i);
						break;

					case 3:
						addDigitToDisplay(i);
						break;

					case 4:
						addDigitToDisplay(i);
						break;

					case 5:
						addDigitToDisplay(i);
						break;

					case 6:
						addDigitToDisplay(i);
						break;

					case 7:
						addDigitToDisplay(i);
						break;

					case 8:
						addDigitToDisplay(i);
						break;

					case 9:
						addDigitToDisplay(i);
						break;
					
					case 10:	// ChangeSign
                        processSignChange();
						break;

					case 11:	// Decimal point
						addDecimalPoint();
						break;

					case 12:	// Equal
						processEquals();
						break;

					case 13:	// Divide
						processOperator("/");
						break;

					case 14:	// Multiply
						processOperator("*");
						break;

					case 15:	// Minus
						processOperator("-");
						break;

					case 16:	// plus
						processOperator("+");
						break;
        	  		
					case 17:	// Backspace
						if (displayMode != ERROR_MODE)
							{
							setDisplayString(getDisplayString().substring(0,getDisplayString().length() - 1));
							
							if (getDisplayString().length() < 1)
								setDisplayString("0");
						    }
						break;

					case 18:	// CE
						clearExisting();
						break;

					case 19:	// C
						clearAll();
						break;
        	  	    }
        	  	   
        	}	


	}

	void setDisplayString(String s)
		{
		lab1.setText(s);
	    }

	String getDisplayString ()
		{
		return lab1.getText();
	    }

	void addDigitToDisplay(int digit)
		{
		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();
		
		if (inputString.indexOf("0") == 0)
			{
			inputString = inputString.substring(1);
		    }

		if ((!inputString.equals("0") || digit > 0) &&(inputString.length() < MAX_INPUT_LENGTH))
			{
			setDisplayString(inputString + digit);
		    }
		displayMode = INPUT_MODE;
		clearOnNextDigit = false;
	}

	void addDecimalPoint()
		{
		displayMode = INPUT_MODE;

		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();
	
		// If the input string already contains a decimal point, don't
		//  do anything to it.
		if (inputString.indexOf(".") < 0)
			setDisplayString(new String(inputString + "."));
	   }

	void processSignChange()
		{
		if (displayMode == INPUT_MODE)
		{
			String input = getDisplayString();

			if (input.length() > 0 && !input.equals("0"))
			{
				if (input.indexOf("-") == 0)
					setDisplayString(input.substring(1));

				else
					setDisplayString("-" + input);
			}
			
		}

		else if (displayMode == RESULT_MODE)
		{
			double numberInDisplay = getNumberInDisplay();
		
			if (numberInDisplay != 0)
				displayResult(-numberInDisplay);
		}
	}

	void clearAll()	
		{
		setDisplayString("0");
		lastOperator = "0";
		lastNumber = 0;
		displayMode = INPUT_MODE;
		clearOnNextDigit = true;
	    }

	void clearExisting()
		{
		setDisplayString("0");
		clearOnNextDigit = true;
		displayMode = INPUT_MODE;
	    }

	double getNumberInDisplay()	
		{
		String input = lab1.getText();
		return Double.parseDouble(input);
    	}

	void processOperator(String op) 
	    {
			double numberInDisplay = getNumberInDisplay();
			lastNumber = numberInDisplay;	
			clearOnNextDigit = true;
			lastOperator = op;
		}

	void processEquals()
		{
		double result = 0;

		if (displayMode != ERROR_MODE)
			{
			try			
			{
				result = processLastOperator();
				displayResult(result);
			}
			
			catch (DivideByZeroException e)	{
				displayError("Cannot divide by zero!");
			}

			lastOperator = lastOperator;
		}
	}

	double processLastOperator() throws DivideByZeroException 
		{
		double result = 0;
		double numberInDisplay = getNumberInDisplay();

		if (lastOperator.equals("/"))
		{
			if (numberInDisplay == 0)
				throw (new DivideByZeroException());
			result = lastNumber / numberInDisplay;
		}
			
		if (lastOperator.equals("*"))
			result = lastNumber * numberInDisplay;

		if (lastOperator.equals("-"))
			result = lastNumber - numberInDisplay;

		if (lastOperator.equals("+"))
			result = lastNumber + numberInDisplay;

		return result;
	    }

	void displayResult(double result)
		{
		setDisplayString(Double.toString(result));
		lastNumber = result;
		displayMode = RESULT_MODE;
		clearOnNextDigit = true;
	    }

	void displayError(String errorMessage){
		setDisplayString(errorMessage);
		lastNumber = 0;
		displayMode = ERROR_MODE;
		clearOnNextDigit = true;
	}
	public static void main(String arg[])
	{
		new cal();
	}
    }

You have used a DivideByZeroException class which is not defined. Defined that class by inheriting to Exception class

This article has been dead for over six months. Start a new discussion instead.