Help with Java Program (Calculator)

I am writing a Java program for a calculator using object oriented programming. I am having problems with some of my methods and some of action events for when the button is clicked.

The program currently uses 7 classes -

1 - Calculator class (the calc skin and main method) no problems here

2 - The memory class (all the methods in here for the calculator memory buttons) no problems here

3 - The display class (A JTextField is used for the display panel and then the methods to set, get etc the display string have been put in this class) having heaps of problems with the methods, no sure if the JTextField should be done as a seperate class like the keyPad etc

4 - The keyPad class (JButtons that represent the numbers keys on the calculator and their action events) having problems with the action events becuase of the methods in the display class

5 - The functionPad class - the fnctions buttons on the calculatot (J Buttons with action events) having troubles with the action events here as weel

6 - The clearPad - has the clear all button, clear last button and equals button again action event here as confusing me

7 - The memory pad (jbuttons for the memory functions of the calculator)

The program is 80% complete if I could work out the rest of the methods for the display class and the action events which i'm sure will be much eaiser after the methods are done for the display class.

I will post the code here if you want it is very long though if anyone out there can help it would be much appreciated, I am on MSN at witdreday@hotmail.com.

[1626 byte] By [vhcommodoreslea] at [2007-11-27 5:36:14]
# 1

Calculator class

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class calculator extends JFrame

