Results 1 to 4 of 4

Thread: Java Calculator

  1. #1
    SilverSoul is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Java Calculator

    Im am doing a java Calculator and I got it working for the first part except when I use it and the first Operator I use doesn't work and the last Operator I use doesn't work, and I'm at a lost on how to code the program to work. To see what I'm talking run the CalculatorEngineCMD

    Heres the CalculatorEngine Code
    Java Code:
    public class CalculatorEngine {
    	
    
    	private double result;
    	private char operator;
    	private String buffer;
    	
        /**
         * implment an empty constructor
         */
    	public CalculatorEngine() {
    		result=0;
    		operator='=';
    		buffer="";	
    	}
    	
        /**
         * press a character c. The valid value of c is 
         * numbers 0, 1, 2, ..., 9
         * operators +, -, *, /
         * the equality sign =
         * R to reset an engine
         */
    	public void press(char c) {	
    	
    		
    		if(c >= '0' && c<= '9') {
    			pressNumber(c);
    		} else if (c == '=') {
    			calculate();
    		} else if (c == 'R') {
    			reset();
    		} else if (c == 'Q'){
    			Quit();
    		} else{
    			pressOperator(c);
    		}
    		
    	}
    	
    	private void reset() {
    		result=0;
    		operator='=';
    		buffer="";	
    	}
    	
    	private void pressOperator(char c) {
    		
    		operator=c;
    		
    		if (operator=='+'){
    			result = result + Double.parseDouble(buffer);
    			buffer="";
    		}
    		else if (operator=='-'){
    			double a=Double.parseDouble(buffer);
    			result= a - result;
    			buffer="";
    		}
    		else if (operator=='*'){
    			double a=Double.parseDouble(buffer);
    			result= a * result;
    			buffer="";
    		}
    		else if (operator=='/'){
    			double a=Double.parseDouble(buffer);
    			result= a / result;
    			buffer="";
    		}
    		
    		
    	}
    	
    	
    	private void pressNumber(char c) {
    		buffer = buffer + c;
    	}
    	
    	private void calculate() {
    
    		buffer="";
    			
    	}
    	
    	
        /**
         * return the current result of a calculator
         */
    	public double getResult(){ 
    	
    		return result;
    	}
    	
        /**
         * return the current buffer number
         * for example if users PRESS '1' and PRESS '4', the current buffer is 14
         */
    	public double getDisplayBuffer() {
    		
    		double val = 0;
    		if(buffer.length() > 0) {
    			val = Double.parseDouble(buffer);
    		}
    		else{
    			val=0;
    		}
    		return val;
    	
    	
    	}
    	public String Quit(){
    		String s="Goodbye!";
    		return s;
    	}
    }

    Heres the CalculatorEngineCMD program to run the Calculator

    Java Code:
    public class CalculatorCMD {
    
    	public static void main(String[] args) {
    
    		CalculatorEngine engine = new CalculatorEngine();
    
    		engine.press('2');
    		System.out.println("Buffer:"+engine.getDisplayBuffer());
    		
    		engine.press('+');
    		System.out.println("Result:"+engine.getResult());
    		
    		engine.press('5');
    		System.out.println("Buffer:"+engine.getDisplayBuffer());
    		
    		engine.press('*');
    		System.out.println("Result:"+engine.getResult());
    		
    		engine.press('2');
    		System.out.println("Buffer:"+engine.getDisplayBuffer());
    		
    		engine.press('=');
    		System.out.println("Result:"+engine.getResult());
    		
    	}
    
    }
    Last edited by SilverSoul; 04-25-2012 at 07:04 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Java Calculator

    What do you mean by "doesn't work"? There are many ways for code to NOT WORK.
    Please explain what happens.

    Try debugging the code by adding some println statements to print out the values of variables as they are changed and used and to show execution flow. The print out will show you what the computer sees and does so you can change the code to have it do what you want it to do.

    In the pressOperator method there should be and ending else statement after the if/else if statements that prints out a message when none of the above were true.
    Last edited by Norm; 04-25-2012 at 06:55 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SilverSoul is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Java Calculator

    I'm sorry try running the CalculatorCMD program again. If you run it the calculator should run 2 + 5 * 2 and the output should be result = 14, but the output is result = 10. It totally ignores the 2 + that's in front on the 5.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Java Calculator

    Have you Tried debugging the code by adding some println statements to print out the values of variables as they are changed and used and to show execution flow. The print out will show you what the computer sees and does so you can change the code to have it do what you want it to do.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Calculator in java
    By andrewparent in forum New To Java
    Replies: 1
    Last Post: 04-20-2012, 08:36 PM
  2. java stack RPN calculator
    By ali1 in forum New To Java
    Replies: 0
    Last Post: 10-30-2011, 09:04 PM
  3. Java Date Calculator
    By vodkanJava in forum New To Java
    Replies: 3
    Last Post: 03-16-2011, 09:45 PM
  4. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  5. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •