I have hit a stone wall this week. I have tried numeous methods to get a functioning array into this code, and get the code to call for a total value. Now I have changed code around untik I no longer have compile errors, but now all of my values are null. Please help me out, as I don't know of anything else to do but to start over at an empty line 1. This project is for an inventory program to document CDs (number, title, quantity, price, value), and be able to return the value of the collection. The following is my code as it stands now.

public class mediaInventory
{
   public static void main(String args [])
   {

	        CD mycd[];
	            CD myCd = new CD(1, "Alice in Chains", 4, 14.99);
	            System.out.println( myCd );

	            myCd = new CD( 2, "Black Label Society", 8, 17.99 );
	            System.out.println( myCd );

	            myCd = new CD( 3, "Bonoroo", 2, 21.99 );
	            System.out.println( myCd );

	            myCd = new CD( 4, "Dio", 5, 14.99 );
	            System.out.println( myCd );

	            myCd = new CD( 5, "Eric Clapton", 11, 12.99 );
	            System.out.println( myCd );

	            myCd = new CD( 6, "Led Zeppelin", 9, 16.99 );
	            System.out.println( myCd );

	            myCd = new CD( 7, "Monte Montgomery", 3, 18.99 );
	            System.out.println( myCd );

	            myCd = new CD( 8, "Primus", 5, 14.99 );
	            System.out.println( myCd );

	            myCd = new CD( 9, "SRV", 5, 17.99 );
	            System.out.println( myCd );

	            myCd = new CD( 10, "Tool", 4, 13.00 );
                System.out.println( myCd );



   } //end main

} // end class mediaInventory1

class CD
	   {
   		  private int itemNum;
   	      private String cdTitle;
   	      private int cdUnits;
   	      private double cdPrice;
   	      public CD( int item, String title, int units, double price )

   	      {

   	         itemNum = item;
   	         String CdTitle = title;
   	         int CdUnits = units;
   	         double CdPrice = price;

   	      } //end four argument constructor

   	      public void setItemNum( int item ) // set CD Item

   	      {
   	         itemNum = item;
   	      } //end method set Cd Item

   	      public int getItemNum() //return CD Item
   	      {
   	         return itemNum;
   	      } //end method get Cd Item

   	      public void setCdTitle( String title ) //set CD Title

   	      {
   	         String CdTitle = title;
   	      } //end method set Cd Title

   	      public String getCdTitle() //return Cd Title

   	      {
   	         String CdTitle = cdTitle;
   	         return CdTitle;
   	      } //end method get Cd Title

   	      public void setCdUnits( int units )
   	      {
   	         cdUnits = units;
   	      } //end method set Cd Units

   	      public int getCdUnits() //return Cd Units

   	      {
   	         return cdUnits;
   	      } //end method get Cd Units

   	      public void setCdPrice(double price)
   	      {
   	         cdPrice = price;
   	      } //end method setcdPrice

   	      public double getCdPrice() //return Cd Price

   	      {
   	         return cdPrice;
   	      } //end method get Cd Price

   	      public double value() //calculate inventory value

   	      {
   	         return cdPrice * cdUnits;
   	      } //end method value

   	      public String toString()
   	      {
   	         return String.format( "Item No: %3d Title: %-12s Quantity: %d Price:$%.2f Value: $%.2f",
   	         itemNum, cdTitle, cdUnits, cdPrice, value() );
   	      }

}

Any help & guidance is greatly appreciated.

By any chance do you go to umbc . . ? This just looks somewhat similar to a program they typically have students do (although the one they do is a lot more complicated, but that could be because you just started or omitted code, etc). Could also just be a common programming project.

Anyway,

CD mycd[]; declares a new array, but does not initialize it. Basically, what that means is that you declared an array, but did not set aside any memory for it to use. Had you initialized it properly, it would look like this:

CD mycd[] = new CD[NUMBER_OF_CDS]; where NUMBER_OF_CDS is an integer or a variable (of type int) that you declared. This declares an array of CD's. Lets say you had said CD mycd[] = new CD[3], then the compiler would set aside enough memory for you to hold 3 CD's. Think of it as looking like this:

[CD Number 1][CD Number 2][CD Number 3]. And the way you would put a CD into CD Number 1's space is by saying mycd[0] = *name of a CD Object goes here*.

In the first class that you posted, you created a new CD Object by saying CD myCd = new CD(whatever). But then you kept saying myCd = new CD(whatever), which basically overwrites the CD that you previously created. What you probably should be doing looks like this:

CD[] allMyCDs = new CD[NUMBER_OF_CDS_GOES_HERE];
CD aliceInChains = new CD(whatever);
allMyCDs[0] = aliceInChains;
CD gogoStuff = new CD(whatever);
allMyCDs[1] = gogoStuff;

Hope that helps. I have no idea what skill level you are at, so my apologies if that explanation was unhelpful or anything like that. Feel free to ask any questions you have or to clarify what you need help with.

Comments
Good explanation.

Thank you very much! That makes a great deal of sense to me. I started this introductory Java course a little over four weeks ago. I feel like a bit of a fool, as I have so-far averaged a high A, and this week my grey matter seems as though it has gone on strike. As for your first question, I go to uop (see ad this page ;) )

You have given me a great place to begin fixing this mess, and I thank you from the bottom of my heart. Have a great day

This article has been dead for over six months. Start a new discussion instead.