Please tell me the solution of this program.

/* Count the number of cats
Write a code that counts the number of cats created (count) and then displays it on the screen.
*/

public class Solution
{
    public static void main(String[] args)
    {
        Cat cat1 = new Cat();
        //add your code here

        Cat cat2 = new Cat();
        //add your code here

        System.out.println("Cats count is " + Cat.count);
    }
    public static class Cat
    {
        public static int count = 0;
    }
}

asked 14 Mar '15, 04:51

Rajat's gravatar image

Rajat
316
accept rate: 0%

edited 14 Mar '15, 08:37

Elleonora%20Carry's gravatar image

Elleonora Carry ♦♦
275


you will have to call the class constructor of cat . by this you can get the count value .

link

answered 17 Mar '15, 14:41

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311324
accept rate: 0%

You must write your solution and people help you. Print ready solutions is prohibited the forum rules.

link

answered 14 Mar '15, 06:17

RabenokDmitry's gravatar image

RabenokDmitry
1263
accept rate: 14%

actually the solution is rather simple. what worked for me is that I incremented the count after each new cat.

link

answered 15 Mar, 10:45

Vagon's gravatar image

Vagon
11
accept rate: 0%

you will have to call the class constructor of cat . by this you can get the count value .

HOW?!

EVERYONE MUST WRITE

  1. Cat.count ++;

In both cases!

link

answered 16 Jun, 03:05

MykytaZ's gravatar image

MykytaZ
11
accept rate: 0%

edited 16 Jun, 03:10

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×36
×11
×4

Asked: 14 Mar '15, 04:51

Seen: 1,993 times

Last updated: 16 Jun, 03:10