Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: CD collection Constructor and Array question

  1. #1
    snobbysteven is offline Member
    Join Date
    Oct 2012
    Posts
    17
    Rep Power
    0

    Angry CD collection Constructor and Array question

    my question is in the class CDCollection, what exactly do I need to put in the CDCollection1832() and addCD(). i put the get methods in there as a temp, but im a little stumped. Also how can I get the CD's made from c1 and c2 into an Array to store? because I still have a test class after the CD collection class which is mostly why im a little stumped, along with ive never used Arrays in this type of problem before.

    Thanks!

    Java Code:
    public class CD1832 {
    	
    	private String title;
    	private String artist;
    	private double cost;
    	
    	public CD1832()
    	{
    		title = "no title";
    		artist = "no artist";
    		cost = 0;
    	}
    	
    	public CD1832(String title, String artist, double cost)
    	{
    		this.title = title;
    		this.artist = artist;
    		this.cost = cost;
    	}
    	
    	public void setTitle(String title)
    	{
    		this.title = title;
    	}
    
    	public void setArtist(String artist)
    	{
    		this.artist = artist;
    	}
    	
    	public void setCost(double cost)
    	{
    		this.cost = cost;
    	}
    	
    	public String getTitle()
    	{
    		return title;
    	}
    	
    	public String getArtist()
    	{
    		return artist;
    	}
    	
    	public double getCost()
    	{
    		return cost;
    	}
    	
    	public String toString()
    	{
    		return ("Name of CD: " + title + ",  Artist: " + artist + ",  Cost: "+ cost);
    	}
    }


    Java Code:
    public class CDCollection1832 {
    	
    	private CD1832[] collection = new CD1832[50];
    	private int count;
    	private double totalCost;
    	
    	CD1832 c = new CD1832("james","tyler",20);
    	CD1832 c2 = new CD1832("Falloutboy","Takeover",13.99);
    	
    	public CDCollection1832()
    	{
    		c.getArtist();
    		 c.getTitle();
    		 c.getCost();	
    	}
    	
    	public void addCD(String title, String artist, double cost)
    	{
    	 c.getArtist();
    	 c.getTitle();
    	 c.getCost();
    	}
    
    	public String toString()
    	{
    	return "Number of CDs: " + count +
    			"Total Cost: " + totalCost;	
    	}
    }
    Java Code:
    public class Ass9ID1832 {
    
    	
    	public static void main(String[] args) {
    		
    		CDCollection1832 c1 = new CDCollection1832();
    		
    		
    		
    		
    		
    		
    
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: CD collection Constructor and Array question

    You're sort of defeating the purpose of passing the CD fields as arguments to the collection. Besides, your Collection class is not set up to use them. I would just pass instances of the CD1832.

    Like this:

    Java Code:
    public void addCD(CD1832 cd)    {
           CD1832[count++] = cd;
    }
    
    // Then to add them do this
    
    public static void main(String[] args) {         
            CDCollection1832 c1 = new CDCollection1832();
           c1.add(new CD1832("james","tyler",20)); 
           c1.add(new CD1832("Falloutboy","Takeover",13.99));
    }
    For clarity (and just good practice) you should explicitly initialize count to 0 in your Collections constructor.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: CD collection Constructor and Array question

    Quote Originally Posted by jim829 View Post
    For clarity (and just good practice) you should explicitly initialize count to 0 in your Collections constructor.
    I'm curious as to why you recommend this. Fields have default values: 0 for numeric primitives, false for boolean and null for reference types. Why should it be good practice to explicitly initialize any field to its default value?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: CD collection Constructor and Array question

    For me, it is simply a reminder that yes, that is indeed what I want it to be. I probably over stretched when I said good practice because that is a generalization that I am not qualified to make. So I just find it clearer and don't have to think about it. Usually, if I allocate an array and depend on the values to be a certain default like an array of nulls for a hashmap, I also put in a comment mainly to remind myself. Earlier, in a different post, someone used a default value to initialize their array. I wonder if s/he would have made that mistake if they had explicitly set it to 0.

    At any rate I need to choose my words more carefully.

    Regards,
    Jim
    Last edited by jim829; 03-27-2013 at 04:46 AM.
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Question about Interface Collection<E>
    By yma16 in forum New To Java
    Replies: 2
    Last Post: 04-11-2011, 04:00 AM
  2. constructor question
    By mac in forum New To Java
    Replies: 4
    Last Post: 03-20-2010, 09:22 PM
  3. Garbage Collection Question
    By DrMath in forum New To Java
    Replies: 5
    Last Post: 10-03-2009, 11:08 AM
  4. Replies: 2
    Last Post: 12-19-2008, 03:20 AM
  5. Converting a Collection to an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:44 PM

Posting Permissions

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