Hello.
The task requirement states "The Person class's adjustAge method should increase the Person's age by 20."
The code for the adjustAge method has age = age+20. I am thinking that this is correct as the formula adds 20 from the passed in class variable. I tried multiplication 20* 20 and age = age + age + 20. All have been unsuccessful.
I may have missed something. Could someone look at my code and give me some feedback?
Unclear task requirement
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Anonymous #10995060
11 May, 11:39
Thanks you guys!
0
public enum WaysToDie
16 December 2021, 21:49
Think carefully about what variable is really being accessed by the code:
public void adjustAge(int age) {
age = age + 20;
...
Because age is both a local and a instance variable we need to specify precisely which variable to change. The above code actually accesses the local variable(method argument) age and sets it equal to itself(argument value) + 20. This is not what the code is supposed to do.
+2
Socello
16 December 2021, 06:45
As you see, because method adjustAge has declear a variable named age(local variable).So in this method the local variable(line 18) will mask the instance variable(line 16).
So, your code just add 20 to the variable you post.But this task hope you change the instance variable,that's why you cant pass.
the soluation is:
Please forgive me for possible misrepresentation, because I am not a native English speaker
+3