Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to discard and swap items in java?

    What is the correct code for me to discard the item one, then put item two in item one's place, item three in item two's place, item four in item three's place, and for item five, it will be the new equipped item.

    it should also return the discarded item at the end. Thanks


    PHP Code:
    public void equip (Item e)
        {
            if (
    itemOne == null)
            {
                
    itemOne e;
                
    itemCounter+= 1;
            }
            
            else if (
    itemTwo == null)
            {
                
    itemTwo e;
                 
    itemCounter+= 1;
            }
                
            else if (
    itemThree == null)
            {
                
    itemThree e;
                
    itemCounter+= 1;
            }
            else if (
    itemFour == null)
            {
                
    itemFoure;
                
    itemCounter+= 1;
            }
                
            else if (
    itemFive == null)
            {
                
    itemFive e;
                
    itemCounter+= 1;
            }
                
        }
        
        
    /**
         * The swap() method enables the Avatar to discard the Item in the first position of his/her belt,
         * advance the remaining Items in his/her belt one position forward, then place a new Item 
         * in the last position of his/her belt. 
         * 
         * @param e - : the Item to be added to the Avatar's belt
         * @param return the name of the Item discarded from the Avatar's belt
         */
        
    public String swap (Item e)
        {
            ?????

        } 
    Last edited by VIPStephan; 09-29-2012 at 08:42 PM. Reason: added code BB tags

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You should post in the correct forum firstly: this is the JavaScript forum, not java.

    You also need to wrap your code in [CODE ] [/ CODE] tags (without the spaces). Highlight your code and press the hash # button when creating your post.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,932
    Thanks
    6
    Thanked 1,041 Times in 1,014 Posts
    What the others said, plus: if you post any code please put it in between [CODE][/CODE] tags. It makes scanning your posts much easier. You can do this by clicking the small ‘#’ icon above the reply field.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This behaviour is best done using a collection such as a LinkedList. The LinkedList includes the Deque and Queue interfaces, so you can easily treat it as a queue or a stack.
    A quick example:
    PHP Code:
    import java.util.LinkedList;


    public class 
    QueueExample<T> extends LinkedList<T>
    {    
        public 
    T equip(T obj)
        {
            
    T result this.pollFirst();
            
    this.offerLast(obj);
            return 
    result;
        }
            
        public static 
    void main(String[] argv)
        {
            
    QueueExample<Itemqe = new QueueExample<Item>();
            
    qe.add(new Item());
            
    qe.add(new Item());
            
    qe.add(new Item());
            
    qe.add(new Item());
            
    qe.add(new Item());
            
            
    System.out.println(qe);
            
    System.out.println("Removed: " qe.equip(new Item()).toString());
            
    System.out.println(qe);
            
    System.out.println("Removed: " qe.equip(new Item()).toString());
            
    System.out.println(qe);
        }
        
    }

    class 
    Item
    {
        private static 
    int iCnt 0;
        private 
    int i;
        
        public 
    Item()
        {
            
    this.= ++Item.iCnt;
        }
        
        public 
    String toString()
        {
            return 
    "Item[" this."]";
        }

    I just keep making new ones, so my numbers keep counting up. But if you want you can cycle the list by offering the polledfirst object after you push the new item on the queue.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •