4 Replies - 701 Views - Last Post: 29 October 2012 - 02:55 PM Rate Topic: -----

#1 TesseracT   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-October 12

Help with adding decimals to calculator

Posted 29 October 2012 - 02:07 AM

Hi, i'm very new to java. Basically at my course we had to make a very basic calculator with Java.
I believe I have completed it, and I have submitted what I have done. How ever, my question is, how would I add in decimal points to my calculator?

Also is there a way to simplify some of what I have done with the gui and button layout?
What about changing the colours of the buttons?

Again, I am very new to java, and have little understanding of it, and found this calculator very hard, however, managed to get something that seems to work. Simple explanations would be appreciated :)




Calculator


package calculator08;

import javax.swing.AbstractButton;
import javax.swing.JButton;

import org.eclipse.jdt.annotation.NonNullByDefault;

@NonNullByDefault
public class Calculator {
	// Constant defining OPERATOR state.
	// Operator buttons set state to OPR.
	private final int OPR = 0;
	// Constant defining NUMERIC state.
	// Digit buttons set state to NUM.
	private final int NUM = 1;
	// state when errors occur
	private final int ERR = 2;
	// ------------------------------------------------------------------------
	// The current state of the calculator.
	private int state;
	// First number (remembered result from last calculation).
	private int num1;
	// First operator (remembered when last calculation was done).
	// Operators are: + - * / =.
	private char op1;
	// Second number (number currently being entered).
	// Undefined if state is OPR.
	private int num2;

	// field for memory buttons
	private int memoryNumber;

	public Calculator() {
		num1 = 0;
		op1 = '=';
		num2 = 0;
		state = OPR;
		memoryNumber = 0;

	}

	// Returns the text to show in the calculator's display.
	public String getDisplayText() {

		String display = " ";
		if (state == ERR) {
			display = "ERROR";

		}
		if (state == OPR) {
			/*
			 * if (num1 < 0 && op1 != '=') { display = "( " +
			 * Integer.toString(num1) + " )" + " " + Character.toString(op1) +
			 * " " + Integer.toString(num2);
			 * 
			 * } else if (num2 < 0 && num1 < 0) {
			 * 
			 * display = "( " + Integer.toString(num1) + " )" + " " +
			 * Character.toString(op1) + " " + "( " + Integer.toString(num2) +
			 * " )"; } else {
			 */
			display = Integer.toString(num1) + " " + Character.toString(op1);
			// }

		}

		if (state == NUM) {

			// if (num1 < 0) {
			// display = "( " + Integer.toString(num1) + " )" + " "
			// + Character.toString(op1) + " "
			// + Integer.toString(num2);

			// } else if (num2 < 0 && op1 != '=') {
			//
			// display = Integer.toString(num1) + " "
			// + Character.toString(op1) + " " + "( "
			// + Integer.toString(num2) + " )";
			// } else
			if (op1 == '=') {
				display = Integer.toString(num2);
			} else {
				display = Integer.toString(num1) + " "
						+ Character.toString(op1) + " "
						+ Integer.toString(num2);
			}

		}

		return display;
	}

	
	
	// Updates this calculator, when a digit is entered.
	public void enterDigit(int digit) {

		if (state == NUM) {
			if (num2 == 0) {
				state = OPR;

			}
			if (num2 > 0) {
				num2 = (num2 * 10) + digit;
			}
			if (num2 < 0) {
				num2 = (num2 * 10) - digit;
			}

			if (num2 > Integer.MAX_VALUE / 10 || num2 < Integer.MIN_VALUE / 10) {
				state = ERR;
			}

		}
		if (state == OPR) {
			num2 = digit;
			state = NUM;

		}

	}

	// Updates this calculator, when an operator is entered.
	public void enterOperator(char op2) {
		System.out.println(state);
		if (op1 == '/' && num2 == 0 && state != OPR) {
			state = ERR;

		}

		if (state == OPR) {
			op1 = op2;

		} else if (state == NUM) {

			if (op1 == '+') {
				num2 = num1 + num2;
			}
			if (op1 == '-') {
				num2 = num1 - num2;
			}
			if (op1 == '*') {
				num2 = num1 * num2;
			}
			if (op1 == '/' && num2 != 0) {

				num2 = num1 / num2;

			}
			if (op1 == '=') {
				num1 = 0 + num2;
			}
			state = OPR;
			num1 = num2;
			num2 = 0;
			op1 = op2;

		}

	}

