• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

While Loop Example in Sybex OCA Exam 1Z0-808 Study Guide on pg.77 by Boyarsky and Selikoff

 
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking at page 77 at an example given to illustrate the while loop.

"Let’s return to our mouse example from Chapter 1 and show a loop can be used to
model a mouse eating a meal:"



I understand the code conceptually but I was not able to type this code into eclipse and actually run it. I tried enclosing the eatCheese method within the main() method and various other approaches but I continued to receive various errors. Then I finally tried this:



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.
I like to actually run my examples what I come across in the book in an IDE because it allows me to further my understanding and solidify it.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need a main method to call the method eatCheese from the book example with some int parameter.
No further modifications needed
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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




Victor, I tried to make sense of your advice but I'm not clear on what you are exactly proposing, so here is what I did:



Eclipse doesn't like the fact that the eatCheese method has a void return type. I change it to int, and it still isn't happy.  If you can show me exactly what you mean, it would be helpful. Thank you
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Henry, I tried this as well, along with various variations, but to no avail. If you can show me exactly how it's done, I'll be very grateful. 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.

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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






Henry,

Indention is not the issue. The code itself has issues. Try it on your IDE and you will see what I mean.

This is the error I am getting:

void is an invalid type for the variable eatCheese
 
Enthuware Software Support
Posts: 4810
52
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





Thank you, Paul! That was very informative!

I tried the code that you provided, with some additions, and I have two questions:



The result I get after running this is as follows:

90 room in stomach left
100 amount of food left


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?
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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'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.


You are right. Not sure what I was thinking when I wrote that  Sorry about that.

Request to moderator - I am unable to edit it now so please edit out that part of my post to avoid confusion.
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I think I know what I was thinking - the method eatCheese had to be made static so that it could be called from the static main method. Somehow while typing the response, I wrote something entirely different.
thanks for the correction!
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,

In regards to question #1, the eatCheese method will run as long as food is more than 0 and room in stomach is more than 0. The && operator ensures that both conditions are met and the condition on the left hand side determines whether the condition on the right hand side will even be checked or not. I understand this and there isn't any confusion on the fact that both food and roomInStomach are relevant here.

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."
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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, by the way, I wrote this also and it appears to work. In this example,  I have declared both variables as static (class) variables and my eatCheese() method does not take any parameters. It simply begins the eating process, until the stomach is full... or the food has been depleted. I think you'll be at least semi-proud of me hehe


 
Paul Anilprem
Enthuware Software Support
Posts: 4810
52
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
M Richardson
Ranch Hand
Posts: 228
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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




Paul, so here is my understanding on the "conflict" between the two "food" variables. When we declare the food variable as a method parameter, the scope of that food variable is restricted to that method. This is distinct from the food variable that we have declared as a class (static) variable. Therefore, there were two different food variables with different scopes, which I was mistaking as one.

The way I rectified this was to not declare the food variable as a method parameter, or local variable, but instead, only to declare it as a class (static) variable.

Thank you for challenging me and taking the time and energy to go into the details. Appreciate it
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic