Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    when I call this method I get the answer in binary :S
    HELP PLEASE MY EXAM IS WITHIN 4 HOURS....
    this is supposed to use stacks in order to solve postfix notation...
    u take a string push the values till u get an operator then pop 2 values perform the operation that u have then put the result back into stack...
    String x = "23+"; // brings back 101 which is 5 in binary :S
    public static int evaluate(String S){
    		StackObj stack = new StackObj(S.length());
    		int result = 0;
    		for(int i = 0; i < S.length(); i++){
    			switch (S.charAt(i)){
    			case '0':
    			case '1':	
    			case '2':
    			case '3':	
    			case '4':
    			case '5':
    			case '6':
    			case '7':
    			case '8':
    			case '9':stack.push(new Integer (S.charAt(i))); break;
    			case '+':result = ((Integer)stack.pop()).intValue() + ((Integer)stack.pop()).intValue();  
    			 		 stack.push(new Integer (result)); break;	
    			case '-':result = ((Integer)stack.pop()).intValue() - ((Integer)stack.pop()).intValue();  
    			 		 stack.push(new Integer (result)); break;	
    			case '/':result = ((Integer)stack.pop()).intValue() / ((Integer)stack.pop()).intValue();  
    			 		 stack.push(new Integer (result)); break;	
    			case '*':result = ((Integer)stack.pop()).intValue() * ((Integer)stack.pop()).intValue();  
    					 stack.push(new Integer (result)); break;
    			}
    		}
    		return ((Integer)stack.pop()).intValue();
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    If you want help, you'll have to provide an SSCCE that demonstrates the problem- for example, we have no idea what StackObj is doing.

    You'll also want to avoid mentioning your deadlines like that. It makes you seem impatient, which will make people not want to help you. There are hundreds of posts here, each with an urgent user waiting on a reply, and your time is not more important than theirs.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    You are absolutely right...
    F*** it with my midterms...
    now stackobj is a class that i made which works exactly like a stack but for objects...
    THANK YOU...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    Quote Originally Posted by Medo Almasry View Post
    You are absolutely right...
    F*** it with my midterms...
    now stackobj is a class that i made which works exactly like a stack but for objects...
    THANK YOU...
    I'd be willing to run your code, but I can't do it without an SSCCE. It's up to you, but it's my bedtime soon.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    Thnaks alot dude...
    It doesn't matter no more...
    but Thanks alot
    have a good night though

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    Why doesn't it matter? It doesn't look like a complicated problem to fix, if you just throw together the SSCCE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    here this is stackobj
    public class StackObj {
     
    	  private Object[] theStack;
    	  private int maxSize;	
    	  private int top;
     
    	  public StackObj(int s)
    	  {
    	    maxSize = s;
    	    theStack = new Object[maxSize];
    	    top = -1;
    	  }	
    	  public void push(Object elem)
    	  {
    	  	top++;
    	    theStack[top] = elem;
    	  }
    	  public Object pop()
    	  {
    	  	Object result = theStack[top];
    	  	top--;
    	    return result;
    	  }
    	  public Object top()
    	  {
    	    return theStack[top];
    	  }
    	  public boolean isFull()
    	  {
    	    return (top == (maxSize-1) );
    	  }
    	  public boolean isEmpty()
    	  {
    	    return (top == -1);
    	  }
    	  public int size() 
    	  {
    	    return (top+1);
    	  }
    }
    Last edited by helloworld922; October 23rd, 2011 at 12:00 AM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,988
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]

    You're storing the individual digits as chars. Chars are basically a subset of int, and their values map to ints that have little to do with the character they represent. So when you call:

    new Integer (S.charAt(i))

    What you're actually getting is the int that represents the char, not the actual char. It can be a bit confusing, so I recommend you write a little test program that prints out char values and the int values underlying them.

    But to fix your problem, you want to convert the char to a String first.

    Recommended reading: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 09:20 AM
  2. Parsing Scientific Notation from String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: December 14th, 2010, 08:45 AM
  3. 50 SCJP Mock Exam Questions with Answers
    By JavaPF in forum The Cafe
    Replies: 3
    Last Post: July 6th, 2010, 11:10 AM
  4. convert infix to postfix
    By tina G in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 11th, 2010, 12:46 AM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 09:23 AM