	// Memory buttons
	public void clearMemory() {
		memoryNumber = 0;
		System.out.println(memoryNumber);

	}

	public void readMemory() {
		if (num2 == 0 && state == OPR) {
			state = NUM;
			num2 = memoryNumber;

		} else {

			num1 = memoryNumber;
			state = OPR;
		}
	}

	public void saveInMemory() {

		if (state == OPR) {
			memoryNumber = num1;

		}
		if (state == NUM && num2 > 0) {
			memoryNumber = num2;
		}
		System.out.println(memoryNumber);
	}

	public void addToMemory() {
		if (state == OPR) {
			memoryNumber = memoryNumber + num1;

		}
		if (state == NUM && num2 > 0) {

			memoryNumber = memoryNumber + num2;

		}

		System.out.println(memoryNumber);

	}

	// Special buttons

	public void clearNumber() {
		if (state == ERR) {
			state = OPR;
			num1 = 0;
			op1 = '=';
		}
		if (state == NUM && num2 == 0 && num1 == 0) {
			num1 = 0;
		} else {
			num2 = 0;
			state = OPR;
		}
		if (state == OPR && op1 == '=') {
			num1 = 0;
		}

	}

	public void reset() {
		num1 = 0;
		op1 = '=';
		state = OPR;
		num2 = 0;

	}

	public void removeDigit() {

		if (state == NUM && num2 == 0 && num1 == 0) {
			num1 = num1 / 10;
			if (num1 == 0) {
				num1 = 0;
				op1 = '=';
				state = OPR;
				num2 = 0;
			}

		} else {
			num2 = num2 / 10;
			if (num2 == 0) {
				num2 = 0;
				state = OPR;

			}

		}

	}

	public void changeSign() {
		if (state == NUM && num2 == 0 && num1 == 0) {
			num1 = num1 * (-1);
		} else {
			num2 = num2 * (-1);
		}
		if (state == OPR && op1 == '=') {
			num1 = num1 * (-1);
		}

	}
}





Calculator Frame


package calculator08;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

import org.eclipse.jdt.annotation.NonNullByDefault;

@NonNullByDefault
public class CalculatorFrame extends JFrame {

	public static void main(String[] args) {
		CalculatorFrame frame = new CalculatorFrame();
		frame.pack();
		frame.setVisible(true);
	}

	private CalculatorFrame() {
		this.initFrame();
		this.initContent();
	}

	private void initFrame() {
		this.setTitle("Calculator");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(200, 100);
		this.setResizable(false);
		this.getContentPane().setPreferredSize(new Dimension(310, 370));
		this.getContentPane().setLayout(null);
	}

	// ------------------------------------------------------------------------
	private JTextField txfDisplay;
	private JButton[] btnDigit;
	private JButton btnAdd, btnSub, btnMul, btnDiv, btnCalc;
	private JButton btnMemCopy, btnMemSave, btnMemAdd, btnMemClear;
	private JButton btnSpDelDigit, btnSpDelNumber, btnSpDelSaved, btnSpDec, 
			btnSpChangeSign ; 
	private Calculator calculator = new Calculator();

	private DigitController digitCtrl = new DigitController();
	private OperatorController oprCtrl = new OperatorController();
	private MemoryController memCtrl = new MemoryController();
	private SpecialController specCtrl = new SpecialController();

	private void initContent() {
		// width and height of buttons
		int wh = 50;
		// distance between buttons, and between buttons and window-border
		int sep = 10;
		Font font = new Font("Courier", Font.BOLD, 20);
		Insets margin = new Insets(1, 1, 1, 1);

		txfDisplay = new JTextField();
		this.add(txfDisplay);
		txfDisplay.setFont(font);
		txfDisplay.setFocusable(false);
		txfDisplay.setHorizontalAlignment(JTextField.RIGHT);
		txfDisplay.setSize(5 * wh + 4 * sep, wh);
		txfDisplay.setLocation(sep, sep);

		// calculation buttons
		btnCalc = new JButton("=");
		this.add(btnCalc);
		btnCalc.setFocusable(false);
		btnCalc.setFont(font);
		btnCalc.setMargin(margin);
		btnCalc.setSize(wh, wh);
		btnCalc.setLocation(4 * wh + 5 * sep, 5 * wh + 6 * sep);
		btnCalc.addActionListener(oprCtrl);

		btnAdd = new JButton("+");
		this.add(btnAdd);
		btnAdd.setFocusable(false);
		btnAdd.setFont(font);
		btnAdd.setMargin(margin);
		btnAdd.setSize(wh, wh);
		btnAdd.setLocation(4 * wh + 5 * sep, 4 * wh + 5 * sep);
		btnAdd.addActionListener(oprCtrl);

		btnSub = new JButton("-");
		this.add(btnSub);
		btnSub.setFocusable(false);
		btnSub.setFont(font);
		btnSub.setMargin(margin);
		btnSub.setSize(wh, wh);
		btnSub.setLocation(4 * wh + 5 * sep, 3 * wh + 4 * sep);
		btnSub.addActionListener(oprCtrl);

		btnMul = new JButton("*");
		this.add(btnMul);
		btnMul.setFocusable(false);
		btnMul.setFont(font);
		btnMul.setMargin(margin);
		btnMul.setSize(wh, wh);
		btnMul.setLocation(4 * wh + 5 * sep, 2 * wh + 3 * sep);
		btnMul.addActionListener(oprCtrl);

		btnDiv = new JButton("/");
		this.add(btnDiv);
		btnDiv.setFocusable(false);
		btnDiv.setFont(font);
		btnDiv.setMargin(margin);
		btnDiv.setSize(wh, wh);
		btnDiv.setLocation(4 * wh + 5 * sep, 1 * wh + 2 * sep);
		btnDiv.addActionListener(oprCtrl);

		// Memory Buttons
		btnMemClear = new JButton("MC");
		this.add(btnMemClear);
		btnMemClear.setFocusable(false);
		btnMemClear.setFont(font);
		btnMemClear.setMargin(margin);
		btnMemClear.setSize(wh, wh);
		btnMemClear.setLocation(0 * wh + 1 * sep, 2 * wh + 3 * sep);
		btnMemClear.addActionListener(memCtrl);

		btnMemCopy = new JButton("MR");
		this.add(btnMemCopy);
		btnMemCopy.setFocusable(false);
		btnMemCopy.setFont(font);
		btnMemCopy.setMargin(margin);
		btnMemCopy.setSize(wh, wh);
		btnMemCopy.setLocation(0 * wh + 1 * sep, 3 * wh + 4 * sep);
		btnMemCopy.addActionListener(memCtrl);

		btnMemSave = new JButton("MS");
		this.add(btnMemSave);
		btnMemSave.setFocusable(false);
		btnMemSave.setFont(font);
		btnMemSave.setMargin(margin);
		btnMemSave.setSize(wh, wh);
		btnMemSave.setLocation(0 * wh + 1 * sep, 4 * wh + 5 * sep);
		btnMemSave.addActionListener(memCtrl);

		btnMemAdd = new JButton("M+");
		this.add(btnMemAdd);
		btnMemAdd.setFocusable(false);
		btnMemAdd.setFont(font);
		btnMemAdd.setMargin(margin);
		btnMemAdd.setSize(wh, wh);
		btnMemAdd.setLocation(0 * wh + 1 * sep, 5 * wh + 6 * sep);
		btnMemAdd.addActionListener(memCtrl);

		// Special Buttons
		btnSpDelDigit = new JButton("<x");
		this.add(btnSpDelDigit);
		btnSpDelDigit.setFocusable(false);
		btnSpDelDigit.setFont(font);
		btnSpDelDigit.setMargin(margin);
		btnSpDelDigit.setSize(wh, wh);
		btnSpDelDigit.setLocation(1 * wh + 2 * sep, 1 * wh + 2 * sep);
		btnSpDelDigit.addActionListener(specCtrl);

		btnSpDelNumber = new JButton("CE");
		this.add(btnSpDelNumber);
		btnSpDelNumber.setFocusable(false);
		btnSpDelNumber.setFont(font);
		btnSpDelNumber.setMargin(margin);
		btnSpDelNumber.setSize(wh, wh);
		btnSpDelNumber.setLocation(2 * wh + 3 * sep, 1 * wh + 2 * sep);
		btnSpDelNumber.addActionListener(specCtrl);

		btnSpDelSaved = new JButton("C");
		this.add(btnSpDelSaved);
		btnSpDelSaved.setFocusable(false);
		btnSpDelSaved.setFont(font);
		btnSpDelSaved.setMargin(margin);
		btnSpDelSaved.setSize(wh, wh);
		btnSpDelSaved.setLocation(3 * wh + 4 * sep, 1 * wh + 2 * sep);
		btnSpDelSaved.addActionListener(specCtrl);

		btnSpChangeSign = new JButton("+/-");
		this.add(btnSpChangeSign);
		btnSpChangeSign.setFocusable(false);
		btnSpChangeSign.setFont(font);
		btnSpChangeSign.setMargin(margin);
		btnSpChangeSign.setSize(wh, wh);
		btnSpChangeSign.setLocation(2 * wh + 3 * sep, 5 * wh + 6 * sep);
		btnSpChangeSign.addActionListener(specCtrl);
		
		btnSpDec = new JButton(".");
		this.add(btnSpDec);
		btnSpDec.setFocusable(false);
		btnSpDec.setFont(font);
		btnSpDec.setMargin(margin);
		btnSpDec.setSize(wh, wh);
		btnSpDec.setLocation(3 * wh + 4 * sep, 5 * wh + 6 * sep);
		btnSpDec.addActionListener(specCtrl);
		
	

		
		
		
		// Digit Buttons
		btnDigit = new JButton[10];
		for (int i = 0; i <= 9; i++) {
			btnDigit[i] = new JButton("" + i);
			this.add(btnDigit[i]);
			btnDigit[i].setFocusable(false);
			btnDigit[i].setFont(font);
			btnDigit[i].setMargin(margin);
			btnDigit[i].setSize(wh, wh);
			btnDigit[i].addActionListener(digitCtrl);
			if (i == 0) {
				btnDigit[i].setLocation(1 * wh + 2 * sep, 5 * wh + 6 * sep);
			}
			if (i == 1) {
				btnDigit[i].setLocation(1 * wh + 2 * sep, 4 * wh + 5 * sep);

			}
			if (i == 2) {
				btnDigit[i].setLocation(2 * wh + 3 * sep, 4 * wh + 5 * sep);
			}
			if (i == 3) {
				btnDigit[i].setLocation(3 * wh + 4 * sep, 4 * wh + 5 * sep);
			}
			if (i == 4) {
				btnDigit[i].setLocation(1 * wh + 2 * sep, 3 * wh + 4 * sep);
			}
			if (i == 5) {
				btnDigit[i].setLocation(2 * wh + 3 * sep, 3 * wh + 4 * sep);
			}
			if (i == 6) {
				btnDigit[i].setLocation(3 * wh + 4 * sep, 3 * wh + 4 * sep);
			}
			if (i == 7) {
				btnDigit[i].setLocation(1 * wh + 2 * sep, 2 * wh + 3 * sep);
			}
			if (i == 8) {
				btnDigit[i].setLocation(2 * wh + 3 * sep, 2 * wh + 3 * sep);
			}
			if (i == 9) {
				btnDigit[i].setLocation(3 * wh + 4 * sep, 2 * wh + 3 * sep);
			}

		}

	}

