Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By jbarke12

Thread: Help with contains method in a Jukebox Stack

  1. #1
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Help with contains method in a Jukebox Stack

    Hey, I'm currently doing an HND in Computing and id like some help with my Java Assignment, here's what i have so far:

    JukeBoxStack.java
    Java Code:
    package DSA2;
    
    public class JukeBoxStack 
    {
        private CDNode top = null;
        private CDNode end = null;
        
        public void push(String artist, String albumTitle, int playDuration, int numberOfTracks)
        {    
            CDNode tmpStackNode = new CDNode(artist, albumTitle, playDuration, numberOfTracks);
            
            if(isEmpty())
            {
                top = tmpStackNode;
                end = tmpStackNode;
            }
            else
            {
                end.setPrev(tmpStackNode);
                tmpStackNode.setNext(end);
                end = tmpStackNode;
            }
        }
        
        public boolean isEmpty()
        {
            if(top == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        public String pop()
        {
            String strOutput = "";
            
            if (isEmpty())
            {
                strOutput = "\nThe JukeBox is Empty!";
                return strOutput;
            }
            else
            {
                CDNode tmpStackNode = top;
                top = top.getPrev();
                top.setNext(null);
                strOutput = tmpStackNode.getDetails() + " has been removed.";
                return strOutput;
            }    
        }
        
        public String peek()
        {
            String strOutput = "";
            if(isEmpty())
            {
                return null;
            }
            strOutput = "The top CD is: " + top.getDetails();
            return strOutput;
        }
    
        public String display()
        {
            String strOutput = "";
            strOutput = "\nJukeBox Stack Contents";
            strOutput = strOutput + "\n######################\n";
            if(isEmpty())
            {
                strOutput = strOutput + "No CDs in the JukeBox";
            }
      
            CDNode tmpStackNode = top;
            
            while (tmpStackNode != null)
            {
                strOutput = strOutput + tmpStackNode.getDetails() + "\n";
                tmpStackNode = tmpStackNode.getPrev();
            }
            return strOutput;
        }
        
        public boolean contains(String searchName)
        { 
            if()
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    CDNode.java
    Java Code:
    package DSA2;
    
    public class CDNode 
    {
        private int playDuration, numberOfTracks;
        private String artist, albumTitle;
        private CDNode next, prev;
        
        public CDNode(String artist, String albumTitle, int playDuration, int numberOfTracks)
        {
            this.playDuration = playDuration;
            this.numberOfTracks = numberOfTracks;
            this.artist = artist;
            this.albumTitle = albumTitle;
            next = null;
        }
        
        public String getDetails()
        {
            return artist + " - " + albumTitle;
        }
        
        public void setNext(CDNode Qnode)
        {
            next = Qnode;
        }
        
        public CDNode getNext()
        {
            return next;
        }
        
        public void setPrev(CDNode Qnode)
        {
            prev = Qnode;
        }
        
        public CDNode getPrev()
        {
            return prev;
        }
    
    }
    What i need help with is the contains method, line 87 of JukeBoxStack, how can i go about searching if a particular 'CD' is available in the Jukebox, all i need is a True / False return.

    Thanks in advance for any help, Jason.

  2. #2
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    If it was me I would iterate through all the nodes and test each albumTitle against searchName. The easiest way would be with a for loop or a while loop.

  3. #3
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    Yes i was thinking that, and i have tried to iterate through the stack, though I cant work out how, i can do it with an array, but seeing as this isn't an array I'm not too sure how.
    I'm guessing it has something to do with using the getPrev method, so check the top, call getPrev, check, call, etc.. but how would I code this ? thanks

  4. #4
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    I believe you should try this on your own. I will write down in words how to accomplish this. If you get hung up let me know and I'll help more. First I would test if top equal null and would return false if it did. Next I would create a temp CDNode and set it equal to top. I would then create a while loop while(true). Within the while loop I would test if temp's albumn title equal searchName (i would also get use to using .trim() when comparing strings). I would return true if the two were equal. Otherwise I would test if temp's next was null if it was I would break out of the while loop. Otherwise i would set temp equal to temp.getNext(). Outside of the while loop i would return false.

  5. #5
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    Java Code:
        public boolean contains(String searchName)
        { 
            if(isEmpty())
            {
                return false;
            }
            else
            {
                CDNode tmpStackNode = top;
                while(true)
                {
                    if(tmpStackNode.albumTitle.equals(searchName))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
    That's what I've wrote, and it seems to work, does it look right to you? It's not exactly what you suggested although it seems to be working for me, Thanks for your help!

    EDIT: I've tested it in my menu system and it inst working, it was working in my test class... any ideas?
    Last edited by jason3460; 05-07-2013 at 03:11 PM.

  6. #6
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    Very close:

    In the second else :

    Java Code:
    if(temp.getNext()==null){
    				break;
    			} else{
    				temp= temp.getNext();
    			}
    and then outside of the while loop return false.

    In the code you have it would only work if the album was in the first node.

  7. #7
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    Java Code:
        public boolean contains(String searchName)
        { 
            if(isEmpty())
            {
                return false;
            }
            else
            {
                CDNode tmpStackNode = top;
                while(true)
                {
                    if(tmpStackNode.albumTitle.equals(searchName))
                    {
                        return true;
                    }
                    else
                    {
                        if(tmpStackNode.getNext() == null)
                        {
                            break;
                        } 
                        else
                        {
                            tmpStackNode = tmpStackNode.getNext();
                        }
                    }
                }
                return false;
            }
        }
    This is my code, but when i search for an album title it returns false for everything other than the first node.

  8. #8
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    In line 28 take out return false; and move that to where the break; is. Try that

  9. #9
    jason3460 is offline Member
    Join Date
    Dec 2012
    Posts
    21
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    Still returning a false for everything other than the first node

    EDIT:

    I fixed it, instead of .getNext() i used .getPrev(), as next would be up the stack for example from the first node to -1 node or however you would word it. Prev goes down the stack of nodes.
    Last edited by jason3460; 05-07-2013 at 03:46 PM.

  10. #10
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Help with contains method in a Jukebox Stack

    Correct.
    jason3460 likes this.

Similar Threads

  1. Replies: 4
    Last Post: 05-25-2012, 03:54 PM
  2. Replies: 2
    Last Post: 01-14-2012, 08:56 PM
  3. Replies: 1
    Last Post: 10-06-2011, 02:26 PM
  4. Replies: 1
    Last Post: 03-17-2010, 05:25 AM
  5. Replies: 3
    Last Post: 09-27-2008, 04:46 AM

Posting Permissions

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