In the Cat constructor, I thought somethng should be here when the class is called, but in the original solution, it added nothing.
I was creating the objects like this,
Cat cat1 = new Cat();
Cat cat2 = ' ' ' '
'' '' ''
That's why there should have been something in the constructor, like when calling the class, increment a static variable.
In the original solution, it used for loop to create objects.
I was thinking to add objects in the array using '+', then I thought, Java is not that easy, 🤣.
then, At last, System.out.println(cats);
to which the solution used a 'for' loop and some Get method, I knew nothing about it.
When I print 'cats', shouldn't it show every item it consists of?
General Query
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Thomas
22 October 2022, 16:40
What is the exact question now? If it is possible to make use of the constructor to create all ten objects? (yes)
Why the print method does not print meaningful info for you? (it prints the class name + the hexed objects hash code - but you can change this behavior [later in the course])
If it is possible to add some counter or id to the class? (yes)
If it is the best solution to use a for loop? (it's one possible solution - I would have used a while loop and checked the lists size)
What do you mean with array? The ArrayList?
I do not know what I could answer you...
+1
Amber Singh Rathour Pandey
23 October 2022, 11:57
What I meant was, if one is given 10 objects to create, with different names. In that case, I think creating objects using the For loop might not work.
Yes, I meant ArrayList, cause I don't know how to add objects there yet, so I was trying to add them using '+' notation, like when you add Strings together.
I was given a task to print 10 cat objects, as there was nothing in it, that's why I asked about 'shouldn't there be something inside?', I thought it might show nothing.
Like some previous task, there was a task to increment catCount static variable when the Class is called & print the catCount.
Looks like I made you confused. LOL,
Don't worry, Mr. Thomas, I'm working on my conversational skills, through lots of typing, I may not speak well if I am to meet someone face to face though..
0
Thomas
23 October 2022, 15:06
When you add Objects to a list or an array then you do not put that entire object into the container but just the reference to that object. That reference you can that grab again from the container and assign it to a variable. That's similar to
Both reference variables are referencing now the same object.
OK, you learned a little bit about lists and know now how to add objects. Cool...
Cause the object does not have any state and behavior it doesn't mean it can't be printed. By default the class name followed by a hexed hash code is printed. If you need another output you need to code it. Again you learned something new.
You can add elements one by one like
or shorter
But you also can put that adding into a loop
Or you use a while loop
or you make use of the constructor... but I do not know if CG accepts that. And I don't recommend that as this is really unexpected behavior and fellow coders may hate you for that.
+1
Thomas
23 October 2022, 15:08
To that you can add your id if you want... and override the toString method so you get some meaningful output. But as said, I'd go for the while loop.
If you don't understand everything now, don't worry. You'll learn that soon enough and you also learned about the CG method. They teach a little bit and give you tasks. Some are really demanding and you need to read more about stuff in the internet. Even better would be to read a good Java book at the same time. +1