aspose file tools*
The moose likes Beginning Java and the fly likes So can I invoke methods correctly now? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "So can I invoke methods correctly now?" Watch "So can I invoke methods correctly now?" New topic
Author

So can I invoke methods correctly now?

Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Hey all,

I have been asked the following question:

Study the following code and then select the options from the drop-down lists below that are correct about the values of a, b and c after the method mQ2 is invoked once on a newly created object of class Q2. Note that the answers you choose for a, b and c may or may not be different from each other.




Using NetBeans IDE, here is what I have done:

My main.java file




My QuestionTwo.java class file:




However I do not think my println statement is in the correct place to display the results?
This question is confusing me with its local variables -- think I am nearly there though. Any pointers?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
You have been given a badly-designed class if it doesn’t have all its fields marked private. Your println() looks all right, except that you need a space between " and b. You can get your hands on a and b like this qt.a etc. But you cannot get your hands on c, because it is a local variable. And there are some confusing features about c, which I shall keep quiet about for the time being.

You are supposed to use a pencil and paper to work out the answer to your question, I think.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
As Campbell says, c is the interesting variable here.
What are the possible answers that you have been given ?


Joanne
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
The options for each are 0, 1, 2, 3, 4 and undefined.

It's quite defeating when you can't even get your program to display a simple output :\
Thanks for the help though -- it's supporting to know there is a community out there that are willing to help 'greenhorns' like myself
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
To help a little bit more - you can't write a program to get the answer to this question. As Campbell says - you need to do it with pencil and paper.
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
I got
a = 1
b = 1
c = undefined.

Would you agree?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
If you explain why you think those are the answers, I'll tell you if I agree.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
I don’t think they have given you the correct value for c when you reach the println(). Or, the correct value for c is not among the options you are given.

Do they say which line you are supposed to be at when you get those values? Is it after 19 (a++;) and before 20, which is a single }?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
Campbell Ritchie wrote:I don’t think they have given you the correct value for c when you reach the println(). Or, the correct value for c is not among the options you are given.

Do they say which line you are supposed to be at when you get those values? Is it after 19 (a++;) and before 20, which is a single }?

The question says 'after the method mQ2 is invoked'. Assuming this means after it has returned, then c is undefined and is the reason you can't write a program to get the answer to this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
You mean after the method has returned? I actually think c is less than undefined. I think it simply doesn’t exist. Actually it has gone out of scope by line 19, too. There is an undefined variable called c in lines 9-10 and 14-15, or thereabouts, but by line 19 c simply no longer exists.

You would have to write “undefined” for that question, as the nearest approach to the real state of events.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
1, 1, is correct, isn’t it, Joanne?
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Bit difficult for someone who has been involved with Java for about 3 weeks, no?

So far I have been given about 70 very light pages of reading to do, and I am expected to answer questions like this without looking outwith the course material. Last year I got marks removed for citing references outside of the course material on a written question.

The deadline was over an hour ago for the assessment, so it has been submitted.


I just went with 1, 1 and undefined for a, b and c respectively.

I put a


anywhere the program would allow it. When I ran the program the last outputs I got were 1 and 1, so just went with that. Can't wait to get the solution back to see how it should have been done!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3528
    
  15
As you have submitted your answer, I'll explain.
Whenever you have opening and closing squiggly brackets { }, any variables declared within them is only defined within them. Outside of the {} they are undefined (or don't exist). The variables are said to be 'in scope' between their declaration and the }.

So, you have a variable c declared on line 11 which is only in scope between line 11 and line 14.
You have a second c variable declared on line 17 - note that this is totally unrelated to the variable declared on line 11. It is in scope between lines 17 and 21.
So, after the method returns there is no c variable in scope and so it is undefined. In actual fact, as Campbell says, it doesn't exist.

Variable b. This is the easy one. It is an instance variable. It is in scope between lines 6 and 26. So if you have an object of type QuestionTwo it will always contain a valid b variable. It is initialised to 1 at line 6 and never changed after that, so its value will be 1.

Variable a. There are three a variables.
The one declared at line 5, which like b is an instance variable - because there is no explicit initialisation of it, it will be set to zero.
The one declared at line 12, which like c is a local variable and is only in scope between lines 12 and 14. However, between those lines, it will hide the a variable declared at line 5. So the code between lines 12 and 13 has no affect on the a declared at line 5.
The one declared at line 18. Again a local variable, again it hides the a at line 5, again line 20 has no affect on the line 5 variable.

So when we get to line 22, nothing has been done to the line 5 a and the line 12 a and line 18 a no longer exist. Line 23 then increments the line 5 a from 0 to 1.

Simple really You just need to be aware of which variable is being referenced at each point. When writing your own code, always try and avoid having local and instance variables with the same name and then you won't need to worry.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Ali Lumsden wrote:. . . Last year I got marks removed for citing references outside of the course material on a written question. . . .
What!!

As Joanne has explained, you had it correct.
Ali Lumsden
Greenhorn

Joined: Oct 26, 2011
Posts: 14
Well I'm glad I got the answer right.
Though not without some help from this site

I get the principle of 'code-blocks' as they are referred to. The only preparation I received for this question is half a page that uses this example:

You can nest code blocks, as shown below:



So how they expected me to go from that, to the question I started this thread with, is beyond me. I am entirely new, as you've gathered not only to Java, but programming altogether. I study a BA(Hons) degree in Business currently, but do this work as well to keep me busy. Plus I find it interesting.

Though having to remember to do assignments one way for one institution and one way for the other, is sometimes confusing.
Though this Open University is just a joke for teaching in general. I really dislike it, but just need to make it a year and that should be all the Java done with!

Thanks alot for your time and input, it's been interesting. Expect to see me back in a few nights with questions -- although these shouldn't be assessment related!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: So can I invoke methods correctly now?