Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Design and code a GUI calculator in java

Design and code a Swing GUI calculator. You can use Display 17.19 as a starting
point, but your calculator will be more sophisticated. Your calculator will have
two text fields that the user cannot change: One labeled “Result” will contain the
result of performing the operation, and the other labeled “Operand” will be for
the user to enter a number to be added, subtracted, and so forth from the
result. The user enters the number for the “Operand” text field by clicking buttons
labeled with the digits 0 through 9 and a decimal point, just as in a real calculator.
Allow the operations of addition, subtraction, multiplication, and division. Use a
GridLayout manager to produce a button pad that looks similar to the keyboard
on a real calculator.
When the user clicks a button for an operation, the following occurs: the operation
is performed, the “Result” text field is updated, and the “Operand” text field is
cleared. Include a button labeled “Reset” that resets the “Result” to 0.0. Also
include a button labeled “Clear” that resets the “Operand” text field so it is blank.
Hint: Define an exception class named DivisonByZeroException. Have your
code throw and catch a DivisonByZeroException if the user attempts to “divide
by zero.” Your code will catch the DivisonByZeroException and output a suitable
message to the “Operand” text field. The user may then enter a new substitute

number in the “Operand” text field. Because values of type double are, in effect,
approximate values, it makes no sense to test for equality with 0.0. Consider an
operand to be “equal to zero” if it is in the range -1.0e-10 to +1.0e-10.

Answer:


/**
 * Calculator.java 
 */

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calculator extends JFrame /*implements ActionListener*/ {

   private JTextField _result, _operand;
   private JButton _one, _two, _three, _four, _five, _six, _seven,
      _eight, _nine, _zero, _point, _add, _sub, _mult, _div;

   public Calculator ()
   {
      setTitle("Question 3");
      setDefaultCloseOperation(EXIT_ON_CLOSE);

      setLayout(new BorderLayout());

      add(this.createTextFields(), BorderLayout.NORTH);
      add(this.createNumbers(), BorderLayout.CENTER);
   }

   public JPanel createTextFields()
   {
      JPanel text = new JPanel();
      text.add(new JLabel("Result"));
      _result = new JTextField("0", 10);
      _result.setEditable(false);
      text.add(_result);

      text.add(new JLabel("Operand"));
      _operand = new JTextField(10);
      _operand.setEditable(false);
      text.add(_operand);

      return text;
   }

   public JPanel createNumbers()
   {
      JPanel nums = new JPanel();
      nums.setLayout(new BorderLayout());

      JPanel ops = new JPanel();
      ops.setLayout(new GridLayout(4,1));
      _add = new JButton("+");
      _add.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       Calculator.this.operatorPressed("+");
	    }
	 });
      ops.add(_add);

      _sub = new JButton("-");
      _sub.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       Calculator.this.operatorPressed("-");
	    }
	 });
      ops.add(_sub);

      _mult = new JButton("*");
      _mult.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       Calculator.this.operatorPressed("*");
	    }
	 });
      ops.add(_mult);

      _div = new JButton("/");
      _div.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       try 
	       {
		  Calculator.this.divisionPressed();
	       }
	       catch(DivisionByZeroException dbze)
	       {
		  _operand.setText("Error");
	       }
	    }
	 });
      ops.add(_div);

      nums.add(ops, BorderLayout.EAST);

      JPanel numbers = new JPanel();
      numbers.setLayout(new GridLayout(4,3));
      _one = new JButton("1");
      _one.addActionListener(new ActionListener() 
	 {
	    public void actionPerformed(ActionEvent e) 
	    {
	       _operand.setText(_operand.getText() + "1");
	    }
	 });
      numbers.add(_one);

      _two = new JButton("2");
      _two.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "2");
	    }
	 });
      numbers.add(_two);

      _three = new JButton("3");
      _three.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "3");
	    }
	 });
      numbers.add(_three);

      _four = new JButton("4");
      _four.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "4");
	    }
	 });
      numbers.add(_four);

      _five = new JButton("5");
      _five.addActionListener(new ActionListener() 
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "5");
	    }
	 });
      numbers.add(_five);

      _six = new JButton("6");
      _six.addActionListener(new ActionListener() 
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "6");
	    }
	 });
      numbers.add(_six);

      _seven = new JButton("7");
      _seven.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "7");
	    }
	 });
      numbers.add(_seven);

      _eight = new JButton("8");
      _eight.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "8");
	    }
	 });
      numbers.add(_eight);

      _nine = new JButton("9");
      _nine.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "9");
	    }
	 });
      numbers.add(_nine);

      _point = new JButton(".");
      _point.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + ".");
	    }
	 });
      numbers.add(_point);

      _zero = new JButton("0");
      _zero.addActionListener(new ActionListener()
	 {
	    public void actionPerformed(ActionEvent e)
	    {
	       _operand.setText(_operand.getText() + "0");
	    }
	 });
      numbers.add(_zero);


      nums.add(numbers, BorderLayout.CENTER);
      return nums;
   }

   private double getNumFromResult()
   {
      return Double.parseDouble(_result.getText());
   } 

   private double getNumFromOperand()
   {
      return Double.parseDouble(_operand.getText());
   }

   private void divisionPressed() throws DivisionByZeroException
   {
      double result = getNumFromResult();
      double num = getNumFromOperand();
      
      if ( num < 1.0e-10 && num > -1.0e-10 ) {
	 throw new DivisionByZeroException();
      } // end of if ()
      else {
	 result /= num;
	 _result.setText("" + result);
	 _operand.setText("");
      }//end of else
   }

   private void operatorPressed(String op)
   {
      double result = getNumFromResult();
      double num = getNumFromOperand();

      if ( op.equals("+") ) {
	 result += num;
      } // end of if ()
      else if ( op.equals("-") ) {
	 result -= num;
      } // end of if ()
      else if ( op.equals("*") ) {
	 result *= num;
      } // end of if ()

      _result.setText("" + result);
      _operand.setText("");
   }

   public static void main (String[] args) 
   {
      Calculator calc = new Calculator();
      calc.pack();
      calc.show();
   } 
   
}

public class DivisionByZeroException extends Exception{
   public DivisionByZeroException (){
      
   }
}


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!