{

private Container CP = new Container( );

keyPad KP = new keyPad( );

memoryPad MP = new memoryPad( );

display DP = new display( );

functionPad FP = new functionPad( );

public calculator( )

{

super ( );

setTitle("Sponsored by BigPipers Co-operative LTD May 2007 Marcus Hawkins ");

setSize(600, 600);

CP = getContentPane( );

CP.setBackground(Color.orange);

CP.setLayout(null);

FP.setLocation(255, 100);

CP.add(FP);

DP.setLocation(30, 20);

CP.add(DP);

MP.setLocation(30, 100);

CP.add(MP);

KP.setLocation(95, 100);

CP.add(KP);

}

public static void main (String []Args)

{

calculator win1 = new calculator( );

win1.setLocation(500, 350);

win1.setVisible(true);

}

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 2

memory class

public class memory

{

private double contents;

public memory( )

{

contents = 0.0;

}

private void storeMemory(double nuContents)

{

contents = nuContents;

}

private double getMemory( )

{

return contents;

}

public void clearMemory( )

{

contents = 0.0;

}

private void addMemory(double addValue)

{

contents += addValue;

}

public void subMemory(double subValue)

{

contents -= subValue;

}

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 3

display class

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class display extends JPanel

{

private JTextField InputNumber = new JTextField (50);

public static String DisplayString;

public display ( )

{

setSize(325, 50);

setBackground(Color.green);

setLayout(null);

add(InputNumber);

InputNumber.setSize(325,50);

}

public void updateDisplayView( )

{

String temp9 = InputNumber.getText( );

}

public void clearDisplayString( )

{

DisplayString = "0.0";

}

public void setDisplayString(String nuDisplayString)

{

DisplayString = nuDisplayString;

}

public static String getDisplayString( )

{

return DisplayString;

}

public void apendChar( )

{

}

public void clearLast( )

{

}

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 4

keypad class

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class keyPad extends JPanel implements ActionListener

{

private JButton zeroButton = new JButton ("0");

private JButton oneButton = new JButton ("1");

private JButton twoButton = new JButton ("2");

private JButton threeButton = new JButton ("3");

private JButton fourButton = new JButton ("4");

private JButton fiveButton = new JButton ("5");

private JButton sixButton = new JButton ("6");

private JButton sevenButton = new JButton ("7");

private JButton eightButton = new JButton ("8");

private JButton nineButton = new JButton ("9");

private JButton plusminusButton = new JButton ("+/-");

private JButton decimalButton = new JButton (".");

public keyPad( )

{

super( );

setSize(150, 200);

setBackground(Color.blue);

setLayout(null);

zeroButton.setLocation(0, 150);

zeroButton.setSize(50, 50);

oneButton.setLocation(0, 100);

oneButton.setSize(50, 50);

twoButton.setLocation(50, 100);

twoButton.setSize(50, 50);

threeButton.setLocation(100, 100);

threeButton.setSize(50, 50);

fourButton.setLocation(0, 50);

fourButton.setSize(50, 50);

fiveButton.setLocation(50, 50);

fiveButton.setSize(50, 50);

sixButton.setLocation(100, 50);

sixButton.setSize(50, 50);

sevenButton.setLocation(0, 0);

sevenButton.setSize(50, 50);

eightButton.setLocation(50, 0);

eightButton.setSize(50, 50);

nineButton.setLocation(100, 0);

nineButton.setSize(50, 50);

plusminusButton.setLocation(100, 150);

plusminusButton.setSize(50, 50);

decimalButton.setLocation(50, 150);

decimalButton.setSize(50, 50);

add(zeroButton);

add(oneButton);

add(twoButton);

add(threeButton);

add(fourButton);

add(fiveButton);

add(sixButton);

add(sevenButton);

add(eightButton);

add(nineButton);

add(plusminusButton);

add(decimalButton);

zeroButton.addActionListener(this);

/*oneButton.addActionListener(this);

twoButton.addActionListener(this);

threeButton.addActionListener(this);

fourButton.addActionListener(this);

fiveButton.addActionListener(this);

sixButton.addActionListener(this);

sevenButton.addActionListener(this);

eightButton.addActionListener(this);

nineButton.addActionListener(this);

decimalButton.addActionListener(this);

plusminusButton.addActionListener(this);

*/

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource( ) == zeroButton)

{

try{

String temp = display.getDisplayString( );

temp = temp + 0;

display.setDisplayString(temp);

display.updateDisplayView( );

}

catch (NumberFormatException nfe)

{

}

}

}

/*

if(ae.getSource( ) == oneButton)

if(ae.getSource( ) == twoButton)

if(ae.getSource( ) == threeButton)

if(ae.getSource( ) == fourButton)

if(ae.getSource( ) == fiveButton)

if(ae.getSource( ) == sixButton)

if(ae.getSource( ) == sevenButton)

if(ae.getSource( ) == eightButton)

if(ae.getSource( ) == nineButton)

if(ae.getSource( ) == decimalButton)

if(ae.getSource( ) == plusminusButton)

*/

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 5

clearPad

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class clearPad extends JPanel // implements ActionListener

{

private JButton clearallButton = new JButton ("CA");

private JButton clearlastButton = new JButton ("CL");

private JButton equalsButton = new JButton ("=");

public clearPad( )

{

super( );

setSize(55, 180);

setBackground(Color.white);

setLayout(null);

clearallButton.setLocation(50, 140);

clearallButton.setSize(50, 50);

clearlastButton.setLocation(100, 200);

clearlastButton.setSize(50, 50);

equalsButton.setLocation(150, 260);

equalsButton.setSize(50, 50);;

add(clearallButton);

add(clearlastButton);

add(equalsButton);

/*clearlastButton.addActionListener(this);

clearallButton.addActionListener(this);

equalsButton.addActionListener(this);

*/

}

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 6

function pad

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class functionPad extends JPanel// implements ActionListener

{

private JButton clearallButton = new JButton ("CA");

private JButton clearlastButton = new JButton ("CL");

private JButton equalsButton = new JButton ("=");

private JButton addButton = new JButton ("+");

private JButton minusButton = new JButton ("-");

private JButton multiplyButton = new JButton ("*");

private JButton divideButton = new JButton ("/");

public functionPad( )

{

super( );

setSize(100, 200);

setBackground(Color.white);

setLayout(null);

addButton.setLocation(0, 0);

addButton.setSize(50, 50);

minusButton.setLocation(0, 50);

minusButton.setSize(50, 50);

multiplyButton.setLocation(0, 100);

multiplyButton.setSize(50, 50);

divideButton.setLocation(0, 150);

divideButton.setSize(50, 50);

clearallButton.setLocation(50, 0);

clearallButton.setSize(50, 50);

clearlastButton.setLocation(50, 50);

clearlastButton.setSize(50, 50);

equalsButton.setLocation(50, 150);

equalsButton.setSize(50, 50);;

add(clearallButton);

add(clearlastButton);

add(equalsButton);

add(addButton);

add(minusButton);

add(divideButton);

add(multiplyButton);

/*clearlastButton.addActionListener(this);

clearallButton.addActionListener(this);

equalsButton.addActionListener(this);

addButton.addActionListener(this);

minusButton.addActionListener(this);

divideButton.addActionListener(this);

multiplyButton.addActionListener(this);

*/

}

/*

public void actionPerformed(ActionEvent ae)

{

String temp;

if(ae.getSource( ) == clearallButton)

{

display.clearDisplayString( );

memory.clearMemory( );

if(ae.getSource( ) == clearlastButton)

temp = display.getDisplayString( );

temp = temp.substring(0, temp.length - 1);

display.setDisplayString(temp);

display.updateDisplayView( );

}

/*

if(ae.getSource( ) == equalsButton)

if ae.getSource( ) == addButton)

if ae.getSource( ) == minusButton)

if ae.getSource( ) == divideButton)

if ae.getSource( ) == multiplyButton)

*/

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 7

memory pad

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class memoryPad extends JPanel implements ActionListener

{

private JButton memaddButton = new JButton ("M+");

private JButton memminusButton = new JButton ("M-");

private JButton memrecallButton = new JButton ("MR");

private JButton memclearButton = new JButton ("MC");

public memoryPad( )

{

super( );

setSize(55, 200);

setBackground(Color.blue);

setLayout(null);

memaddButton.setLocation(0, 0);

memaddButton.setSize(55, 50);

memminusButton.setLocation(0, 50);

memminusButton.setSize(55, 50);

memrecallButton.setLocation(0, 100);

memrecallButton.setSize(55, 50);

memclearButton.setLocation(0, 150);

memclearButton.setSize(55, 50);

add(memaddButton);

add(memminusButton);

add(memrecallButton);

add(memclearButton);

memaddButton.addActionListener(this);

memminusButton.addActionListener(this);

memrecallButton.addActionListener(this);

memclearButton.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource( ) == memaddButton)

{

try

{

double temp = Double.parseDouble (myDislay.getdisplayString ( ));

myMem.storeMemory(myMem.getMemory( ) + temp);

}

catch(NumberFormatExpection nfe)

{

double temp = 0.0;

myMem.storeMemory (myMem.getMemory( ) + temp);

}

}

if(ae.getSource( ) == memminusButton)

{

try

{

double temp = Double.parseDouble (myDislay.getdisplayString ( ));

myMem.storeMemory(myMem.getMemory( ) - temp);

}

catch(NumberFormatExpection nfe)

{

double temp = 0.0;

myMem.storeMemory (myMem.getMemory( ) - temp);

}

}

if(ae.getSource( ) == memclearButton)

{

myMem.clearMemory( );

}

if(ae.getSource( ) == memrecallButton)

{

myDisplay.setDsplayString (Double.tostring(myMem.getMemory( )));

}

}

}

vhcommodoreslea at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 8

When posting code, please use the code tags as described in the [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips link[/url]

You may be able to go back and edit the posts, provided you didn't reply to them.

Also, it's unlikely that anyone is going to want to go through that much code to discover your problems, you should have provided us with an [url=http://mindprod.com/jgloss/sscce.html]SSCCE [/url] which would either have solved your problems, or at least simplified it enough for us to be able to help you.

Also, you would be best advised to use the sun recommended java coding standards when writing Java [url=http://java.sun.com/docs/codeconv/]Code conventions[/url]

macrules2a at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 9

Mate,

This doesn't mean I like dunnydores, o'right... I was learning swing and I thought this was a nice little exercise, so I got stuck in and finished it off.

Be warned that I'm only a learner at this myself, so don't go using this thing as an example of how do stuff... and it seems to mostly work, but I'd personally refrain from using it to calculate the national debt of Angola or anything important like that... at least someone's tested it.

Calculator.javapackage calc;

import javax.swing.JFrame;

import javax.swing.WindowConstants;

import java.awt.Container;

import java.awt.Color;

public class Calculator extends JFrame {

private static final long serialVersionUID = 234093248;

private Container pane = new Container();

Display display = new Display();

Memory memory = new Memory();

KeyPad keypad = new KeyPad(display, memory);

public Calculator() {

super();

this.setTitle("Calculator");

this.setSize(212, 360);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

pane = this.getContentPane( );

pane.setBackground(Color.orange);

pane.setLayout(null);

display.setLocation(2, 2);

pane.add(display);

keypad.setLocation(2, 25);

pane.add(keypad);

}

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new Calculator().setVisible(true);

}

});

}

}

Memory.javapackage calc;

public class Memory {

private double value;

public Memory() {

this.clear();

}

public void set(double value) {

this.value = value;

}

public double get() {

return this.value;

}

public void clear() {

this.value = 0.0;

}

}

Display.javapackage calc;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.awt.Color;

import java.text.NumberFormat;

public class Display

extends JPanel

{

private static final long serialVersionUID = 650231146;

private JTextField text = new JTextField(50);

public static String display;

public Display() {

this.setSize(200, 20);

this.setBackground(Color.green);

this.setLayout(null);

this.text.setSize(200, 20); this.add(text);

this.clear();

}

public void update() {

this.text.setText(this.display);

}

public void clear() {

this.set("");

this.update();

}

public void blink() {

}

// getters

public String getString() {

return this.display;

}

public double get() {

double d = 0.0;

try {

d = Double.parseDouble(this.display);

} catch (NumberFormatException e) {

d = Double.NaN;

System.err.println(e.toString());

}

return d;

}

//setters

public void set(String value) {

if ("0".equals(value) || "0.0".equals(value)) {

this.display = "";

} else {

this.display = value;

}

this.update();

}

public void set(double value) {

this.set(""+value);

}

public void append(String c) {

this.display += c;

this.update();

}

public void backspace() {

if(this.display.length()==0) return;

this.display = this.display.substring(0, this.display.length()-1);

this.update();

}

}

KeyPad.javapackage calc;

import javax.swing.JButton;

import javax.swing.JPanel;

import java.awt.Color;

import java.awt.Point;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

public class KeyPad

extends JPanel

implements ActionListener

{

private static final long serialVersionUID = 2349832;

private static final int BUTTONS = 22;

private static final int BTNSIZE = 50;

private static final String faces[] = {

"ca","cl","m","mr"

,"?,"/","*","-" //\361=?/font>

,"7","8","9","+"

,"4","5","6",""

,"1","2","3","="

,"0","."

};

private static final int I_PLUS= 11;

private static final int I_BLANK= 15;

private static final int I_EQUALS= 19;

private static final int I_ZERO= 20;

private static final int I_DECIMAL = 21;

private Display display;

private Memory memory;

private JButton buttons[] = new JButton[BUTTONS];

//private double previous = 0.0;

private double value = 0.0;

private char operation = '\0';

public KeyPad(Display display, Memory memory) {

super();

this.display = display;

this.memory = memory;

this.setSize(4*BTNSIZE , 6*BTNSIZE);

this.setBackground(Color.blue);

this.setLayout(null);

for (int i=0; i<BUTTONS; i++) {

buttons[i] = new JButton(faces[i]);

buttons[i].setSize(BTNSIZE, BTNSIZE);

buttons[i].setLocation( (i%4)*BTNSIZE , i/4*BTNSIZE );

this.add(buttons[i]);

buttons[i].addActionListener(this);

}

// resize & reposition the wierd buttons

buttons[I_PLUS].setSize(BTNSIZE, BTNSIZE*2);//double height

buttons[I_BLANK].setSize(0, 0);//invisible

buttons[I_EQUALS].setSize(BTNSIZE, BTNSIZE*2); //double height

buttons[I_ZERO].setSize(BTNSIZE*2, BTNSIZE);//double width

// move the decimal point button right one place

Point p = buttons[I_DECIMAL].getLocation();

p.x+=BTNSIZE;

buttons[I_DECIMAL].setLocation(p);

}

public void actionPerformed(ActionEvent event) {

if( ! (event.getSource() instanceof JButton) ) return;

JButton button = (JButton)event.getSource();

String btnText = button.getText();

if (btnText.matches("[0-9\\.]")) {

display.append(btnText);

} else if (btnText.matches("[/\\*\\-\\+]")) {

this.value = display.get();

this.operation = btnText.charAt(0);

display.clear();

} else if ("=".equals(btnText)) {

this.value = this.calculate(this.value, this.operation, display.get());

display.set(this.value);

} else if ("CA".equals(btnText)) {

this.value = 0;

display.clear();

} else if ("CL".equals(btnText)) {

display.backspace();

} else if ("M".equals(btnText)) {

memory.set(display.get());

display.blink();

} else if ("MR".equals(btnText)) {

display.set(memory.get());

display.blink();

} else if ("?.equals(btnText)) {

display.set(display.get() * -1.0);

} else if ("".equals(btnText)) {

// shutdown

} else {

System.err.println("KeyPad.actionPerformed: btnText="+btnText);

}

}

private static double calculate(double a, char op, double b) {

double result = 0.0;

switch (op) {

case '/': result = a / b; break;

case '*': result = a * b; break;

case '-': result = a - b; break;

case '+': result = a + b; break;

}

return result;

}

}

Cheers. Keith.">

corlettka at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...
# 10

You specify the keys as lowercase

package calc;

private static final String faces[] = {

"ca","cl","m","mr"

,"?,"/","*","-" //\361=?/font>

,"7","8","9","+"

,"4","5","6",""

,"1","2","3","="

,"0","."

};

But you check for upper case.

else if ("CA".equals(btnText)) {

this.value = 0;

display.clear();

} else if ("CL".equals(btnText)) {

display.backspace();

} else if ("M".equals(btnText)) {

memory.set(display.get());

display.blink();

} else if ("MR".equals(btnText)) {

display.set(memory.get());

display.blink();

} else if ("?.equals(btnText)) {

display.set(display.get() * -1.0);

else if ("".equals(btnText)) {

// shutdown

else {

System.err.println("KeyPad.actionPerformed:

btnText="+btnText);

}

macrules2a at 2007-7-12 15:06:39 > top of Java-index,Java Essentials,New To Java...