Static cats

  • 10
  • Locked
A static cat is every cat's cat. So, in this task let's add a static variable cats to the Cat class. Each time a new cat (new Cat object) is created, let it be added to the variable cats. We need to create 10 Cat objects. Then the printCats method should display all the cats on the screen.
You can't complete this task, because you're not signed in.
Comments (28)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Barkha Mishra Java Developer at Evosys
28 August 2022, 12:33
The requirements are bit ambiguous. There's nothing to be written in constructor. You need to add the new created Cat object to cats arrayList in the main method only. If still you are not able to get the correct solution, then go to Help (next to Verify) and click on Download correct solution
Level 16 , Texas, United States
4 May 2021, 17:15
google is your friend.
Karas Java Developer
10 November 2021, 23:33
I don't need google my wife knows everything. LOL.
Level 11 , Banja Luka, Bosnia and Herzegovina
25 February 2021, 17:32
Very useful assignment. Arraylist kick a**, although it should be presented before OOP?!
Karas Java Developer
2 September 2020, 20:42
The use of this kind of array is very smart. Read about it and you will understand, it changes size. public static ArrayList<String> cats = new ArrayList<String>(); I am also of the opinion that some theory should have come before this but who am I to argue: "no pain, no gain"
Level 15 , United Kingdom
31 August 2020, 23:25
not sure if i was missing something but I was adding the cat references within the constructor (cats.add(this)) and the program kept failing condition 4 about adding cats. The output of the program seems the same if I add them within the main method (right after creation) but it didn't fail verification there. Any ideas why it didn't like adding them via the constructor as we were taught?
Karas Java Developer
2 September 2020, 20:47
I had the same problem but I noticed in my own code, the instructions the Capitan says are completelly overstated from the requirements on the side, The captain just says add to the array but the requirement clearly explains: The main method must add all the created cats to the variable cats. So the addition is not actually in the constructor but the main(); For the forth requirement use a for loop to iterate through the array and prinln() EACH in a separate line. Best of luck.
Level 10 , Trieste, Italy
9 November 2021, 09:49
for the .4 i used .size and .get(i)..
Level 22 , Cairns, Australia
4 August 2020, 17:59
I was really just blind guessing this one. A little context to ArrayList would be nice. [ArrayListObject].get([indicator]) gets you the Object at the [indicator] place.
Michaela Obertova
Level 29 , Prievidza
10 October 2022, 12:03
As you are on level 22 now, you definitely already know this, but for other people reading this, there is an easier / more concise way to loop over an ArrayList: ArrayList<String> words = new ArrayList<>(); words.add("Hello"); words.add("Goodbye"); words.add("Hi"); for (String s: words) { System.out.println(s); }
andy 6473
Level 9 , Bangalore, India
19 June 2020, 14:25
Can someone please explain this code. I am really confused.
Leon J Boczkowski
Level 10 , Pittsburgh, United States
14 May 2020, 12:38
It was good to see what happens when you initialize and object with no variables and add it to an ArrayList. At first I was confused because it kept coming up with the reference address. A good example, but the wrong time to point this out. Maybe a little later on so no confusion arises.
29 March 2020, 00:20
The instructions of this task are confusing. For example, the second instruction gives the impression that the constructor has to add the cat objects to the ArrayList cats. Moreover, is not clear how to use ArrayList<Cat> class or how to initialize a variable with this type.
3 March 2020, 22:35
Is seems in this task that we have to create objects within a loop. When an object is created in a loop, we cannot assign it a reference variable of our choosing, right? In real world programming when do we create objects in this manner?