SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Swing I
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

import javax.swing.SwingConstants;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Calc extends JFrame {

	// Control variables
	private boolean notPlaced = true;
	private boolean divmsg = false;

	// Elements
	private int w, h;
	private JPanel panel;
	private GridBagConstraints gc;
	private JLabel resultLabel;
	private JLabel operandLabel;
	private JTextField resultField;
	private JTextField operandField;
	private JButton resetButton;
	private JButton clearButton;
	private JButton button7;
	private JButton button8;
	private JButton button9;
	private JButton buttonAdd;
	private JButton button4;
	private JButton button5;
	private JButton button6;
	private JButton buttonSub;
	private JButton button1;
	private JButton button2;
	private JButton button3;
	private JButton buttonMul;
	private JButton button0;
	private JButton buttonDec;
	private JButton buttonDiv;

	private class ClearAction implements ActionListener {
		@Override
		public void actionPerformed (ActionEvent e) {
			operandField.setText("");
			notPlaced = true;
			divmsg = false;
		}
	}

	private class DecAction implements ActionListener {
		@Override
		public void actionPerformed (ActionEvent e) {
			if (notPlaced) {
				if(divmsg)
					new ClearAction().actionPerformed(e);
				operandField.setText(operandField.getText() + ".");
				notPlaced = false;
			}
		}
	}

	private class DivAction implements ActionListener {
		public void thrower () throws DivisionByZeroException {
			throw new DivisionByZeroException();
		}
		@Override
		public void actionPerformed (ActionEvent e) {
			try{
				double op = Double.parseDouble(operandField.getText());
				if (op >= -1.0e-10 && op <= 1.0e-10)
					thrower();
				resultField.setText(
					Double.toString(
						Double.parseDouble(resultField.getText())
						/
						op
					)
				);
				new ClearAction().actionPerformed(e);
			} catch (Exception mismatch) {}
		}
	}

	// Exception
	private class DivisionByZeroException extends Exception {
		public DivisionByZeroException () {
			operandField.setText("Division By Zero");
			divmsg = true;
		}
	}

	public Calc () {

		// Initialization
		notPlaced = true;

		// Size
		w = 400;
		h = 400;
		setSize(w, h);

		// Position
		setLocationRelativeTo(null);

		// Close Operation
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		// Title
		setTitle("Java Swing GUI Calculator");

		// Resizable false
		setResizable(false);

		// Make panel
		panel = new JPanel(new GridBagLayout());

		// Set default GridBagConstraints
		gc = new GridBagConstraints();
		gc.gridx		= 0;
		gc.gridy		= 0;
		gc.gridwidth	= 2;
		gc.gridheight	= 1;
		gc.weightx		= 50;
		gc.weighty		= 100;
		gc.insets		= new Insets (5, 5, 5, 5);
		gc.anchor		= gc.CENTER;
		gc.fill			= gc.BOTH;

		// Panel contents

		// Result label
		resultLabel = new JLabel("Result Field", SwingConstants.CENTER);
		panel.add(resultLabel, gc);

		// Operand label
		operandLabel = new JLabel("Operand Field", SwingConstants.CENTER);
		gc.gridx = 2;
		panel.add(operandLabel, gc);
		
		// Result Field
		resultField = new JTextField(Double.toString(0.0), 19);
		resultField.setEditable(false);
		gc.gridx = 0;
		gc.gridy = 1;
		panel.add(resultField, gc);

		// Operand Field
		operandField = new JTextField("", 19);
		operandField.setEditable(false);
		gc.gridx = 2;
		panel.add(operandField, gc);

		// Reset button
		resetButton = new JButton("Reset");
		resetButton.addActionListener(e -> resultField.setText(Double.toString(0.0)));
		gc.gridx = 0;
		gc.gridy = 2;
		panel.add(resetButton, gc);

		// Clear button
		clearButton = new JButton("Clear");
		clearButton.addActionListener(new ClearAction());
		gc.gridx = 2;
		panel.add(clearButton, gc);

		// 7
		button7 = new JButton("7");
		button7.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "7");
		});
		gc.gridwidth = 1;
		gc.gridx = 0;
		gc.gridy = 3;
		panel.add(button7, gc);

		// 8
		button8 = new JButton("8");
		button8.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "8");
		});
		gc.gridx = 1;
		panel.add(button8, gc);

		// 9
		button9 = new JButton("9");
		button9.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "9");
		});
		gc.gridx = 2;
		panel.add(button9, gc);

		// +
		buttonAdd = new JButton("+");
		buttonAdd.addActionListener( e -> {
				try{
					resultField.setText(
						Double.toString(
							Double.parseDouble(resultField.getText())
							+
							Double.parseDouble(operandField.getText())
						)
					);
					new ClearAction().actionPerformed(e);
				} catch (Exception mismatch) {}
			}
		);
		gc.gridx = 3;
		panel.add(buttonAdd, gc);

		// 4
		button4 = new JButton("4");
		button4.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "4");
		});
		gc.gridx = 0;
		gc.gridy = 4;
		panel.add(button4, gc);

		// 5
		button5 = new JButton("5");
		button5.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "5");
		});
		gc.gridx = 1;
		panel.add(button5, gc);

		// 6
		button6 = new JButton("6");
		button6.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "6");
		});
		gc.gridx = 2;
		panel.add(button6, gc);

		// -
		buttonSub = new JButton("-");
		buttonSub.addActionListener( e -> {
				try{
					resultField.setText(
						Double.toString(
							Double.parseDouble(resultField.getText())
							-
							Double.parseDouble(operandField.getText())
						)
					);
					new ClearAction().actionPerformed(e);
				} catch (Exception mismatch) {}
			}
		);
		gc.gridx = 3;
		panel.add(buttonSub, gc);

		// 1
		button1 = new JButton("1");
		button1.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "1");
		});
		gc.gridx = 0;
		gc.gridy = 5;
		panel.add(button1, gc);

		// 2
		button2 = new JButton("2");
		button2.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "2");
		});
		gc.gridx = 1;
		panel.add(button2, gc);

		// 3
		button3 = new JButton("3");
		button3.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "3");
		});
		gc.gridx = 2;
		panel.add(button3, gc);

		// *
		buttonMul = new JButton("*");
		buttonMul.addActionListener( e -> {
				try{
					resultField.setText(
						Double.toString(
							Double.parseDouble(resultField.getText())
							*
							Double.parseDouble(operandField.getText())
						)
					);
					new ClearAction().actionPerformed(e);
				} catch (Exception mismatch) {}
			}
		);
		gc.gridx = 3;
		panel.add(buttonMul, gc);

		// 0
		button0 = new JButton("0");
		button0.addActionListener(e -> {
			if (divmsg)
				new ClearAction().actionPerformed(e);
			operandField.setText(operandField.getText() + "0");
		});
		gc.gridwidth = 2;
		gc.gridx = 0;
		gc.gridy = 6;
		panel.add(button0, gc);

		// .
		buttonDec = new JButton(".");
		buttonDec.addActionListener(new DecAction());
		gc.gridwidth = 1;
		gc.gridx = 2;
		panel.add(buttonDec, gc);

		// /
		buttonDiv = new JButton("/");
		buttonDiv.addActionListener(new DivAction());
		gc.gridx = 3;
		panel.add(buttonDiv, gc);

		// Add panel
		add(panel);

		// Visible
		setVisible(true);
	}

	public static void main (String[] args) {
		new Calc();
	}

}

 

0 0

Discussions

Post the discussion to improve the above solution.