this is the task in which i have to write a method setCatsCount. and i wrote following code in that method is given below . but it has a error of a pass testing but program runs correctly . help needed . / Implement method setCatsCount() Implement the setCatsCount method, so the value of the catsCount variable would be equal to the passed parameter. /

public class Cat { private static int catsCount = 0; public static void main (String[] args){}

public static void setCatsCount(int catsCount) {
    System.out.println(catsCount);

}

}

asked 17 Mar '15, 12:40

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311324
accept rate: 0%

how can the value of class variable be equal with the value of the passed parameter .

(17 Mar '15, 15:41) Muhammad Has...

123next »

still waiting for some help :(

link

answered 18 Mar '15, 16:52

Muhammad%20Hassan%20Durrani's gravatar image

Muhammad Has...
4311324
accept rate: 0%

link

answered 24 Mar '15, 10:22

Once%20the%20King%20of%20Andorra's gravatar image

Once the Kin...
676
accept rate: 11%

First you have to create an object, then access the static variable and also dont forget the main method..

link

answered 26 Mar '15, 21:59

Zain%20Mehar's gravatar image

Zain Mehar
13
accept rate: 0%

as you said i did like thsi . but i think its not a right way . please guide me .

    public static void setCatsCount(int catsCount) {
    private static int catCount = 0  ;
    this.catCount = catsCount;
}
(30 Apr '15, 09:13) Muhammad Has...

did it like this and then what have to b implement in main ??

private static int catsCount = 0;
static int Count = 0  ;
public static void main(String[] args)
{

}

public static void setCatsCount(int catsCount) {
 catsCount++;
}
(30 Apr '15, 09:24) Muhammad Has...

You should read about an increment operator.

link

answered 01 Apr '15, 05:44

Andrey's gravatar image

Andrey
1323
accept rate: 35%

Cat.catsCount = catsCount;

link

answered 26 Jun '15, 17:35

Asad's gravatar image

Asad
111
accept rate: 0%

Please fix me this :

package com.javarush.test.level04.lesson02.task03;

/ Implement method setCatsCount() Implement the setCatsCount method, so the value of the catsCount variable would be equal to the passed parameter. /

public class Cat { private static int catsCount = 0;

public static void main(String args[])

{

    Cat myCat = new Cat();

    myCat.setCatsCount();

    System.out.println("cat count is " +myCat.getCatsCount(catsCount));

}

public void setCatsCount() {
    //add your code here
    this.catsCount = catsCount;
}
public int getCatsCount(int catsCount)
{
    return this.catsCount;
}

}

link

answered 03 Apr '16, 15:58

peggy's gravatar image

peggy
11
accept rate: 0%

edited 03 Apr '16, 15:58

First of all, you have problems with getters/setters, i.e you implemented them wrong. You can use google for that part.

(04 Apr '16, 16:28) Andrey

And another piece of advice related to the same mistake of yours, read about encapsulation in java, it's very important to understand this concept.

(04 Apr '16, 16:36) Andrey

Please help !

I searched half of internet because of this and nothing helped !!This "set" and "get" is from google.

That new() is correct ? And "this" ?. At least tell me what is correct ! I added "static" to functions but didnt helped even.

link

answered 04 Apr '16, 16:18

peggy's gravatar image

peggy
11
accept rate: 0%

edited 04 Apr '16, 16:46

As I mentioned above, your get and set methods are incorrect. The correct version is below: void setCatsCount(int catsCount) { this.catsCount = catsCount; } int getCatsCount() { return catsCount; } Don't forget about reading about an encapsulation, you defenitly have no idea what getters and setters do, which is very bad if you take programming in Java seriously.

(04 Apr '16, 17:12) Andrey

I didn't find anything serious about "encapsulation"as i didnt find a good source. Where did you find it with examples ?

link

answered 05 Apr '16, 16:28

peggy's gravatar image

peggy
11
accept rate: 0%

Getters and setters are like "Pointers " ? And classes are like "structures" ?

link

answered 05 Apr '16, 16:31

peggy's gravatar image

peggy
11
accept rate: 0%

Don't focus on classes right now. Getters and setters are accessors, the first one is for reading the value from an object's state (an object is simply an instance of a class; state have many names: fields, class and instance variables etc.), and the second one is for writing a value to it, so later on you can read it out by using getter method.

(05 Apr '16, 16:52) Andrey

Here's http://stackoverflow.com/questions/19044362/java-encapsulation-concept-not-clear quite a good explanation what an encapsulation is. And you can start learning Java from official Oracle tutorial https://docs.oracle.com/javase/tutorial/java/concepts/index.html should be very helpful.

(05 Apr '16, 16:52) Andrey
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:

×69
×36
×4

Asked: 17 Mar '15, 12:40

Seen: 6,376 times

Last updated: 27 Jul, 12:04