I have hit a brick wall whilst attempting to create a two-class Java calculator. I have the code for an interface which works and displays ok but creating a separate class 'CalcEngine' to do the actual calculations has proven to be beyond me.
I'd appreciate it if someone could kick start things for me and create a class calcEngine which works with the interface class and allows input when from single button i.e. if one is pressed on the calc then 1 displays onscreen.
Please note I'm not asking someone to do the whole thing for me as I want to learn and I'm confident I can do the rest including addition subtraction etc. Once I get over the obstacle of getting the two classes to communicate. Any and all assistance would be very much appreciated.
Please see the calcInterface class code below:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
/**
*A Class that operates as the framework for a calculator.
*No calculations are performed in this section
*/
public class CalcFrame
implements ActionListener
{
private CalcEngine calc;
private JFrame frame;
private JTextField display;
private JLabel status;
/**
* Constructor for objects of class GridLayoutExample
*/
public CalcFrame()
{
makeFrame();
//calc = engine;
}
/**
* This allows you to quit the calculator.
*/
// Alows the class to quit.
private void quit()
{
System.exit(0);
}
// Calls the dialog frame with the information about the project.
private void showAbout()
{
JOptionPane.showMessageDialog(frame,
"Group Project",
"About Calculator Group Project",
JOptionPane.INFORMATION_MESSAGE);
}
private void makeFrame()
{
frame = new JFrame("Group Project Calculator");
makeMenuBar(frame);
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));
/**
* Insert a text field
*/
display = new JTextField();
contentPane.add(display, BorderLayout.NORTH);
//Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
contentPane.add(new JButton("1"));
contentPane.add(new JButton("2"));
contentPane.add(new JButton("3"));
contentPane.add(new JButton("4"));
contentPane.add(new JButton("5"));
contentPane.add(new JButton("6"));
contentPane.add(new JButton("7"));
contentPane.add(new JButton("8"));
contentPane.add(new JButton("9"));
contentPane.add(new JButton("0"));
contentPane.add(new JButton("+"));
contentPane.add(new JButton("-"));
contentPane.add(new JButton("/"));
contentPane.add(new JButton("*"));
contentPane.add(new JButton("="));
contentPane.add(new JButton("C"));
contentPane.add(buttonPanel, BorderLayout.CENTER);
//status = new JLabel(calc.getAuthor());
//contentPane.add(status, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
/**
* Create the main frame's menu bar.
* The frame that the menu bar should be added to.
*/
private void makeMenuBar(JFrame frame)
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
// create the File menu
menu = new JMenu("File");
menubar.add(menu);
// create the Quit menu with a shortcut "Q" key.
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { quit(); }
});
menu.add(item);
// Adds an about menu.
menu = new JMenu("About");
menubar.add(menu);
// Displays
item = new JMenuItem("Calculator Project");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { showAbout(); }
});
menu.add(item);
}
/**
* An interface action has been performed.
* Find out what it was and handle it.
* @param event The event that has occured.
*/
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if(command.equals("0") ||
command.equals("1") ||
command.equals("2") ||
command.equals("3") ||
command.equals("4") ||
command.equals("5") ||
command.equals("6") ||
command.equals("7") ||
command.equals("8") ||
command.equals("9")) {
int number = Integer.parseInt(command);
calc.numberPressed(number);
}
else if(command.equals("+")) {
calc.plus();
}
else if(command.equals("-")) {
calc.minus();
}
else if(command.equals("=")) {
calc.equals();
}
else if(command.equals("C")) {
calc.clear();
}
else if(command.equals("?")) {
}
// else unknown command.
redisplay();
}
/**
* Update the interface display to show the current value of the
* calculator.
*/
private void redisplay()
{
display.setText("" + calc.getDisplayValue());
}
/**
* Toggle the info display in the calculator's status area between the
* author and version information.
*/
}