The Artima Developer Community
Sponsored Link

Java Answers Forum
Stuck writing code for arrayCopyOfQueue method

2 replies on 1 page. Most recent reply: May 8, 2003 7:18 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Emily

Posts: 1
Nickname: emily
Registered: May, 2003

Stuck writing code for arrayCopyOfQueue method Posted: May 6, 2003 8:30 PM
Reply to this message Reply
Advertisement
There is an exercise in my Java textbook that reads:

Write code for each of the following method:

Method name: arrayCopyOfQueue

Parameters: a Queue parameter called q

Pre: q.size() > 0

Post: result has the same content as q where lowest index to greatest of the result array corresponds to rear-to-front of the stack. (Note that q should remain unaltered by this method.)

This is from the "Queues" chapter exercises, can anyone help?
Thank you!!
Emily


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Stuck writing code for arrayCopyOfQueue method Posted: May 8, 2003 6:29 AM
Reply to this message Reply
without knowing the methods in the Queue class this is as far as I can go.

    /** Method name: arrayCopyOfQueue 
    *   Parameters: a Queue parameter called q 
    *   Pre: q.size() > 0 
    *   Post: result has the same content as q where 
    *   lowest index to greatest of the result array 
    *   corresponds to rear-to-front of the stack. 
    *   (Note that q should remain unaltered by this method.) 
    *   @param input Queue
    *   @return reversed Queue
    */
    public Queue arrayCopyOfQueue(Queue q){
        Queue newQueue = new Queue();
        if (q.size() > 0){
            Object nextObject = null;
 
            // have to know methods of your Queue class for getting and setting elements.
        }
        
        return newQueue;
    }
 

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Stuck writing code for arrayCopyOfQueue method Posted: May 8, 2003 7:18 AM
Reply to this message Reply
    /** Method name: arrayCopyOfQueue 
    *   Parameters: a Queue parameter called q 
    *   Pre: q.size() > 0 
    *   Post: result has the same content as q where 
    *   lowest index to greatest of the result array 
    *   corresponds to rear-to-front of the stack. 
    *   (Note that q should remain unaltered by this method.) 
    *   @param input Queue
    *   @return reversed Queue
    */
    public Queue arrayCopyOfQueue(Queue q){
        Queue newQueue = new Queue();
        if (q.size() > 0){
            ArrayList list = new ArrayList();
            Object nextObject = null;
            while ((nextObject = q.front()) != null){
                list.add(nextObject())
            }
            for (int i = list.size() -1; i >= 0; i=i-1){
                newQueue.enqueue(list.get(i));
            }
        }        
        return newQueue;
    }
 

Flat View: This topic has 2 replies on 1 page
Topic: java awt problem Previous Topic   Next Topic Topic: Stuck & project due soon!  HELP!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use