/**
* The main class of a simple calculator. Create one of these and you'll
* get the calculator on screen.
*
* @author David J. Barnes and Michael Kolling
* @version 31 July 2000
*/
public class Calculator
{
private CalcEngine engine;
private UserInterface gui;
/**
* Create a new calculator and show it.
*/
public Calculator()
{
engine = new CalcEngine();
gui = new UserInterface(engine);
}
/**
* In case the window was closed, show it again.
*/
public void show()
{
gui.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* A graphical user interface for the calculator. No calculation is being
* done here. This class is responsible just for putting up the display on
* screen. It then refers to the "CalcEngine" to do all the real work.
*
* @author David J. Barnes and Michael Kolling
* @version 31 July 2000
*/
public class UserInterface
implements ActionListener
{
private CalcEngine calc;
private boolean showingAuthor;
private JFrame frame;
private JTextField display;
private JLabel status;
/**
* Create a user interface for a given calcEngine.
*/
public UserInterface(CalcEngine engine)
{
calc = engine;
showingAuthor = true;
makeFrame();
frame.setVisible(true);
}
/**
* Make this interface visible again. (Has no effect if it is already
* visible.)
*/
public void setVisible(boolean visible)
{
frame.setVisible(visible);
}
/**
* Make the frame for the user interface.
*/
private void makeFrame()
{
frame = new JFrame(calc.getTitle());
JPanel contentPane = (JPanel)frame.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));
display = new JTextField();
contentPane.add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
addButton(buttonPanel, "7");
addButton(buttonPanel, "8");
addButton(buttonPanel, "9");
addButton(buttonPanel, "C");
addButton(buttonPanel, "4");
addButton(buttonPanel, "5");
addButton(buttonPanel, "6");
addButton(buttonPanel, "?");
addButton(buttonPanel, "1");
addButton(buttonPanel, "2");
addButton(buttonPanel, "3");
addButton(buttonPanel, "*");
addButton(buttonPanel, "0");
addButton(buttonPanel, "+");
addButton(buttonPanel, "-");
addButton(buttonPanel, "=");
contentPane.add(buttonPanel, BorderLayout.CENTER);
status = new JLabel(calc.getAuthor());
contentPane.add(status, BorderLayout.SOUTH);
frame.pack();
}
/**
* Add a button to the button panel.
*/
private void addButton(Container panel, String buttonText)
{
JButton button = new JButton(buttonText);
button.addActionListener(this);
panel.add(button);
}
/**
* An interface action has been performed. Find out what it was and
* handle it.
*/
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.multiply();
else if(command.equals("="))
calc.equals();
else if(command.equals("C"))
calc.clear();
else if(command.equals("?"))
showInfo();
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.
*/
private void showInfo()
{
if(showingAuthor)
status.setText(calc.getVersion());
else
status.setText(calc.getAuthor());
showingAuthor = !showingAuthor;
}
}
/**
* The main part of the calculator doing the calculations.
*
* @author David J. Barnes and Michael Kolling
* @version 0.1 (incomplete)
*/
public class CalcEngine
{
// Put instance variables here.
int entry=0;
int result=0;
int display;
int lastEntry =0;
/**
* Create a CalcEngine instance. Initialise its state so that it is ready
* for use.
*/
public CalcEngine()
{
entry = display;
}
/**
* Return the value that should currently be displayed on the calculator
* display.
*/
public int getDisplayValue()
{
return entry;
}
/**
* A number button was pressed. Do whatever you have to do to handle it.
* The number value of the button is given as a parameter.
*/
public void numberPressed(int number)
{
entry = entry * 10 + number;
}
/**
* The 'plus' button was pressed.
*/
public void plus()
{
lastEntry = entry;
entry = 0;
result = entry + lastEntry;
}
/**
* The 'minus' button was pressed.
*/
public void minus()
{
lastEntry = entry;
entry = 0;
result = entry - lastEntry;
}
/**
* The 'multiply' button was pressed.
*/
public void multiply()
{
lastEntry = entry;
entry = 0;
result = entry * lastEntry;
}
/**
* The '=' button was pressed.
*/
public void equals()
{
}
/**
* The 'C' (clear) button was pressed.
*/
public void clear()
{
entry = 0;
lastEntry = 0;
result = 0;
display = 0;
}
/**
* Return the title of this calculation engine.
*/
public String getTitle()
{
return "Calculator";
}
/**
* Return the author of this engine. This string is displayed as it is,
* so it should say something like "Written by H. Simpson".
*/
public String getAuthor()
{
return "Written by Scott, Krutika, & Leah";
}
/**
* Return the version number of this engine. This string is displayed as
* it is, so it should say something like "Version 1.1".
*/
public String getVersion()
{
return "Version 1.0";
}
}
// some variable identifying last operation

New Topic/Question
This topic is locked



MultiQuote





|