Forums Register Login

So can I invoke methods correctly now?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
As Campbell says, c is the interesting variable here.
What are the possible answers that you have been given ?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I got
a = 1
b = 1
c = undefined.

Would you agree?
+Pie Number of slices to send: Send
If you explain why you think those are the answers, I'll tell you if I agree.
+Pie Number of slices to send: Send
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 }?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
1, 1, is correct, isn’t it, Joanne?
+Pie Number of slices to send: Send
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!
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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!
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 781 times.
Similar Threads
4 quest diffulties please help
Interfaces - 6 questions
5 javaprepare questions
Can we have inner interfaces ...!?
java
I can't figure out how to solve the error in my code
While-Loop for ending program from user input
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:01:16.