Hi all,

I would like to know what to add to the following code to count the number of objects that have been created.

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

                CD cd1 = new CD("Kaiser "," up the khazi ",(double) 9.99);
		CD cd2 = new CD("Oasis "," morning glory ",(double) 3.99);
		CD cd3 = new CD("Bob Dylan "," Alreet Sunna ",(double) 6.99);

		System.out.println("Artist \t\tTitle\t\tCost");
		System.out.println("====================================");
		System.out.println(cd1.toString());
		System.out.println(cd2.toString());
		System.out.println(cd3.toString());
            
	}
}

Recommended Answers

All 9 Replies

add objects to the list

List<CD> cdList = new ArrayList<CD>();

and work with list.
you are going to the OOP!

hi,

thanks for the post.

im very very new at java. Does that count them?

i added System.out.println( List ); but it only outputs []

Please be patient :-)

How do i get it to display the number of objects?

Hi quuba: I went thru all this with him less than an hour ago - see topic "counting objects and totalling their values". He marked it solved, then asked exactly the same question. Good luck!

Hi James,

I thought it would be more sensibe to repost a break down of the question. I asked too much for me to digest you answers.

Im not daft, just very wet behind the ears.

Hi there. I'm sorry if I dumped too much on you too quickly.
I suggest you don't break down your requirements in steps like this - you'll get answers for the first requirement that may lead you down a blind alley as far as the following requirents are concerned. Get the design/strategy right for the totality of the requirements, then implement on step at a time.
Here's some background to the what and why of the code snippets I posted earlier.
You hava a CD class. Instances of this class have attributes like value, artist etc. You have a constructor that takes those values as parameters and sets up the instance. It's good practice (take this on trust for now) to keep those variables private, and have public "get" methods that other classes can use to access the CD's attributes. So far so good.
Now you want to write code that needs to access all the CDs you have created, whether to count them, add up their values, [search for CDs by a particular artist (eg)] etc. To make that possible you need to get a list of all the CDs so you can loop though them all.
You can use a single static variable in the CD class to hold that list - I'd use an ArrayList. In the constructor for CD instances you can add that instance to the list, so it's always up to date. You can make the list public, but many people would suggest you keep that private as well, and have a public "get" method for it.
So now everything you need is publically available - you can get a list of all CDs, you can count how many things are in thatlist, or write a simple loop to get the value of each CD in the list and add them up (etc).
The code to get the list and loop through it can be anywhere you like. You can put it in another class (maybe ine that does a user interface), put it in your main(...) method, whatever.

Does this help?

Fantastic,

Ive worked on a few examples on Get Set a few days ago and I do get the need to keep things private. Printing out to a screen im ok with, creating new objects im also ok with. Ive got till friday to get my head around this "existing object" interaction.

As a visual learner, sometimes i dont get stuff unless i see it. Also with a history of PHP im trying to approach Java as an empty vessle.

Once i understand this, and i have an annoying brain that wont let me move on until its in, im going to focus on interacting with a database.

Having a static Collection of CD objects is fine and will work, but I see nothing in your requirements that would need more than I posted in my post in your last thread (a static integer and a static double, adjusted during each call to the constructor). Did you run the code that I posted? Will that not work for your needs?

Hi Vernon. whyteyoh and I shared a few more details offline, and the requirements extend to things like printing the most expensive item and printing sorted lists of CDs. So while your code does meet the initial requirements, I was steering him down a route that would extend nicely to the later requirements. James.

Hi Vernon. whyteyoh and I shared a few more details offline, and the requirements extend to things like printing the most expensive item and printing sorted lists of CDs. So while your code does meet the initial requirements, I was steering him down a route that would extend nicely to the later requirements. James.

OK. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.