10 Replies - 2146 Views - Last Post: 12 May 2010 - 07:43 PM Rate Topic: -----

#1 Trintar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-April 10

Problems with CD Library program

Posted 11 May 2010 - 05:41 PM

Hi,

I have a few questions regarding my current class assignment. I need to create a program to manage a CD library using two classes, CD and MusicLibrary. We are given a test method to conform our code to and I am having a couple of problems, specifically getting the addCD method to work properly. Here is the code:


MusicLibrary Class
import java.util.*;
import java.lang.*;

public class MusicLibrary
{
    Scanner scan = new Scanner(System.in);
    int numCDs = 0;
    CD[] CDs = new CD[numCDs];
    
    public MusicLibrary()
    {
        
    }

    public void displayCDs()
    {
        for(int i = 0; i < numCDs; i++)
        {
            System.out.println("==============================");
            System.out.println(CDs[i].toString());
        }
    }
    
    public boolean isFull()
    {
        if(CDs[numCDs] == null)
            return false;
        return true;
    }
    
    public boolean addCD(CD[] CDs)
    {
        //make sure it isn't full
        if(CDs[numCDs] != null)
            return false;
        else
        {    
            //find the first empty spot
            int i = 0;
            while(CDs[i] != null)
            {
                i++;
            }
            
            return true;
        }
    }
    
    public boolean deleteCD(int n)
    {
        int delIdx = n;
        int last = CDs.length;
        
        for(int i = delIdx; i < last; i++)
            CDs[i+1] = CDs[i];
        
        last = last - 1;
        
        return true;
    }
    
    public static void test()
    {
        MusicLibrary library = new MusicLibrary();
        
        library.addCD( new CD("title3", 2004) );
        library.addCD( new CD("title1", 2005) );
        library.addCD( new CD("title4", 2005) );
        library.addCD( new CD("title2", 2006) );
        library.addCD( new CD("title6", 2006) );
        
        library.displayCDs();
        
        library.deleteCD(2);
        library.displayCDs();
        
        library.deleteCD(1);
        library.displayCDs();
    }// test method
}



CD Class

import java.util.*;
import java.lang.*;

public class CD
{
    String title;
    int year;

    public CD(String title, int year)
    {
        title = "";
        year = 0;
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public int getYear()
    {
        return year;
    }
    
    public String toString()
    {
        return "\"" + title + "\" released in " + year;
    }
    
}



I am getting an error in the test method. This method has to remain as is, and I need to design my program around that snippet of code to make it work.

addCD(CD[]) in MusicLibrary cannot be applied to (CD).

Parts of the code are unfinished and I am still working on parts, but I put the whole thing up so you can see all that I'm currently working with. Thanks in advance for any assistance!

Is This A Good Question/Topic? 0
  • +

Replies To: Problems with CD Library program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,801
  • Joined: 27-December 08

Re: Problems with CD Library program

Posted 11 May 2010 - 05:50 PM

Do you want your addCD() method to accept a single CD or an array of CDs, noted using the square brackes like so CD[]? It looks like from the body you want to accept only a single CD.
Was This Post Helpful? 1
  • +
  • -

#3 Trintar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-April 10

Re: Problems with CD Library program

Posted 11 May 2010 - 06:02 PM

I'm pretty sure it just needs to add a single CD. The test method doesn't add any CDs by an array, so I think a single CD would be fine. However, if I do just addCd (CD CDs), I'm not really sure what way to go about finding the last empty CD. Does something like this get the job done?

    public boolean addCD(CD CDs)
    {
        //find the first empty spot
        int i = 0;
        while(CDs[i] != null)
        {
            i++;
        }
        
        return true;
    }



I'm currently getting "array required, but CD found" and it looks like it's because of my while() loop. Could you give me a push in the right direction on how to approach this method?




EDIT: Cancel that, I think I got it.

    public boolean addCD(CD CDs)
    {
        numCDs++;   
        return true;
    }



Now when I run the program, I get an error on this line:

            System.out.println(CDs[i].toString());



"ArrayOutOfBoundException: 0" shows up in the coding window. The terminal window shows this:

java.lang.ArrayIndexOutOfBoundsException: 0
	at MusicLibrary.displayCDs(MusicLibrary.java:28)
	at MusicLibrary.test(MusicLibrary.java:68)



Any thoughts?

This post has been edited by Trintar: 11 May 2010 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,801
  • Joined: 27-December 08

Re: Problems with CD Library program

Posted 11 May 2010 - 06:07 PM

Maybe you should store an instance variable to hold the index of the first empty space. Then add the CD to that index in your add() method, then increment this variable.

Also, the reason you are getting an error is b/c the name of your param CD in the add() method is the same as the CD array instance variable. If you change the name of the CD param in the add() method, or reference your CD array as this.CDs, you should be good. What is happening is that the param is overshadowing your array instance variable.
Was This Post Helpful? 1
  • +
  • -

#5 Trintar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-April 10

Re: Problems with CD Library program

Posted 11 May 2010 - 06:24 PM

I'm not sure if I understand you correctly. Are you saying to do something like this?

