Very simple problem...PLEASE HELP!
Consider the following code which is supposed to add a value passed to the method to an instance variable; however, there is a problem. Re-write this code, without renaming any variable names, to resolve the problem.
Code Java:
private int number = 123;
public void addNumbers (int number)
{
number = number + number;
System.out.println("The local variable is: " + number);
System.out.println("The instance variable is: " + number);
}
I have no idea where to even start... any ideas?
Re: Very simple problem...PLEASE HELP!
Quote:
I have no idea where to even start... any ideas?
Yes, write a runnable piece of code and test it...check the output, what you expect and what you get. Then fix it (hint: two variables have identical names - you must differentiate between the two).
Re: Very simple problem...PLEASE HELP!
I came with moving the print statement to the front so I have this:
Code Java:
private int number = 123;
public void addNumbers (int number)
{
System.out.println("The local variable is: " + number);
number = number + number;
System.out.println("The instance variable is: " + number);
}
Which takes care of the local variable. My problem is finding a way to differentiate between the two number variables, do you have any advice?
Re: Very simple problem...PLEASE HELP!
Read through the Oracle tutorials, the answer is in this link (and yes, there is a clue in this post)
Re: Very simple problem...PLEASE HELP!
Quote:
Originally Posted by
copeg
Read through the Oracle tutorials, the answer is in
this link (and yes, there is a clue in this post)
:) this will help you.