A
aks_java
Hi, I've been working on a java program that implements a calculator.
There are following modules in my program:
- Calculator.java : contains main method, creates a calculator
instance.
- calcGUI.java: Builds the basic calculator GUI.
- ActionCalculator.java: Implements action listener class.
- Operations.java Implements the operations and does all the
calculations on operands depending upon the operator used.
The problem is that I can't get this program to work and it seems the
calculator object that has been created in Calculator.java cannot be
accessed in ActionCalculator.java and Operations.java even though the
object has been declared as a public static.
Here are the modules:
****************** Calculator.java*****************************
package calculator;
public class Calculator {
public static calcGUI calculator;
public static void main (String args[]) {
calculator = new calcGUI();
calculator.setVisible( true );
}
}
*********************calcGUI.java**********************************
package calculator;
import java.awt.*;
import javax.swing.*;
public class calcGUI extends JFrame {
public JTextField displayField;
public JLabel memRegLabel;
public String memRegVal = "";
public double prevValue = 0;
public String operatorUsed = "=";
public boolean resetFlag = false;
public calcGUI () {
super ();
setSize( 400, 200 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container cont = getContentPane();
displayField = new JTextField("0", 12);
cont.setLayout( new BoxLayout( cont, BoxLayout.Y_AXIS ) );
cont.add( displayField = new JTextField( "0", 12 ) );
displayField.setHorizontalAlignment( JTextField.RIGHT );
JPanel panel1 = new JPanel();
panel1.setLayout( new BoxLayout( panel1, BoxLayout.X_AXIS ) );
cont.add( panel1 );
JPanel panel2 = new JPanel();
panel2.setLayout( new BoxLayout( panel2, BoxLayout.Y_AXIS ) );
panel1.add( panel2 );
JPanel panel3 = new JPanel();
panel3.setLayout( new GridLayout( 1, 23) );
panel3.add( memRegLabel = new JLabel( memRegVal = "" ) );
memRegLabel.setHorizontalAlignment( SwingConstants.CENTER );
panel3.add( new JButton( "Backspace" ) );
panel3.add( new JButton( "CE" ) );
panel3.add( new JButton( "C" ) );
panel2.add( panel3 );
JPanel panel4 = new JPanel();
panel4.setLayout( new GridLayout( 4, 6) );
panel4.add( new JButton( "MC" ) );
panel4.add( new JButton( "7" ) );
panel4.add( new JButton( "8" ) );
panel4.add( new JButton( "9" ) );
panel4.add( new JButton( "/" ) );
panel4.add( new JButton( "" ) );
panel4.add( new JButton( "MR" ) );
panel4.add( new JButton( "4" ) );
panel4.add( new JButton( "5" ) );
panel4.add( new JButton( "6" ) );
panel4.add( new JButton( "*" ) );
panel4.add( new JButton( "sqrt" ) );
panel4.add( new JButton( "MS" ) );
panel4.add( new JButton( "1" ) );
panel4.add( new JButton( "2" ) );
panel4.add( new JButton( "3" ) );
panel4.add( new JButton( "-" ) );
panel4.add( new JButton( "1/x" ) );
panel4.add( new JButton( "M+" ) );
panel4.add( new JButton( "0" ) );
panel4.add( new JButton( "+/-" ) );
panel4.add( new JButton( "." ) );
panel4.add( new JButton( "+" ) );
panel4.add( new JButton( "=" ) );
panel2.add( panel4 );
cont.add( panel1 );
ActionCalculator a = new ActionCalculator();
for( Component comp : panel3.getComponents())
{
if( comp.getClass().toString().matches( ".*JButton.*" ) )
((JButton) comp).addActionListener( a );
}
for( Component comp : panel4.getComponents() )
{
((JButton) comp).addActionListener( a );
}
}
}
****************************ActionCalculator.java***************************
package calculator;
import java.awt.*;
import javax.swing.*;
public class calcGUI extends JFrame {
public JTextField displayField;
public JLabel memRegLabel;
public String memRegVal = "";
public double prevValue = 0;
public String operatorUsed = "=";
public boolean resetFlag = false;
public calcGUI () {
super ();
setSize( 400, 200 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container cont = getContentPane();
displayField = new JTextField("0", 12);
cont.setLayout( new BoxLayout( cont, BoxLayout.Y_AXIS ) );
cont.add( displayField = new JTextField( "0", 12 ) );
displayField.setHorizontalAlignment( JTextField.RIGHT );
JPanel panel1 = new JPanel();
panel1.setLayout( new BoxLayout( panel1, BoxLayout.X_AXIS ) );
cont.add( panel1 );
JPanel panel2 = new JPanel();
panel2.setLayout( new BoxLayout( panel2, BoxLayout.Y_AXIS ) );
panel1.add( panel2 );
JPanel panel3 = new JPanel();
panel3.setLayout( new GridLayout( 1, 23) );
panel3.add( memRegLabel = new JLabel( memRegVal = "" ) );
memRegLabel.setHorizontalAlignment( SwingConstants.CENTER );
panel3.add( new JButton( "Backspace" ) );
panel3.add( new JButton( "CE" ) );
panel3.add( new JButton( "C" ) );
panel2.add( panel3 );
JPanel panel4 = new JPanel();
panel4.setLayout( new GridLayout( 4, 6) );
panel4.add( new JButton( "MC" ) );
panel4.add( new JButton( "7" ) );
panel4.add( new JButton( "8" ) );
panel4.add( new JButton( "9" ) );
panel4.add( new JButton( "/" ) );
panel4.add( new JButton( "" ) );
panel4.add( new JButton( "MR" ) );
panel4.add( new JButton( "4" ) );
panel4.add( new JButton( "5" ) );
panel4.add( new JButton( "6" ) );
panel4.add( new JButton( "*" ) );
panel4.add( new JButton( "sqrt" ) );
panel4.add( new JButton( "MS" ) );
panel4.add( new JButton( "1" ) );
panel4.add( new JButton( "2" ) );
panel4.add( new JButton( "3" ) );
panel4.add( new JButton( "-" ) );
panel4.add( new JButton( "1/x" ) );
panel4.add( new JButton( "M+" ) );
panel4.add( new JButton( "0" ) );
panel4.add( new JButton( "+/-" ) );
panel4.add( new JButton( "." ) );
panel4.add( new JButton( "+" ) );
panel4.add( new JButton( "=" ) );
panel2.add( panel4 );
cont.add( panel1 );
ActionCalculator a = new ActionCalculator();
for( Component comp : panel3.getComponents())
{
if( comp.getClass().toString().matches( ".*JButton.*" ) )
((JButton) comp).addActionListener( a );
}
for( Component comp : panel4.getComponents() )
{
((JButton) comp).addActionListener( a );
}
}
}
************************Operations.java***************************************
package calculator;
public class Operations {
public double calculateResult() {
double totalVal = calculator.prevValue;
double val2 = Double.parseDouble
(calculator.displayField.getText());
if (calculator.operatorUsed.equals("+")) {
totalVal += val2;
}
if (calculator.operatorUsed.equals("-")) {
totalVal -= val2;
}
if (calculator.operatorUsed.equals("/")) {
totalVal /= val2;
}
if (calculator.operatorUsed.equals("*")) {
totalVal *= val2;
}
if (calculator.operatorUsed.equals("=")) {
totalVal = val2;
}
return totalVal;
}
}
There are following modules in my program:
- Calculator.java : contains main method, creates a calculator
instance.
- calcGUI.java: Builds the basic calculator GUI.
- ActionCalculator.java: Implements action listener class.
- Operations.java Implements the operations and does all the
calculations on operands depending upon the operator used.
The problem is that I can't get this program to work and it seems the
calculator object that has been created in Calculator.java cannot be
accessed in ActionCalculator.java and Operations.java even though the
object has been declared as a public static.
Here are the modules:
****************** Calculator.java*****************************
package calculator;
public class Calculator {
public static calcGUI calculator;
public static void main (String args[]) {
calculator = new calcGUI();
calculator.setVisible( true );
}
}
*********************calcGUI.java**********************************
package calculator;
import java.awt.*;
import javax.swing.*;
public class calcGUI extends JFrame {
public JTextField displayField;
public JLabel memRegLabel;
public String memRegVal = "";
public double prevValue = 0;
public String operatorUsed = "=";
public boolean resetFlag = false;
public calcGUI () {
super ();
setSize( 400, 200 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container cont = getContentPane();
displayField = new JTextField("0", 12);
cont.setLayout( new BoxLayout( cont, BoxLayout.Y_AXIS ) );
cont.add( displayField = new JTextField( "0", 12 ) );
displayField.setHorizontalAlignment( JTextField.RIGHT );
JPanel panel1 = new JPanel();
panel1.setLayout( new BoxLayout( panel1, BoxLayout.X_AXIS ) );
cont.add( panel1 );
JPanel panel2 = new JPanel();
panel2.setLayout( new BoxLayout( panel2, BoxLayout.Y_AXIS ) );
panel1.add( panel2 );
JPanel panel3 = new JPanel();
panel3.setLayout( new GridLayout( 1, 23) );
panel3.add( memRegLabel = new JLabel( memRegVal = "" ) );
memRegLabel.setHorizontalAlignment( SwingConstants.CENTER );
panel3.add( new JButton( "Backspace" ) );
panel3.add( new JButton( "CE" ) );
panel3.add( new JButton( "C" ) );
panel2.add( panel3 );
JPanel panel4 = new JPanel();
panel4.setLayout( new GridLayout( 4, 6) );
panel4.add( new JButton( "MC" ) );
panel4.add( new JButton( "7" ) );
panel4.add( new JButton( "8" ) );
panel4.add( new JButton( "9" ) );
panel4.add( new JButton( "/" ) );
panel4.add( new JButton( "" ) );
panel4.add( new JButton( "MR" ) );
panel4.add( new JButton( "4" ) );
panel4.add( new JButton( "5" ) );
panel4.add( new JButton( "6" ) );
panel4.add( new JButton( "*" ) );
panel4.add( new JButton( "sqrt" ) );
panel4.add( new JButton( "MS" ) );
panel4.add( new JButton( "1" ) );
panel4.add( new JButton( "2" ) );
panel4.add( new JButton( "3" ) );
panel4.add( new JButton( "-" ) );
panel4.add( new JButton( "1/x" ) );
panel4.add( new JButton( "M+" ) );
panel4.add( new JButton( "0" ) );
panel4.add( new JButton( "+/-" ) );
panel4.add( new JButton( "." ) );
panel4.add( new JButton( "+" ) );
panel4.add( new JButton( "=" ) );
panel2.add( panel4 );
cont.add( panel1 );
ActionCalculator a = new ActionCalculator();
for( Component comp : panel3.getComponents())
{
if( comp.getClass().toString().matches( ".*JButton.*" ) )
((JButton) comp).addActionListener( a );
}
for( Component comp : panel4.getComponents() )
{
((JButton) comp).addActionListener( a );
}
}
}
****************************ActionCalculator.java***************************
package calculator;
import java.awt.*;
import javax.swing.*;
public class calcGUI extends JFrame {
public JTextField displayField;
public JLabel memRegLabel;
public String memRegVal = "";
public double prevValue = 0;
public String operatorUsed = "=";
public boolean resetFlag = false;
public calcGUI () {
super ();
setSize( 400, 200 );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Container cont = getContentPane();
displayField = new JTextField("0", 12);
cont.setLayout( new BoxLayout( cont, BoxLayout.Y_AXIS ) );
cont.add( displayField = new JTextField( "0", 12 ) );
displayField.setHorizontalAlignment( JTextField.RIGHT );
JPanel panel1 = new JPanel();
panel1.setLayout( new BoxLayout( panel1, BoxLayout.X_AXIS ) );
cont.add( panel1 );
JPanel panel2 = new JPanel();
panel2.setLayout( new BoxLayout( panel2, BoxLayout.Y_AXIS ) );
panel1.add( panel2 );
JPanel panel3 = new JPanel();
panel3.setLayout( new GridLayout( 1, 23) );
panel3.add( memRegLabel = new JLabel( memRegVal = "" ) );
memRegLabel.setHorizontalAlignment( SwingConstants.CENTER );
panel3.add( new JButton( "Backspace" ) );
panel3.add( new JButton( "CE" ) );
panel3.add( new JButton( "C" ) );
panel2.add( panel3 );
JPanel panel4 = new JPanel();
panel4.setLayout( new GridLayout( 4, 6) );
panel4.add( new JButton( "MC" ) );
panel4.add( new JButton( "7" ) );
panel4.add( new JButton( "8" ) );
panel4.add( new JButton( "9" ) );
panel4.add( new JButton( "/" ) );
panel4.add( new JButton( "" ) );
panel4.add( new JButton( "MR" ) );
panel4.add( new JButton( "4" ) );
panel4.add( new JButton( "5" ) );
panel4.add( new JButton( "6" ) );
panel4.add( new JButton( "*" ) );
panel4.add( new JButton( "sqrt" ) );
panel4.add( new JButton( "MS" ) );
panel4.add( new JButton( "1" ) );
panel4.add( new JButton( "2" ) );
panel4.add( new JButton( "3" ) );
panel4.add( new JButton( "-" ) );
panel4.add( new JButton( "1/x" ) );
panel4.add( new JButton( "M+" ) );
panel4.add( new JButton( "0" ) );
panel4.add( new JButton( "+/-" ) );
panel4.add( new JButton( "." ) );
panel4.add( new JButton( "+" ) );
panel4.add( new JButton( "=" ) );
panel2.add( panel4 );
cont.add( panel1 );
ActionCalculator a = new ActionCalculator();
for( Component comp : panel3.getComponents())
{
if( comp.getClass().toString().matches( ".*JButton.*" ) )
((JButton) comp).addActionListener( a );
}
for( Component comp : panel4.getComponents() )
{
((JButton) comp).addActionListener( a );
}
}
}
************************Operations.java***************************************
package calculator;
public class Operations {
public double calculateResult() {
double totalVal = calculator.prevValue;
double val2 = Double.parseDouble
(calculator.displayField.getText());
if (calculator.operatorUsed.equals("+")) {
totalVal += val2;
}
if (calculator.operatorUsed.equals("-")) {
totalVal -= val2;
}
if (calculator.operatorUsed.equals("/")) {
totalVal /= val2;
}
if (calculator.operatorUsed.equals("*")) {
totalVal *= val2;
}
if (calculator.operatorUsed.equals("=")) {
totalVal = val2;
}
return totalVal;
}
}