    public void displayCDs()
    {
        for(int i = 0; i < numCDs; i++)
        {
            System.out.println("==============================");
            System.out.println(this.CDs[i].toString());
        }
    }



If so, I'm still getting a program-breaking error. Do I need to apply this. to my addCD() method as well? this. simply means that specific instance in a given method, correct?


EDIT: Upon further looking, I noticed I need to put a value in my array I created earlier other than zero. Now the new program-breaking error says:

java.lang.NullPointerException
at MusicLibrary.displayCDs(MusicLibrary.java:28)
at MusicLibrary.test(MusicLibrary.java:68)

NullPointerException: null

This post has been edited by Trintar: 11 May 2010 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,801
  • Joined: 27-December 08

Re: Problems with CD Library program

Posted 11 May 2010 - 06:26 PM

Using this will reference the instance variable outside the method, not the overshadowing variable inside the method. The add() method is the specific method that has variable overshadowing, which needs to be addressed somehow.
Was This Post Helpful? 1
  • +
  • -

#7 Trintar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-April 10

Re: Problems with CD Library program

Posted 12 May 2010 - 05:51 PM

Thanks very much for your help! I've gotten nearly everything worked out now. Now it looks like I'm on my last problem. When I run the test method I get a different output than desired.

Here are the two class codes:

import java.util.*;
import java.lang.*;

public class MusicLibrary
{
    Scanner scan = new Scanner(System.in);
    int numCDs = 0;
    CD[] CDs = new CD[100];
    
    public MusicLibrary()
    {
        
    }

    public void displayCDs()
    {
        for(int i = 0; i < numCDs; i++)
        {
            System.out.println("==============================");
            System.out.println(this.CDs[i].toString());
        }
    }
    
    public boolean isFull()
    {
        if(CDs[numCDs] == null)
            return false;
        return true;
    }
    
    public boolean addCD(CD CDs)
    {
        this.CDs[numCDs] = CDs;
        numCDs++;
        return true;
    }
    
    public boolean deleteCD(int n)
    {
        int delIdx = n;
        int last = CDs.length;
        
        for(int i = delIdx; i < last - 1; i++)
            CDs[i+1] = CDs[i];
        
        last = last - 1;
        numCDs--;
        
        return true;
    }
    
    public static void test()
    {
        MusicLibrary library = new MusicLibrary();
        
        library.addCD( new CD("title3", 2004) );
        library.addCD( new CD("title1", 2005) );
        library.addCD( new CD("title4", 2005) );
        library.addCD( new CD("title2", 2006) );
        library.addCD( new CD("title6", 2006) );
        
        library.displayCDs();
        
        library.deleteCD(2);
        library.displayCDs();
        
        library.deleteCD(1);
        library.displayCDs();
    }// test method
}





import java.util.*;
import java.lang.*;

public class CD
{
    String title;
    int year;

    public CD(String title, int year)
    {
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public int getYear()
    {
        return year;
    }
    
    public String toString()
    {
        return "\"" + title + "\" released in " + year;
    }
    
}




And here is the output:

==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0
==============================
"null" released in 0




Given the test method, I need the '"null" released in 0' to be an actual name "title1" and year "2005".
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Problems with CD Library program

Posted 12 May 2010 - 07:03 PM

look at the CD constructor:

public CD(String title, int year) 
    { 
    }

it is empty. you don't assign the parameters you get to the variables.
it should be:
public CD(String title, int year) 
    { 
         this.title = title;
         this.year = year;
    } 

Was This Post Helpful? 1
  • +
  • -

#9 Trintar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-April 10

Re: Problems with CD Library program

Posted 12 May 2010 - 07:08 PM

Awesome, thanks! But after putting that, I get this as an output:

==============================
CD@10ea9ba
==============================
CD@1a918d5
==============================
CD@9d6065
==============================
CD@238a47
==============================
CD@14b5f4a
==============================
CD@10ea9ba
==============================
CD@1a918d5
==============================
CD@9d6065
==============================
CD@9d6065
==============================
CD@10ea9ba
==============================
CD@1a918d5
==============================
CD@1a918d5



Any thoughts?
Was This Post Helpful? 0
  • +
  • -

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Problems with CD Library program

Posted 12 May 2010 - 07:34 PM

Did you change something in your toString method?
it seems thatyou are no longer overriding toString..
can you please post the updated code?
Was This Post Helpful? 1
  • +
  • -

#11 Trintar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-April 10

Re: Problems with CD Library program

Posted 12 May 2010 - 07:43 PM

Ah, nevermind, I got it. Thanks very much for your help! Everything works now.

This post has been edited by Trintar: 12 May 2010 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1