Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-29-2012, 04:41 PM   PM User | #1
greenarcher
New to the CF scene

 
Join Date: Sep 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
greenarcher is an unknown quantity at this point
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 07:42 PM.. Reason: added code BB tags
greenarcher is offline   Reply With Quote
Old 09-29-2012, 06:01 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 09-29-2012, 06:02 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 09-29-2012, 07:43 PM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,703
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 09-29-2012, 08:06 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:47 AM.


Advertisement
Log in to turn off these ads.