Once I made a simple Java Calculator - I have decided to make it open source :)
so here goes the complete code for this java calculator.
import java.awt.FlowLayout; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Calculator { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { CalculatorFrame frame = new CalculatorFrame(); frame.setSize(800, 211); JLabel label= new JLabel("<html><FONT SIZE=28><font color=#FF0000>A</font><font color=#00FF00>H</font><font color=00FFFF>M</font><font color=#FFFF00>A</font><font color=#0000FF>D</font>" + "<font color=FF00FF> O</font><font color=#FF0000>S</font><font color=FF00FF>A</font><font color=#C0C0C0>M</font><font color=#FF0000>A</font></FONT> presents <br></FONT>" +"NUST School of Electrical Engineering<br> and Computer Science<br>" + "<br>Scientific Calculator v3.0<br>" + "<br>FB.ME/FRONTLINE.FIGHTER<br>FRONTLINE.FIGHTER@LIVE.COM"+ "<<br>Any Comments / Critisim is HeartIly accepted",JLabel.RIGHT); Container contentPane = frame.getContentPane(); contentPane.add(label); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } } /** * A frame with a calculator panel. */ class CalculatorFrame extends JFrame { public CalculatorFrame() { setTitle("Scientific Calculator by Mohammed Ahmad Osama - fb.me/frontline.fighter"); CalculatorPanel panel = new CalculatorPanel(); add(panel); pack(); } } /** * A panel with calculator buttons and a result display. */ class CalculatorPanel extends JPanel { public CalculatorPanel() { setLayout(new BorderLayout()); result = 0; lastCommand = "="; start = true; // add the display display = new JButton("0.00000"); display.setEnabled(false); add(display, BorderLayout.NORTH); display.setBackground(Color.cyan); ActionListener insert = new InsertAction(); ActionListener command = new CommandAction(); // add the buttons in a n x n grid panel = new JPanel(); panel.setLayout(new GridLayout(6, 6)); //1 addButton("7", insert); addButton("8", insert); addButton("9", insert); addButton("/", command); addButton("Sin", command); addButton("ArcSin", command); //2 addButton("4", insert); addButton("5", insert); addButton("6", insert); addButton("*", command); addButton("Cos", command); addButton("ArcCos", command); //3 addButton("1", insert); addButton("2", insert); addButton("3", insert); addButton("-", command); addButton("Tan", command); addButton("ArcTan", command); //4 addButton("0", insert); addButton(".", insert); addButton("=", command); addButton("+", command); addButton("AC", command); addButton("Sinh", command); //5 addButton("Cbrt", command); addButton("Cube", command); addButton("Ln", command); addButton("Log", command); addButton("e", command); addButton("cosh", command); //6 addButton("Sqrt", command); addButton("Sqr", command); addButton("|x|", command); addButton("Inverse", command); addButton("!", command); addButton("Tanh", command); add(panel, BorderLayout.CENTER); } /** * Adds a button to the center panel. * @param label the button label * @param listener the button listener */ private void addButton(String label, ActionListener listener) { JButton button = new JButton(label); button.setForeground(Color.magenta); button.addActionListener(listener); panel.add(button); } /** * This action inserts the button action string to the end of the display text. */ private class InsertAction implements ActionListener { public void actionPerformed(ActionEvent event) { String input = event.getActionCommand(); if (start) { display.setText(""); start = false; } display.setText(display.getText() + input); } } /** * This action executes the command that the button action string denotes. */ private class CommandAction implements ActionListener { public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (start) { if (command.equals("-")) { display.setText(command); start = false; } else lastCommand = command; } else { calculate(Double.parseDouble(display.getText())); lastCommand = command; start = true; } } } /** * Carries out the pending calculation. * @param x the value to be accumulated with the prior result. */ // Evaluate n! //double i,j=1; public void calculate(double x) { // x=Math.toDegrees(x); if (lastCommand.equals("+")) result += x; else if (lastCommand.equals("-")) result -= x; else if (lastCommand.equals("*")) result *= x; else if (lastCommand.equals("/")) result /= x; else if (lastCommand.equals("Inverse")) result = 1/x; else if (lastCommand.equals("=")) result = x; else if (lastCommand.equals("AC")) result = 0; else if (lastCommand.equals("Sin")) result =Math.sin(x); else if (lastCommand.equals("Cos")) result =Math.cos(x); else if (lastCommand.equals("Tan")) result =Math.tan(x); else if (lastCommand.equals("ArcSin")) result =Math.asin(x); else if (lastCommand.equals("ArcCos")) result =Math.acos(x); else if (lastCommand.equals("ArcTan")) result =Math.atan(x); else if (lastCommand.equals("Cosh")) result = (Math.exp(x) + Math.exp(-x)) / 2.0; else if (lastCommand.equals("Sin")) result = (Math.exp(x) - Math.exp(-x)) / 2.0; else if (lastCommand.equals("Tanh")) result =((Math.exp(x) - Math.exp(-x)) /(Math.exp(x) + Math.exp(-x))); else if (lastCommand.equals("Sqrt")) result = Math.sqrt(x); else if (lastCommand.equals("Cbrt")) result = Math.cbrt(x); else if (lastCommand.equals("Sqr")) result =Math.pow(x,2); else if (lastCommand.equals("Cube")) result =Math.pow(x,3); else if (lastCommand.equals("e")) result = Math.exp(x); else if (lastCommand.equals("|x|")) result = Math.abs(x); else if (lastCommand.equals("Ln")) result = Math.log(x); else if (lastCommand.equals("Log")) result = Math.log10(x); // else if (lastCommand.equals("!")) result = j; // else if (lastCommand.equals("=")) result = x; // else if (lastCommand.equals("=")) result = x; // else if (lastCommand.equals("=")) result = x; display.setText("" + result); } private JButton display; private JPanel panel; private double result; private String lastCommand; private boolean start; }
No comments:
Post a Comment