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

Thread: Jukebox Programming project

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

    Default Jukebox Programming project

    import java.io.PrintStream;
     
    public class PlayList
    {
      private Song[] list;
     
      public PlayList()
      {
        this.list = new Song[0];
      }
     
      public Song[] getList()
      {
        Song[] arrayOfSong = new Song[this.list.length];
        for (int i = 0; i < this.list.length; i++)
          arrayOfSong[i] = this.list[i];
        return arrayOfSong;
      }
     
      public void add(Song paramSong)
      {
        if (!inList(paramSong))
        {
          Song[] arrayOfSong = new Song[this.list.length + 1];
     
          for (int i = 0; i < this.list.length; i++)
            arrayOfSong[i] = this.list[i];
          arrayOfSong[i] = paramSong;
          this.list = arrayOfSong;
          System.out.println(paramSong + " was added to the play list.");
        }
        else {
          System.out.println(paramSong + " is already in the play list.");
        }
      }
     
      public void remove(Song paramSong) {
        if (inList(paramSong))
        {
          Song[] arrayOfSong = new Song[this.list.length - 1];
          int i = 0;
          for (int j = 0; j < this.list.length; j++) {
            if (this.list[j].equals(paramSong))
              continue;
            arrayOfSong[i] = this.list[j];
            i++;
          }
          this.list = arrayOfSong;
          System.out.println(paramSong + " was removed from the play list.");
        }
        else {
          System.out.println(paramSong + " is not in the play list.");
        }
      }
     
      private boolean inList(Song paramSong) {
        int i = 0;
        for (int j = 0; (j < this.list.length) && (i == 0); j++)
          if (this.list[j].equals(paramSong))
            i = 1;
        return i;
      }
    }import java.io.PrintStream;
     
    public class PlayList
    {
      private Song[] list;
     
      public PlayList()
      {
        this.list = new Song[0];
      }
     
      public Song[] getList()
      {
        Song[] arrayOfSong = new Song[this.list.length];
        for (int i = 0; i < this.list.length; i++)
          arrayOfSong[i] = this.list[i];
        return arrayOfSong;
      }
     
      public void add(Song paramSong)
      {
        if (!inList(paramSong))
        {
          Song[] arrayOfSong = new Song[this.list.length + 1];
     
          for (int i = 0; i < this.list.length; i++)
            arrayOfSong[i] = this.list[i];
          arrayOfSong[i] = paramSong;
          this.list = arrayOfSong;
          System.out.println(paramSong + " was added to the play list.");
        }
        else {
          System.out.println(paramSong + " is already in the play list.");
        }
      }
     
      public void remove(Song paramSong) {
        if (inList(paramSong))
        {
          Song[] arrayOfSong = new Song[this.list.length - 1];
          int i = 0;
          for (int j = 0; j < this.list.length; j++) {
            if (this.list[j].equals(paramSong))
              continue;
            arrayOfSong[i] = this.list[j];
            i++;
          }
          this.list = arrayOfSong;
          System.out.println(paramSong + " was removed from the play list.");
        }
        else {
          System.out.println(paramSong + " is not in the play list.");
        }
      }
     
      private boolean inList(Song paramSong) {
        int i = 0;
        for (int j = 0; (j < this.list.length) && (i == 0); j++)
          if (this.list[j].equals(paramSong))
            i = 1;
        return i;
      }
    }
    Last edited by joneslay_d; December 2nd, 2010 at 02:32 PM. Reason: code too long


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Jukebox Programming project

    When posting code, it should be in the form of an SSCCE (in other words, as short as possible while still being runnable and showing the problem). Don't forget the code tags to preserve formatting.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Jukebox Programming project

     this.list = new  Song[0];

    It's a bad idea to make an array of Song equal to a single Song object/variable.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Jukebox Programming project

    You seem to have the same class in there twice.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Jukebox Programming project

     if (this.list[j].equals(paramSong))

    You do realize that unless your Song class has a equals method, it will use Object's. Object's will return true if they refer to the same point in memory and false otherwise.

Similar Threads

  1. Project - Please Help
    By toxikbuni in forum Java Theory & Questions
    Replies: 0
    Last Post: April 20th, 2010, 09:58 AM
  2. New to programming
    By Milanocookies56@gmail.com in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2010, 09:55 AM
  3. Can anyone help me on my project?
    By alesana514 in forum Paid Java Projects
    Replies: 1
    Last Post: December 16th, 2009, 09:24 AM
  4. Program to take name as input and write it backwards
    By snake101 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 10th, 2009, 04:35 AM
  5. Programming partner that can earn revenue
    By Jay in forum Project Collaboration
    Replies: 0
    Last Post: May 25th, 2009, 02:33 PM