	// Controller for digit buttons.
	private class DigitController implements ActionListener {
		@Override
		@NonNullByDefault(false)
		public void actionPerformed(ActionEvent e) {
			for (int i = 0; i <= 9; i++) {
				if (e.getSource() == btnDigit[i])
					calculator.enterDigit(i);
			}
			txfDisplay.setText(calculator.getDisplayText());
		}
	}

	// Controller for operator buttons.
	private class OperatorController implements ActionListener {
		@Override
		@NonNullByDefault(false)
		public void actionPerformed(ActionEvent e) {

			if (e.getSource() == btnAdd) {
				calculator.enterOperator('+');
				txfDisplay.setText(calculator.getDisplayText());
			}

			if (e.getSource() == btnSub) {
				calculator.enterOperator('-');
				txfDisplay.setText(calculator.getDisplayText());
			}
			if (e.getSource() == btnMul) {
				calculator.enterOperator('*');
				txfDisplay.setText(calculator.getDisplayText());
			}
			if (e.getSource() == btnDiv) {
				calculator.enterOperator('/');
				txfDisplay.setText(calculator.getDisplayText());
			}
			if (e.getSource() == btnCalc) {
				calculator.enterOperator('=');
				txfDisplay.setText(calculator.getDisplayText());
			}
		}

	}

	// Controller for memory buttons
	private class MemoryController implements ActionListener {

		@Override
		@NonNullByDefault(false)
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnMemSave) {

				calculator.saveInMemory();

			}

			if (e.getSource() == btnMemAdd) {
				calculator.addToMemory();
			}
			if (e.getSource() == btnMemClear) {
				calculator.clearMemory();
			}
			if (e.getSource() == btnMemCopy) {
				calculator.readMemory();
				txfDisplay.setText(calculator.getDisplayText());

			}
		}

	}

	// Controller for special buttons

	private class SpecialController implements ActionListener {

		@Override
		@NonNullByDefault(false)
		public void actionPerformed(ActionEvent e) {

			// private JButton btnSpDelDigit, btnSpDelNumber, btnSpDelSaved,
			// btnSpChangeSign;
			if (e.getSource() == btnSpDelDigit) {
				calculator.removeDigit();
				txfDisplay.setText(calculator.getDisplayText());
			}

			if (e.getSource() == btnSpDelNumber) {
				calculator.clearNumber();
				txfDisplay.setText(calculator.getDisplayText());
			}

			if (e.getSource() == btnSpDelSaved) {
				calculator.reset();
				txfDisplay.setText(calculator.getDisplayText());

			}
			if (e.getSource() == btnSpChangeSign) {
				calculator.changeSign();
				txfDisplay.setText(calculator.getDisplayText());
			}

		}
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Help with adding decimals to calculator

#2 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Help with adding decimals to calculator

Posted 29 October 2012 - 03:22 AM

Quote

Also is there a way to simplify some of what I have done with the gui and button layout?


Yes, use the available layout managers
Check it out
Was This Post Helpful? 0
  • +
  • -

#3 tony12095   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 109
  • Joined: 23-October 12

Re: Help with adding decimals to calculator

Posted 29 October 2012 - 05:03 AM

All you would have to do is add a JButton with the name "."

For example:

JButton decimal  = new JButton(".");

// DO THIS AFTER YOU HAVE INITIATED SOME FORM OF ACTIONLISTENER ON YOUR DECIMAL BUTTON

// then, if you have a textBox or something showing the value of the buttons, just have the decimal button <JTextArea>.setText(".");




thats the simplest way

This post has been edited by tony12095: 29 October 2012 - 05:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 TesseracT   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-October 12

Re: Help with adding decimals to calculator

Posted 29 October 2012 - 11:30 AM

View Posttony12095, on 29 October 2012 - 05:03 AM, said:

All you would have to do is add a JButton with the name "."

For example:

JButton decimal  = new JButton(".");

// DO THIS AFTER YOU HAVE INITIATED SOME FORM OF ACTIONLISTENER ON YOUR DECIMAL BUTTON

// then, if you have a textBox or something showing the value of the buttons, just have the decimal button <JTextArea>.setText(".");




thats the simplest way



That has already been done.... I don't know how to incorporate it into my method though so it would show the decimal on display...
Was This Post Helpful? 0
  • +
  • -

#5 tony12095   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 109
  • Joined: 23-October 12

Re: Help with adding decimals to calculator

Posted 29 October 2012 - 02:55 PM

well when you added the "." JButton, instead of having the function getDisplayText(), just try having your program write out the "." itself inside of the JTextBox, with a function/method such as: .setText(".");
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1