Results 1 to 1 of 1
Thread: Calculator Feedback
- 09-20-2011, 02:23 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 22
- Rep Power
- 0
Calculator Feedback
Hello everyone!
I just finished developing my first program -- a calculator -- and I'm looking for some feedback regarding my programming.
- Please don't worry about offending me -- be honest.
- You should be able to just copy-paste it into a project and it'll work -- I'm using Eclipse.
- I've added as many comments as I could
- There are 2 pages in total
CALCULATOR.JAVA
CALCEVENT.JAVAJava Code:import java.awt.*; import javax.swing.*; public class Calculator extends JFrame{ // For Listeners CalcEvent calcEvent = new CalcEvent(this); // DISPLAY JPanel display = new JPanel(); JLabel dispText = new JLabel("0"); // BUTTONS JPanel buttons = new JPanel(); JButton bClear = new JButton("C"); JButton bPlusMinus = new JButton("+/-"); JButton bDivide = new JButton("/"); JButton bMult = new JButton("*"); JButton bSeven = new JButton("7"); JButton bEight = new JButton("8"); JButton bNine = new JButton("9"); JButton bSub = new JButton("-"); JButton bFour = new JButton("4"); JButton bFive = new JButton("5"); JButton bSix = new JButton("6"); JButton bAdd = new JButton("+"); JButton bOne = new JButton("1"); JButton bTwo = new JButton("2"); JButton bThree = new JButton("3"); JButton bDecimal = new JButton("."); JButton bZero = new JButton("0"); JButton bEquals = new JButton("="); JButton bEmpty = new JButton(""); JButton bEmpty2 = new JButton(""); public Calculator(){ // CALCULATOR SETTINGS super("Calculator"); // Sets window title setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(210, 230); setResizable(false); // Prohibits resizing of window setFocusable(true); // Sets Calculator focusable for KeyEvents // ADD LISTENERS // Key Listener (Keys Typed) addKeyListener(calcEvent); // Action Listener (Clicking each button) bOne.addActionListener(calcEvent); bTwo.addActionListener(calcEvent); bThree.addActionListener(calcEvent); bFour.addActionListener(calcEvent); bFive.addActionListener(calcEvent); bSix.addActionListener(calcEvent); bSeven.addActionListener(calcEvent); bEight.addActionListener(calcEvent); bNine.addActionListener(calcEvent); bClear.addActionListener(calcEvent); bPlusMinus.addActionListener(calcEvent); bDivide.addActionListener(calcEvent); bMult.addActionListener(calcEvent); bSub.addActionListener(calcEvent); bAdd.addActionListener(calcEvent); bDecimal.addActionListener(calcEvent); bZero.addActionListener(calcEvent); bEquals.addActionListener(calcEvent); // Set Display Layout FlowLayout displayLayout = new FlowLayout(FlowLayout.CENTER, 5, 5); setLayout(displayLayout); add(dispText); dispText.setHorizontalAlignment(JLabel.RIGHT); // Align right... just to match most other calcs dispText.setPreferredSize(new Dimension(185, 50)); // Increase width and height of JLabel // Set Button Rows GridLayout layout1To3 = new GridLayout(5, 4, 2, 2); // 5 Rows, 4 Columns, 2px margins buttons.setLayout(layout1To3); buttons.add(bClear); buttons.add(bPlusMinus); buttons.add(bDivide); buttons.add(bMult); buttons.add(bSeven); buttons.add(bEight); buttons.add(bNine); buttons.add(bSub); buttons.add(bFour); buttons.add(bFive); buttons.add(bSix); buttons.add(bAdd); buttons.add(bOne); buttons.add(bTwo); buttons.add(bThree); buttons.add(bEmpty); //These empties are just filler... buttons.add(bEmpty2); //---------------^ buttons.add(bZero); buttons.add(bDecimal); buttons.add(bEquals); add(buttons); setVisible(true); } public static void main(String[] args) { Calculator frame = new Calculator(); } }
And that's it.Java Code:import java.awt.event.*; public class CalcEvent implements KeyListener, ActionListener { // MEMBER VARIABLES Calculator gui; String leftExp = null; String rightExp = null; int side = 0; String oper = null; // I'm not really sure how to describe this. I just know it allows me to access the other page (Calculator.java) CalcEvent(Calculator in){ gui = in; } // ACTION LISTENER - Required Method public void actionPerformed(ActionEvent event){ String command = event.getActionCommand(); calcHandler(command); } // KEY LISTENER - Required Methods public void keyPressed(KeyEvent input){} public void keyReleased(KeyEvent input){} public void keyTyped(KeyEvent input){ char key = input.getKeyChar(); // If enter key is typed, interpret as "=". Otherwise proceed with key. Send either result to calcHandler() if(key == KeyEvent.VK_ENTER){ calcHandler("="); }else{ calcHandler("" + key); } } // CALCULATOR INPUT HANDLER - gets argument from Action/Key Listeners public void calcHandler(String input){ String displayText = gui.dispText.getText(); if(side == 0){ //We're on the left side of the equation if(input.equals("0") || input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4") || input.equals("5") || input.equals("6") || input.equals("7") || input.equals("8") || input.equals("9") || input.equals(".")){ //User has entered a number (or decimal) if(leftExp == null){ leftExp = input; // This is the first character entered }else{ leftExp += input; // This adds to the already existing characters } // Apply changes gui.dispText.setText(leftExp); }else if(displayText != null && input.equals("/") || input.equals("*") || input.equals("-") || input.equals("+")){ // User has entered an operator oper = input; side = 1; }else if(input.equals("+/-") && leftExp != null){ // Makes selection positive or negative if(leftExp.indexOf("-") == -1){ // Working from positive to negative leftExp = "-" + leftExp; }else{ // Working from negative to positive (this just drops the 1st char (ie: "-")) leftExp = leftExp.substring(1); } // Apply changes gui.dispText.setText(leftExp); }else{ // User has entered something else if(input.equals("C")){ // Clear calculator clearCalc(); }else if(input.equals("=")){ // Premature equals... silly user // Do nothing } } }else if(side == 1){ //Now we're in the right side of the equation if(input.equals("0") || input.equals("1") || input.equals("2") || input.equals("3") || input.equals("4") || input.equals("5") || input.equals("6") || input.equals("7") || input.equals("8") || input.equals("9") || input.equals(".")){ //User has entered a number (or decimal) if(oper.equals("/") && input.equals("0")){ // This makes sure that user doesn't divide by zero. calcError("Cannot divide by Zero"); }else{ if(rightExp == null){ rightExp = input; // This is the first character entered }else{ rightExp += input; // This adds to the already existing characters } // Apply changes gui.dispText.setText(rightExp); } }else if(displayText != null && input.equals("/") || input.equals("*") || input.equals("-") || input.equals("+")){ // User has entered an operator -- error (for now.. I'll add functionality to chain equations together later) calcError("-E-"); }else{ // User has entered something else if(input.equals("C")){ // Clear calculator clearCalc(); }else if(input.equals("=")){ // Send everything to final method for calculation. Convert to double first. double left = Double.parseDouble(leftExp); double right = Double.parseDouble(rightExp); // Calls calcMath method, returns double, converts double to string gui.dispText.setText(Double.toString(calcMath(left, oper, right))); // Clear variables leftExp = null; rightExp = null; oper = null; side = 0; } } } } // CLEAR CALCULATOR public void clearCalc(){ // Clear all variables gui.dispText.setText("0"); leftExp = null; rightExp = null; oper = null; side = 0; } // CALCULATOR ERROR public void calcError(String err){ // Insert custom error message then clear all variables gui.dispText.setText(err); leftExp = null; rightExp = null; oper = null; side = 0; } // FINAL EQUATION CALCULATION -- Return double public double calcMath(double l, String o, double r){ double result = 0; if("/".equals(o)){ result = l/r; }else if("*".equals(o)){ result = l*r; }else if("-".equals(o)){ result = l-r; }else{ result = l+r; } return result; } }
Thanks!
Similar Threads
-
Need feedback for my program.
By Pojahn_M in forum New To JavaReplies: 3Last Post: 08-12-2011, 08:12 PM -
About focusLost and keyTyped, need feedback
By mine0926 in forum NetBeansReplies: 0Last Post: 06-26-2010, 10:08 AM -
I need feedback on my TicTacToe game
By kiregad in forum New To JavaReplies: 4Last Post: 03-21-2010, 10:09 PM -
website section feedback
By MuslimCoder in forum Reviews / AdvertisingReplies: 0Last Post: 02-25-2009, 07:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks