Mark Ravenous wrote:
After running it, I get "10 food items left" regardless of what I initialize the value of roomInStomach to be, so I know this isn't correct.
Viktor Logwin wrote:You only need a main method to call the method eatCheese from the book example with some int parameter.
No further modifications needed
Henry Wong wrote:
Mark Ravenous wrote:
After running it, I get "10 food items left" regardless of what I initialize the value of roomInStomach to be, so I know this isn't correct.
All you did was place the example code into a method that is never called. You get 10 items because that is what the value was initialized to.
Henry
Mark Ravenous wrote:
Apparently, Eclipse has an issue with the return type being void. I change it to int, and it doesn't like that either. Thank you.
Mark Ravenous wrote:
Henry Wong wrote:
Mark Ravenous wrote:
Apparently, Eclipse has an issue with the return type being void. I change it to int, and it doesn't like that either. Thank you.
Java doesn't allow the declaration of methods inside another method. Please declare your methods separately.
BTW, if you are having issues seeing this, here is your code with correct indentation...
Mark Ravenous wrote:
Henry
I tried enclosing the eatCheese method within the main() method and various other approaches but I continued to receive various errors.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
I tried enclosing the eatCheese method within the main() method and various other approaches but I continued to receive various errors.
I am not sure if you realize the difference between method definition and a method call.
This is method definition:
It is just a definition of what this method will do when it is called. If no one calls this method, nothing will happen.
Here is another method definition with two calls to two methods:
Here is the complete code that will work:
So now, there are a few things that you missed -
1. you did not realize that you can't define a method within a method.
2. non-static method cannot access static variable of the class directly.
3. you had two variables named food that are accessible in eatCheese - the static variable food and the method parameter food. you did not realize what will happen in such situation.
4. you did not realize that you have to call a method to execute it.
5. you did not realize that how to call an instance method from a main method.
These things tell me that you are trying to run too fast. You need to first go through a beginner level Java book. That might save you a lot of frustration because a certification book will assume that you know the basics.
HTH,
Paul.
Mark Ravenous wrote:
My two questions are:
#1 A while loop is supposed to continue to run as long as the condition is true. In this case, when the eatCheese method is called, we are only calling it using 10 as an argument. Shouldn't this continue to run again and again, until there is no more room left in the stomach?
#2 I also declared a food variable. I initialized it as 100. Even after taking 10 away from it, the value comes back showing as 100 printed in the console. I also noticed that my food--; on line 6 is grayed out while roomInStomach on line 7 is not, in Eclipse. Why is my food varible being given a different treatment?
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
So now, there are a few things that you missed -
2. non-static method cannot access static variable of the class directly.
HTH,
Paul.
Hi, I'm not sure I followed that point.
You can in fact access any static members of a class inside an instance method.
You cannot do the other way, I mean, you cannot access instance methods or fields of "this" in a static context.
Saludos.
Alberto Ibarra wrote:
Paul Anilprem wrote:
So now, there are a few things that you missed -
2. non-static method cannot access static variable of the class directly.
HTH,
Paul.
Hi, I'mnot sure I followed that point.
You can in fact access any static members of a class inside an instance method.
You cannot do the other way, I mean, you cannot access instance methods or fields of "this" in a static context.
Saludos.
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Mark Ravenous wrote:
My two questions are:
#1 A while loop is supposed to continue to run as long as the condition is true. In this case, when the eatCheese method is called, we are only calling it using 10 as an argument. Shouldn't this continue to run again and again, until there is no more room left in the stomach?
#2 I also declared a food variable. I initialized it as 100. Even after taking 10 away from it, the value comes back showing as 100 printed in the console. I also noticed that my food--; on line 6 is grayed out while roomInStomach on line 7 is not, in Eclipse. Why is my food varible being given a different treatment?
Before I answer these questions, can you tell me what is your background? Have you done any programming before or this is the first time you are learning a language? Nothing wrong in either case but the answers will depend on that.
Mark Richardson wrote:
Paul Anilprem wrote:
Mark Ravenous wrote:
My two questions are:
#1 A while loop is supposed to continue to run as long as the condition is true. In this case, when the eatCheese method is called, we are only calling it using 10 as an argument. Shouldn't this continue to run again and again, until there is no more room left in the stomach?
#2 I also declared a food variable. I initialized it as 100. Even after taking 10 away from it, the value comes back showing as 100 printed in the console. I also noticed that my food--; on line 6 is grayed out while roomInStomach on line 7 is not, in Eclipse. Why is my food varible being given a different treatment?
Before I answer these questions, can you tell me what is your background? Have you done any programming before or this is the first time you are learning a language? Nothing wrong in either case but the answers will depend on that.
Paul, I have been learning on and off for a year. I have had the opportunity to make small edits to real projects which utilize Spring, web-services, DAO objects, etc. I have found that the journey to programming is not necessarily a linear one - but you are correct: my basic understanding on a few things is not solid. I tried using other books and sources, but so far, I seem to be making the best (and most consistent progress) using this book and have so far understood everything up until page 77 of this book where I have been able to replicate all the examples... until now![]()
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Mark Richardson wrote:
Paul Anilprem wrote:
Mark Ravenous wrote:
My two questions are:
#1 A while loop is supposed to continue to run as long as the condition is true. In this case, when the eatCheese method is called, we are only calling it using 10 as an argument. Shouldn't this continue to run again and again, until there is no more room left in the stomach?
#2 I also declared a food variable. I initialized it as 100. Even after taking 10 away from it, the value comes back showing as 100 printed in the console. I also noticed that my food--; on line 6 is grayed out while roomInStomach on line 7 is not, in Eclipse. Why is my food varible being given a different treatment?
Before I answer these questions, can you tell me what is your background? Have you done any programming before or this is the first time you are learning a language? Nothing wrong in either case but the answers will depend on that.
Paul, I have been learning on and off for a year. I have had the opportunity to make small edits to real projects which utilize Spring, web-services, DAO objects, etc. I have found that the journey to programming is not necessarily a linear one - but you are correct: my basic understanding on a few things is not solid. I tried using other books and sources, but so far, I seem to be making the best (and most consistent progress) using this book and have so far understood everything up until page 77 of this book where I have been able to replicate all the examples... until now![]()
The reason I asked this is that your questions are very fundamental. Consider the first question -
The condition of the while loop in eatCheese method is food > 0 && roomInStomach > 0 . So why do you think the number of times the loop will execute depends only on roomInStomach? What about food?
Now to your second question -
Do you understand that in your code, inside the eatCheese method, there are two food variables that are accessible? Which are thos? Do you understand which one of them is being decremented when you do food-- or check food>0 inside this method and why? Eclipse is also probably showing a hint about why it is greying out the food-- line. Did you read that hint?
Paul Anilprem wrote:
Mark Richardson wrote:
Paul Anilprem wrote:
Mark Ravenous wrote:
My two questions are:
#1 A while loop is supposed to continue to run as long as the condition is true. In this case, when the eatCheese method is called, we are only calling it using 10 as an argument. Shouldn't this continue to run again and again, until there is no more room left in the stomach?
#2 I also declared a food variable. I initialized it as 100. Even after taking 10 away from it, the value comes back showing as 100 printed in the console. I also noticed that my food--; on line 6 is grayed out while roomInStomach on line 7 is not, in Eclipse. Why is my food varible being given a different treatment?
Before I answer these questions, can you tell me what is your background? Have you done any programming before or this is the first time you are learning a language? Nothing wrong in either case but the answers will depend on that.
Paul, I have been learning on and off for a year. I have had the opportunity to make small edits to real projects which utilize Spring, web-services, DAO objects, etc. I have found that the journey to programming is not necessarily a linear one - but you are correct: my basic understanding on a few things is not solid. I tried using other books and sources, but so far, I seem to be making the best (and most consistent progress) using this book and have so far understood everything up until page 77 of this book where I have been able to replicate all the examples... until now![]()
The reason I asked this is that your questions are very fundamental. Consider the first question -
The condition of the while loop in eatCheese method is food > 0 && roomInStomach > 0 . So why do you think the number of times the loop will execute depends only on roomInStomach? What about food?
Now to your second question -
Do you understand that in your code, inside the eatCheese method, there are two food variables that are accessible? Which are thos? Do you understand which one of them is being decremented when you do food-- or check food>0 inside this method and why? Eclipse is also probably showing a hint about why it is greying out the food-- line. Did you read that hint?
Mark Richardson wrote:
In regards to question #2, unfortunately Eclipse is not giving any warnings to let me know why food is grayed out. Nevertheless, I am understanding now that in my earlier example, the food variable was appearing twice. Once as a satic class variable, and then once as a local variable as a parameter in the eatCheese method declaration.
I have re-written the code now as follows:
Now I am getting an error here where I am being told that "food cannot be resolved to a variable."
Paul, by the way, I wrote this also and it appears to work. In this example,
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:
Mark Richardson wrote:
In regards to question #2, unfortunately Eclipse is not giving any warnings to let me know why food is grayed out. Nevertheless, I am understanding now that in my earlier example, the food variable was appearing twice. Once as a satic class variable, and then once as a local variable as a parameter in the eatCheese method declaration.
You did not answer the last part of the question #2 that I asked - Do you understand which one of them is being decremented when you do food-- or check food>0 inside this method and why?
I have re-written the code now as follows:
Now I am getting an error here where I am being told that "food cannot be resolved to a variable."
You have removed the static variable food. Which food variable do you now expect System.out.println(food + " amount of food left"); to access and why? Have you read about scope of variables? This should be there in the book in the beginning chapters itself.
Paul, by the way, I wrote this also and it appears to work. In this example,
This is better. But I am not sure whether you got it to work by fluke (trial and error) or by design. If you can explain why it works, you have certainly moved forward. You can also explain the issue with the previous version![]()
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|