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

Thread: help please

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help please

    Hello my name is Delicia and I was wondering if anyone could help me with a problem.
    Write a program that creates a queue of integers. After that it loads in 20 random integers between 1 and 6, and after the 20th number it prints out the content of hte queue. Finally, the program calculates and prints out the average of the values without losing the values from the queue!

    If someone can help me I would appreciate it.

  2. #2
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help please

    You can start off by creating your "random generator"

    Random generator = new Random();
    int RandomNumber = 0;
    RandomNumber = generator.nextInt(6)+1;

    Post your code though, we aren't going to do it all for you

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help please

    I know how to use the randomnumber I don't know how to incorporate queue in the program

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: help please

    Quote Originally Posted by delicia View Post
    I know how to use the randomnumber I don't know how to incorporate queue in the program
    import java.util.LinkedList;
    import java.util.Queue;
     
    /**
     *
     * @author javadb.com
     */
    public class Main {
     
        /**
         * Example method for using a Queue
         */
        public void queueExample() {
     
            Queue queue = new LinkedList();
     
            //Using the add method to add items.
            //Should anything go wrong an exception will be thrown.
            queue.add("item1");
            queue.add("item2");
     
            //Using the offer method to add items.
            //Should anything go wrong it will just return false
            queue.offer("Item3");
            queue.offer("Item4");
     
            //Removing the first item from the queue.
            //If the queue is empty a java.util.NoSuchElementException will be thrown. 
            System.out.println("remove: " + queue.remove());
     
            //Checking what item is first in line without removing it
            //If the queue is empty a java.util.NoSuchElementException will be thrown. 
            System.out.println("element: " + queue.element());
     
            //Removing the first item from the queue.
            //If the queue is empty the method just returns false. 
            System.out.println("poll: " + queue.poll());
     
            //Checking what item is first in line without removing it
            //If the queue is empty a null value will be returned. 
            System.out.println("peek: " + queue.peek());
     
        }
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            new Main().queueExample();
        }
    }