Results 1 to 5 of 5

Thread: post fix help?

  1. #1
    jrakidzich is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default post fix help?

    /*
    Hey all, I'm in school for Mobile App development and this Java course is a bit over my head. We are doing a postfix evaluator assignment and I'm lost when it gets to the program processing and sending me the right output. I know that the class Eval is 100% accurate, my instructor said as much, but I cant seem to program it right to get the answer. Can anyone point me in the right direction? And if you could also help me UNDERSTAND what it is that I need to do too I'd appreciate it.

    also this is from my book assigning some things to me, this is what I need to figure out


    "Testing
    You will need to write a driver for the PostfixEvaluator class. This driver should create a PostfixEvaluator object, read one or more expressions, and report the result. It will also have to catch the exception PostfixEvaluator.SyntaxErrorException. A white-box approach to testing would lead you to consider the following test cases. First, you want to exercise each path in the evalOp method by entering a simple expression that uses each operator. Then you need to exercise the paths through eval by trying different orderings and multiple occurrences of the operators. These tests exercise the normal cases, so you next need to test for possible syntax errors. Consider the following cases: an operator without any operands, a single operand, an extra operand, an extra operator, a variable name, and finally an empty string."
    (Koffman 176)
    Koffman, Elliot B. Data Structures: Abstraction and Design Using Java, 2nd Edition. John Wiley & Sons, 012010. VitalBook file.



    */


    Java Code:
    import java.util.*;
    
    public class Eval {
    	
    	
    	public Eval(String string) {
    		// TODO Auto-generated constructor stub
    	}
    	
    	
    	
    	//label all operators
    	public static final String Operators = "+-*/";
    	
    	//Stack for the operands
    	//Data field
    	private Stack<Integer> operandStack;
    	
    	
    	// ###################### methods ###############
    	// evaluates post fix notation. 
    	/* when and operator feeds into the stack it will bypass the stack instead 
    	 * then pop the top two operands off the stack and apply the operator
    
    
            */
    	
    	private int evalOp(char op){
    		
    		// pop the top two operands
    		int a = operandStack.pop();
    		int b = operandStack.pop();
    		int result = 0;
    		
    		// this method reads the operators and assigns operations
    		switch (op){
    			
    		case '+' : result = b + a;
    		case '-' : result = b - a;
    		case '/' : result = b / a;
    		case '*' : result = b * a;
    		
    		}
    		return result;
    	}
    	
    	
    	//this next method determines if a Character is an operator
    	
    	private boolean isOperator(char ch){
    		return Operators.indexOf(ch) != -1;//returns true if operator
    	}
    	
    	//**Evaluates a post fix expression
    	
    	public int eval(String expression) throws SyntaxErrorException {
    		operandStack = new Stack<Integer>();//empty stack
    		
    		String[] tokens = expression.split("\\s+");
    		try{
    			//processor
    			for (String nextToken : tokens){
    				char firstChar = nextToken.charAt(0);
    				
    				//if it's a number get value and push to stack
    				if (Character.isDigit(firstChar)){
    					int value = Integer.parseInt(nextToken);
    					operandStack.push(value);
    				}
    				//if it's an operator, evaluate and push
    				else if (isOperator(firstChar)){
    					int result = evalOp(firstChar);
    				}
    				else{
    					// invalid char error
    					throw new SyntaxErrorException("Invalid Character: " + firstChar);
    				}
    			}// end for statement
    			
    			//pop result from stack
    		int answer = operandStack.pop();
    		//if stack is empty
    		if (operandStack.empty()){
    			return answer;
    		}
    			else{
    				//if stack is not empty
    			throw new SyntaxErrorException("Error: Stack should be empty");
    			}
    			
    		}
    		catch (EmptyStackException ex){
    			throw new SyntaxErrorException("Error: Nothing to 'POP' The stack is empty");
    		}
    		
    
    		
    	}
    	
    	//NClass to report errors
    	public static class SyntaxErrorException extends Exception{
    		
    		//method to make message a String in this class
    		SyntaxErrorException(String message){
    
    			super(message);//read the message from the super
    		}
    	}
    
    	public static void main (String[] args){
    		
    		Eval evaluator = new Eval("4 7 * 20 -");// Eval object
    		
    		String str = new String("4 7 * 20 -");//string to hold the stack data
    		
    		
    		
    		
    		try {
    			evaluator.eval(Operators);//try to implement eval method on string to return answer of 8
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    		
    
    		
    		
    		
    	}
    
    	
    	
    	
    	
    
    }
    Last edited by jrakidzich; 03-16-2014 at 02:58 AM.

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

    Default Re: post fix help?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    One thing you need to do is look at the methods the code calls and see if they return a value and then be sure to assign that value to a variable so it can be used or printed.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jrakidzich is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: post fix help?

    Thanks for the response and that input. I've been trying to work this out on my own from the book, but at this point it doesn't give me any info, it's sink or swim. I'm having troubles with calling the methods properly and inputting the equation into the evaluator. To be honest I have not absorbed much from this online school, I know what a method is and what a variable is and how to create them, but as far as doing much else with them I'm lost. I only have this much because the book pretty much spelled it out for me. the public int eval in the Eval class is what my processor is... I believe. It's really the only public thing in the class, unless I need to make a constructor that will let me do more with the class publicly?

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

    Default Re: post fix help?

    When a method returns a value, the code should assign that value to a variable:
    aVar = refToClass.methodReturnsValue(); // save returned value in aVar

    The eval() method returns a value. Assign that value to a variable.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jrakidzich is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: post fix help?

    Thanks! That is what I needed to get started. I really appreciate it!

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2009, 03:15 AM
  2. my first post
    By kaviyasivashanmugham in forum New To Java
    Replies: 2
    Last Post: 03-13-2009, 09:50 AM
  3. First post out of the way..
    By sirwiggles in forum Introductions
    Replies: 0
    Last Post: 02-06-2009, 10:44 PM
  4. First post ever
    By pbpersson in forum Introductions
    Replies: 4
    Last Post: 08-16-2008, 05:30 AM

Tags for this Thread

Posting Permissions

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