2

edit - I revised my code and replaced my original work with new code, still having similar issues

This data structures class I'm taking is my first programming course, so I'm a bit out of my element. The first project is really kicking my ass. It is to make a Reverse Polish Notation Calculator. It is more or less complete, just a lot of bugs. I have been spending hours tweaking my code but when I address one problem it unleashes another. I apologize in advance for my horrible programming skills.

public class ReversePolishStack {

class SinglyLinkList {
    Node head = null;

    public void push(float newData) {
        Node cache = this.head;
        this.head = new Node(newData, cache);
    }

    public float pop() {
        float out = this.head.data;
        head = head.next;
        return out;
    }
    public void add(float num1, float num2) {
            num1 = pop();                       
            num2 = pop();
            push(num1+num2);
        }
    public void sub(float num1, float num2) {
            num1 = pop();                       
            num2 = pop();
            push(num2-num1);
        }
    public void div(float num1, float num2) {
            num1 = pop();                       
            num2 = pop();
            push(num2/num1);
        }
    public void mult(float num1, float num2) {
            num1 = pop();                       
            num2 = pop();
            push(num1*num2);
        }
    class Node {
        public float data;
        public Node next;

        public Node(float data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}     
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    ReversePolishStack rps = new ReversePolishStack();
    SinglyLinkList sll = rps.new SinglyLinkList(); 
    String entry;
    do
    {
        System.out.print("Enter Expression:\n");       
        Scanner in = new Scanner(System.in);
        entry =in.nextLine();           
        StringTokenizer st = new StringTokenizer(entry," ");                        
        String s1;
        int count = 0;
        while (st.hasMoreElements()) 
        {               
            if (entry.length()<4)   {// for an error message not enough input
                System.out.print("Not enough input"); break;
            }
            else if (count>1 && sll.head.next==null) {
                System.out.print("Not enough operands"); break;
            }                             
            s1 = st.nextToken();          
            if((s1.equals("+") || s1.equals("-") || s1.equals("*") || s1.equals("/")))  {                             
                if(s1.equals("+"))
                    sll.add(sll.head.data, sll.head.next.data);
                else if(s1.equals("-"))
                    sll.sub(sll.head.data, sll.head.next.data);
                else if(s1.equals("/")) {
                    if (sll.head.data==0)   {
                        System.out.println("Division by Zero enounterd."); break;
                    }
                    sll.div(sll.head.data, sll.head.next.data);
                }
                else if(s1.equals("*"))
                    sll.mult(sll.head.data, sll.head.next.data);
                else
                    System.out.print("Unrecognized input");break;
            }
            else {
                sll.push(Float.parseFloat(s1));
            }
            count++;
        }
        System.out.println(sll.head.data);
        sll.pop();
    } while(entry.equals("0")== false); // typeing a single zero terminates
    System.out.print("Thanks for using my RPN Calculator!");
}

}

I have been at this for awhile and I'm sure with every attempt at fixing a bug I also added to the convolution that is my code. Any help would be appreciated!

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Lunch
  • 57
  • 1
  • 1
  • 7
  • 1
    lets go one by one.. whats ur current problem :-) – Crickcoder Oct 11 '13 at 06:22
  • That won't even compile! classes declared inside main method?? Is that a c&p error? – Fildor Oct 11 '13 at 07:48
  • I'm surprised there weren't prerequisutes for this course – Paul Samsotha Oct 11 '13 at 11:12
  • [G V] Mainly I wanted people to point out the big issues like if I'm going about something wrong, and like the next commented mentioned class inside main method. [Fildor] Thanks for the help. [peeskiller] There are pre-requisites... SHHHHHHHHHHHH, but I've been doing fine until now. I have a 86% in the class about 1/3 way through. One of my ex-military professors once said, "If you ain't cheating, you ain't trying." If furthering my education is wrong, than I don't want to be right. I do admit it is unfair to the people trying to get into the class that has done the prereqs. – Lunch Oct 11 '13 at 19:46

1 Answers1

0

First of all you should take out from the main method the classes you have defined. After that you'll get an error because an instance of the class singlyLinkList (singlyLinkList sll = new singlyLinkList();) has been created without creating the outer class (ReversePolishStack).

See as reference this link about nested classes.

I also put the Node class into the singlyLinkList class (btw you should rename this class as SinglyLinkList with the first letter capitalized).

I didn't get into the logic of your code but at least after these fixes your code will compile:

public class ReversePolishStack {

    class singlyLinkList {
        Node head = null;

        public void push(float newData) {
            Node cache = this.head;
            this.head = new Node(newData, cache);
        }

        public float pop() {
            float out = this.head.data;
            head = head.next;
            return out;
        }

        class Node {
            public float data;
            public Node next;

            public Node(float data, Node next) {
                this.data = data;
                this.next = next;
            }
        }
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {
        float number1;
        float number2;
        ReversePolishStack rps = new ReversePolishStack();
        singlyLinkList sll = rps.new singlyLinkList();
        System.out.print("Enter Expression:\n");
                // from here will be the same as you wrote
        }
}

I also suggest you to extract the logic of your program from your main method and create methods in the ReversePolishStack in order to use the oop concepts.

Hope this was useful! Ciao!

Paolo
  • 1,641
  • 11
  • 15