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 7 of 7

Thread: problems with exceptions

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problems with exceptions

    I have 2 error messages regarding the 2 exceptions which i have highlighted in bold in the code snippet below, can someone please tell me if i need some additional code as I am not to sure why it is not compiling

    package supermarketproject;

    import SuperMarketProject.LinearNode;
    import SuperMarketProject.QueueADT;

    public class LinkedQueue<T> implements QueueADT<T>
    {
    private int count;
    private LinearNode<T> front, rear;

    /**
    * Creates an empty queue.
    */
    public LinkedQueue()
    {
    count = 0;
    front = rear = null;
    }

    public boolean isEmpty( ) {
    return front == null;
    }
    /**
    * Adds the specified element to the rear of this queue.
    *
    * @param element the element to be added to the rear of this queue
    */
    public void enqueue (T element)
    {
    LinearNode<T> node = new LinearNode<T>(element);

    if (isEmpty())
    front = node;
    else
    rear.setNext (node);

    rear = node;
    count++;
    }

    public T dequeue() throws EmptyCollectionException
    {
    if (isEmpty())
    throw new EmptyCollectionException ("queue");

    T result = front.getElement();
    front = front.getNext();
    count--;

    if (isEmpty())
    rear = null;

    return result;
    }

    public class ElementNotFoundException extends RuntimeException{
    /**
    * Sets up this exception with an appropriate message.
    */
    public ElementNotFoundException (String collection)
    {
    super ("The target element is not in this " + collection);
    }
    }
    /**
    * EmptyCollectionException represents the situation in which a collection
    * is empty.
    */

    public class EmptyCollectionException extends RuntimeException{
    /**
    * Sets up this exception with an appropriate message.
    */
    public EmptyCollectionException (String collection)
    {
    super ("The " + collection + " is empty.");
    }
    }

    @Override
    public T first() {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int size() {
    throw new UnsupportedOperationException("Not supported yet.");
    }
    }


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: problems with exceptions

    So what are the errors you get? Please post up the full message text and stack trace if present.

    Incidentally, you shouldn't be extending RuntimeException for normal code. RuntimeExceptions are unchecked, and the compiler won't require methods throwing them to declare them. This is because they're intended for JVM code and if the JVM code fails, the system will crash anyway. Non-JVM code should throw Exception subclasses.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with exceptions

    Is this the same problem you posted on this other thread?
    http://www.javaprogrammingforums.com...html#post37598

    Try compiling it with javac.
    You'll get error messages that say what is wrong with the code.
    Last edited by Norm; July 21st, 2011 at 07:50 PM.

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with exceptions

    ok so basically what i need to do is create a simulation to show my manager because she is keen to reduce the waiting times of those customers standing in line ready to purchase their items. She can simply achieve this by opening more lines, however, she does not want to employ more cashiers than required. She has calculated that customers remain happy if they are served within 8 minutes of joining the queue. she wants to calculate the ‘optimal’ number of cashiers which need to be available in order keep the waiting times below 8 minutes. I have 5 different classes of code which are in effect linking with one and other, i do not know if i need to use exceptions or not, can some of you please shed some light as to whether or not i need to use them.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with exceptions

    do not know if i need to use exceptions or not,
    Can you give an example of when you would use an exception?

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problems with exceptions

    unfortunatley not

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problems with exceptions

    Your post#4 sounds like a Queueing Theory problem. I worked on lots of those at school.

Similar Threads

  1. Declaring your own exceptions
    By weakprogrammer in forum Exceptions
    Replies: 4
    Last Post: June 30th, 2011, 08:19 AM
  2. Methods and Exceptions
    By Mandraix in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2011, 07:15 PM
  3. how to handle many more exceptions
    By shailaja in forum Member Introductions
    Replies: 1
    Last Post: December 27th, 2010, 01:02 AM
  4. How To add my own Exceptions
    By Newtojava in forum Object Oriented Programming
    Replies: 1
    Last Post: September 2nd, 2010, 07:43 AM
  5. Java Exceptions
    By Vinceisg0d in forum Java Theory & Questions
    Replies: 2
    Last Post: March 13th, 2010, 12:25 AM