Memory buttons problem

Reply

Join Date: Apr 2004
Posts: 1
Reputation: redelao is an unknown quantity at this point 
Solved Threads: 0
redelao redelao is offline Offline
Newbie Poster

Memory buttons problem

 
0
  #1
Apr 29th, 2004
I am pretty fresh in the java environment but i am working hard to learn. I wrote the code for a calculator, however I am having problems in making the memory buttons function properly. I have to create 4 memory buttons that function in the following way: When the memory button is pushed once, whatever value is in the textfield has to be remembered. When the same memory button is pushed again, the remembered value has to be placed into the text field, and so on. The major conflict is that all four buttons must store and draw their values from the same array. If you can please help me with this, give any suggestions !! Your help is very much appreciated.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//**************************************************************************************************
public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 225;
public static final int HEIGHT = 225;
public static final int ROWS = 5;
public static final int COLUMNS = 4;
private JTextField display;
private boolean transform = false;
private String equal = "=";
private double argum = 0;

//**************************************************************************************************
// This constructor creates the GUI.
public Calculator()
{
setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH,HEIGHT);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
cp.add(display, "North");
JPanel bp = new JPanel();
bp.setLayout(new GridLayout (ROWS,COLUMNS));
String buttons = "789/456*123-0.=+";
for (int n = 0; n < buttons.length(); n++)
addButtons(bp, buttons.substring(n, n + 1));
JButton memo1 = new JButton("M1");
bp.add(memo1);
memo1.addActionListener(this);
JButton memo2 = new JButton("M2");
bp.add(memo2);
memo2.addActionListener(this);
JButton memo3 = new JButton("M3");
bp.add(memo3);
memo3.addActionListener(this);
JButton memo4 = new JButton("M4");
bp.add(memo4);
memo4.addActionListener(this);
cp.add(bp, "Center");
}
private void addButtons(Container con, String str)
{
JButton but = new JButton(str);
con.add(but);
but.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
double[] memo = new double[4];


if ('0' <= str.charAt(0) && str.charAt(0) <= '9'
|| str.equals("."))
{
if (transform) display.setText(str);
else display.setText(display.getText() + str);
transform = false;
}
else
{
double val = (Double.valueOf(display.getText())).doubleValue();
calculate(val);
equal = str;
//These memory buttons functions are not working !!
if (str.equals("M1")) memo[0] = (Double.valueOf(display.getText())).doubleValue();
{if (equal.equals("M1")) display.setText(""+memo[0]);}


transform = true;

}
}
public void calculate(double num)
{

if (equal.equals("+")) argum += num;
else if (equal.equals("-")) argum -= num;
else if (equal.equals("*")) argum *= num;
else if (equal.equals("/")) argum /= num;
else if (equal.equals("=")) argum = num;


display.setText("" + argum);
}
//**************************************************************************************************
// This is the main of the class Calculator.
public static void main(String[] args)
{
JFrame f = new Calculator();
f.setVisible(true);
} // end of main.
} // end of class Calculator.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 72
Reputation: gusano79 is on a distinguished road 
Solved Threads: 4
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: Memory buttons problem

 
0
  #2
May 18th, 2004
One reason why your memory buttons don't work: The array you're using to store the values is declared in the actionPerformed method. This means that as soon as the method returns, the array goes away forever. If you move the declaration somewhere with a larger scope, e.g. make it a member of your Calculator class, the stored values will stick around after the action processing has finished.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC