Results 1 to 10 of 10
  1. #1
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default I need some homework help

    I am taking a Algorithms class and it is using Java as a language. Since I haven't had Java in a long time I am struggling with getting this done. I am using Net beans and the Book Java foundations. I am trying to get a program that is in the book to work but don't understand the errors I am getting and need to get it up and running so I can modify it to complete my homework. I have 7 files and two of them have errors surrounding my EmptyCollectionException.java file

    Java Code:
    package javafoundations;
    
    public class EmptyCollectionException extends Exception
    {
        EmptyCollectionException(String message)
        {
            super(message);
        }
    }
    Java Code:
    //********************************************************************
    //  LinkedStack.java       Java Foundations
    //
    //  Represents a linked implementation of a stack.
    //********************************************************************
    
    package javafoundations;
    
    //import javafoundations.exceptions.*;
    
    public class LinkedStack<T> implements Stack<T>
    {
       private int count;
       private LinearNode<T> top;
    
       //-----------------------------------------------------------------
       //  Creates an empty stack using the default capacity.
       //-----------------------------------------------------------------
       public LinkedStack()
       {
          count = 0;
          top = null;
       }
      //-----------------------------------------------------------------
       //  Removes the element at the top of this stack and returns a
       //  reference to it. Throws an EmptyCollectionException if the
       //  stack contains no elements.
       //-----------------------------------------------------------------
       public T pop() throws EmptyCollectionException(string)
       {
          if (count == 0)
             throw new EmptyCollectionException ("Pop operation failed. "
                + "The stack is empty.");
    
          T result = top.getElement();
          top = top.getNext();
          count--;
    
          return result;
       } 
      //-----------------------------------------------------------------
       //  Returns a string representation of this stack.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = "<top of stack>\n";
          LinearNode current = top;
    
          while (current != null)
          {
             result += current.getElement() + "\n";
             current = current.getNext();
          }
    
          return result + "<bottom of stack>";
       }
    
       //-----------------------------------------------------------------
       //  The following methods are left as Programming Projects.
       //-----------------------------------------------------------------
       // public void push () { }
       // public T peek () throws EmptyCollectionException { }
       // public boolean isEmpty() { }
       // public int size() { }
    }
    Java Code:
    package javafoundations;
    //********************************************************************
    //  PostfixEvaluator.java       Java Foundations
    //
    //  Represents an integer evaluator of postfix expressions. Assumes
    //  the operands are constants.
    //********************************************************************
    
    import javafoundations.*;
    import java.util.Scanner;
    
    public class PostfixEvaluator
    {
       private final char ADD = '+', SUBTRACT = '-';
       private final char MULTIPLY = '*', DIVIDE = '/';
       private LinkedStack<Integer> stack;
    
       //------------------------------------------------------------------
       //  Sets up this evalutor by creating a new stack.
       //------------------------------------------------------------------
       public PostfixEvaluator()
       {
          stack = new LinkedStack<Integer>();
       }
       //------------------------------------------------------------------
       //  Evaluates the specified postfix expression. If an operand is
       //  encountered, it is pushed onto the stack. If an operator is
       //  encountered, two operands are popped, the operation is
       //  evaluated, and the result is pushed onto the stack.
       //------------------------------------------------------------------
       public int evaluate (String expr)
       {
          int op1, op2, result = 0;
          String token;
          Scanner tokenizer = new Scanner (expr);
    
          while (tokenizer.hasNext())
          {
             token = tokenizer.next();
    
             if (isOperator(token))
             {
                op2 = (stack.pop()).intValue();
                op1 = (stack.pop()).intValue();
                result = evalSingleOp (token.charAt(0), op1, op2);
                stack.push (result);
             }
             else
                stack.push (Integer.parseInt(token));
          }
    
          return result;
       }
       //------------------------------------------------------------------
       //  Determines if the specified token is an operator.
       //------------------------------------------------------------------
       private boolean isOperator (String token)
       {
          return (token.equals("+") || token.equals("-") ||
             token.equals("*") || token.equals("/"));
       }
    
       //------------------------------------------------------------------
       // Peforms integer evaluation on a single expression consisting of
       // the specified operator and operands.
       //------------------------------------------------------------------
       private int evalSingleOp (char operation, int op1, int op2)
       {
          int result = 0;
    
          switch (operation)
          {
             case ADD:
                result = op1 + op2;
                break;
             case SUBTRACT:
                result = op1 - op2;
                break;
             case MULTIPLY:
                result = op1 * op2;
                break;
             case DIVIDE:
                result = op1 / op2;
          }
    
          return result;
       }
    }
    Java Code:
    package javafoundations;
    
    //********************************************************************
    //  Postfix.java       Java Foundations
    //
    //  Demonstrates the use of a stack to evaluate postfix expressions.
    //********************************************************************
    import java.util.Scanner;
    
    public class Postfix
    {
       //------------------------------------------------------------------
       //  Reads and evaluates multiple postfix expressions.
       //------------------------------------------------------------------
       public static void main (String[] args)
       {
          String expression, again;
          int result;
    
          try
          {
             Scanner in = new Scanner(System.in);
             do
             {
                PostfixEvaluator evaluator = new PostfixEvaluator();
                System.out.println ("Enter a valid postfix expression: ");
                expression = in.nextLine();
    
                result = evaluator.evaluate (expression);
                System.out.println();
                System.out.println ("That expression equals " + result);
    
                System.out.print ("Evaluate another expression [Y/N]? ");
                again = in.nextLine();
                System.out.println();
             }
             while (again.equalsIgnoreCase("y"));
          }
          catch (Exception IOException)
          {
             System.out.println("Input exception reported");
          }
       }
    }
    Java Code:
    //************************************************************
    //  LinearNode.java       Java Foundations
    //
    //  Represents a node in a linked list.
    //************************************************************
    
    package javafoundations;
    
    public class LinearNode<T>
    {
       private LinearNode<T> next;
       private T element;
    
       //-----------------------------------------------------------------
       //  Creates an empty node.
       //-----------------------------------------------------------------
       public LinearNode()
       {
          next = null;
          element = null;
       }
       //-----------------------------------------------------------------
       //  Creates a node storing the specified element.
       //-----------------------------------------------------------------
       public LinearNode (T elem)
       {
          next = null;
          element = elem;
       }
    
       //-----------------------------------------------------------------
       //  Returns the node that follows this one.
       //-----------------------------------------------------------------
       public LinearNode<T> getNext()
       {
          return next;
       }
    
       //-----------------------------------------------------------------
       //  Sets the node that follows this one.
       //-----------------------------------------------------------------
       public void setNext (LinearNode<T> node)
       {
          next = node;
       }
       //-----------------------------------------------------------------
       //  Returns the element stored in this node.
       //-----------------------------------------------------------------
       public T getElement()
       {
          return element;
       }
    
       //-----------------------------------------------------------------
       //  Sets the element stored in this node.
       //-----------------------------------------------------------------
       public void setElement (T elem)
       {
          element = elem;
       }
    }
    Java Code:
    //********************************************************************
    //  Stack.java       Java Foundations
    //
    //  Defines the interface to a stack collection.
    //********************************************************************
    
    package javafoundations;
    
    public interface Stack<T>
    {
       //  Adds the specified element to the top of the stack.
       public void push (T element);
    
       //  Removes and returns the top element from the stack.
       public T pop();
    
       //  Returns a reference to the top element of this stack
       //  without removing it.
       public T peek();
    
       //  Returns true if the stack contains no elements and false
       //  otherwise.
       public boolean isEmpty();
    
       //  Returns the number of elements in the stack.
       public int size();
    
       //  Returns a string representation of the stack.
       public String toString();
    }
    Any help would be appreciated.

    Thanks,
    B

  2. #2
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: I need some homework help

    The linkedstack file and postfixevaluator is where the problem shows up

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

    Default Re: I need some homework help

    don't understand the errors I am getting
    Please copy and paste here the full text of the error messages.

  4. #4
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: I need some homework help

    I made a modification to the stack.java file

    Java Code:
    //********************************************************************
    //  Stack.java       Java Foundations
    //
    //  Defines the interface to a stack collection.
    //********************************************************************
    
    package javafoundations;
    
    public interface Stack<T>
    {
       //  Adds the specified element to the top of the stack.
       public void push (T element);
    
       //  Removes and returns the top element from the stack.
       public T pop()throws EmptyCollectionException(String);
    
       //  Returns a reference to the top element of this stack
       //  without removing it.
       public T peek();
    
       //  Returns true if the stack contains no elements and false
       //  otherwise.
       public boolean isEmpty();
    
       //  Returns the number of elements in the stack.
       public int size();
    
       //  Returns a string representation of the stack.
       public String toString();
    }
    In the file LinkedStack.java on the public class line.

    Java Code:
    public class LinkedStack<T> implements Stack<T>
    I get the error
    javafoundations.LinkedStack is not abstract and does not override abstract method size() in javafoundations.Stack

    when compiled in netbeans I get:

    run:
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at javafoundations.PostfixEvaluator.<init>(PostfixEva luator.java:23)
    at javafoundations.Postfix.main(Postfix.java:25)
    Caused by: java.lang.RuntimeException: Uncompilable source code - javafoundations.LinkedStack is not abstract and does not override abstract method size() in javafoundations.Stack
    at javafoundations.LinkedStack.<clinit>(LinkedStack.j ava:11)
    ... 2 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 5 seconds)

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

    Default Re: I need some homework help

    LinkedStack is not abstract and does not override abstract method size() in javafoundations.Stack
    When you implement an interface you must override all the methods in the interface. The compiler can NOT find the size() method in the LinkedStack class definition. Look at line 65 where a size() definition is commented out.

  6. #6
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: I need some homework help

    I guess maybe I am not to assume this will work right out of the book. I will look at that. I was just thinking it should work before I start changing it.

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

    Default Re: I need some homework help

    Look at lines 59-60

  8. #8
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: I need some homework help

    yeah I knew that was there I just thought it would work without it. I stand corrected.

    Thanks for your insight. I will get this done yet

  9. #9
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: I need some homework help

    I changed the LinkedStack.java to and now it runs but I still have some problems:

    Java Code:
    
    //********************************************************************
    //  LinkedStack.java       Java Foundations
    //
    //  Represents a linked implementation of a stack.
    //********************************************************************
    
    package javafoundations;
    
    import javafoundations.exceptions.*;
    
    public class LinkedStack<T> implements Stack<T>
    {
       private int count;
       private LinearNode<T> top;
    
       //-----------------------------------------------------------------
       //  Creates an empty stack using the default capacity.
       //-----------------------------------------------------------------
       public LinkedStack()
       {
          count = 0;
          top = null;
       }
      //-----------------------------------------------------------------
       //  Removes the element at the top of this stack and returns a
       //  reference to it. Throws an EmptyCollectionException if the
       //  stack contains no elements.
       //-----------------------------------------------------------------
       public T pop() throws EmptyCollectionException
       {
          if (count == 0)
             throw new EmptyCollectionException ("Pop operation failed. "
                + "The stack is empty.");
    
          T result = top.getElement();
          top = top.getNext();
          count--;
    
          return result;
       } 
      //-----------------------------------------------------------------
       //  Returns a string representation of this stack.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = "<top of stack>\n";
          LinearNode current = top;
    
          while (current != null)
          {
             result += current.getElement() + "\n";
             current = current.getNext();
          }
    
          return result + "<bottom of stack>";
       }
    
       //-----------------------------------------------------------------
       //  The following methods are left as Programming Projects.
       //-----------------------------------------------------------------
        public void push (T element) {
        LinearNode<T> node = new LinearNode<T>(element);
    		node = top;
    		top = node; 
    		count++;
        }
        public T peek () throws EmptyCollectionException { 
        return top.getElement();
        }
        public boolean isEmpty() {
        return count ==0;
                }
        public int size() {
        return count;
       }
    }
    The line: public T peek () throws EmptyCollectionException {
    has an error:
    peek() in javafoundations.LinkedStack cannot implement peek() in javafoundations.Stack
    overridden method does not throw javafoundations.exceptions.EmptyCollectionExceptio n

  10. #10
    brlau is offline Member
    Join Date
    Dec 2011
    Posts
    9
    Rep Power
    0

    Default Re: I need some homework help

    I found that I needed to make the EmptyCollectionException public. That seems to fix all my errors but it still doesn't work.
    when running it I get:
    run:
    Enter a valid postfix expression:
    12 4 -3 7 *19 - /
    Input exception reported
    BUILD SUCCESSFUL (total time: 21 seconds)

    I dont know why it does in input exception reported

Similar Threads

  1. Need help with homework
    By bkim33 in forum New To Java
    Replies: 9
    Last Post: 02-11-2011, 04:50 AM
  2. Need some help with my homework.
    By JacobM in forum New To Java
    Replies: 11
    Last Post: 02-04-2011, 02:42 AM
  3. Homework Help
    By Kyoto in forum New To Java
    Replies: 2
    Last Post: 07-30-2010, 12:30 AM
  4. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 03:37 PM
  5. Homework help...
    By robrobrob in forum New To Java
    Replies: 4
    Last Post: 10-17-2008, 04:24 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
  •