Sally Johnson used to work at the buffet at the Podunk intergalactic station on Terra, but she stopped upon reaching retirement age. Now she collects cats. Over the years, it has become difficult to control the number of purring creatures: cats reproduce rapidly. We won't solve this problem for Sally, but we will write a method that records the number of cats.
Setting the number of cats
- 2
Locked
Comments (10)
- Popular
- New
- Old
You must be signed in to leave a comment
Daniel
4 May 2020, 18:00
Be careful what the last condition says. The keyword here is set, NOT increase.
The passed integer sets the declared integer up. End.
I did not pay attention and I set up the method to throw the number taken inside the variable, which increases the number, it doesn't just set the number up.
What I did was "variable is variable + passedInteger ";
Pay attention. :)
0
adevalebrook
22 November 2019, 11:46
Here you can find the explanation: https://stackoverflow.com/questions/11664522/why-cant-we-use-this-keyword-in-a-static-method
0
AlasCaras
11 March 2021, 20:30
that link is ligit
0
Sachin
22 November 2019, 05:45
The Cat class's setCatCount method must set the value of the catCount variable equal to the passed argument.
jst read the last requirement you will know what to do.
Cat c = new Cat();
c.setCatCount(5);
you need to just assign pass value to static var.
0
John
27 June 2019, 21:06
I don't see how this is different from the previous problem.
If I send a value to setCatCount from 'main" like:
public static void main(String[] args) {
Cat cat = new Cat();
cat.setCatCount(4);
The value of catCount in the setCatCount method is 4, but the value of catCount in the main method is still 0. I used a similar approach in the previous problem and it worked, but it's not working here. Using 'this." doesn't work.
0
John
27 June 2019, 21:16
I got it to work using Cat.catCount per bri. But I don't know why this works. Can someone explain?
0
bri
27 January 2019, 13:54
<aybe this is a silly question, but:
When should we know when to use this.blablabla and the overall class.blablabla?
ex.
this.catCount v.s. Cat.catCount?
0
Bryce Lindley
15 February 2019, 00:42
I believe you use this. when there is an argument in the method () " variable in the method parameters "
Cat.catCount is calling the global variable static catCount().
Perhaps?
0
Thomas Ward
21 February 2019, 00:13
Could the keyword static be a factor?
0
GarBer
22 May 2019, 14:18
I think it's when an object is NOT passed to the method. This is true for static methods (I think).
0