Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Study Guide OCA - Assessment Test #14  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize this has been explained, but I still have a question.  

If house.getChickens().size() = 0, then the for loop is never executed, resulting in no output- happily so even.
I thought the update statements aren't touched until after the body executes, which it never does.



This compiles and runs without any output.  What am I missing?  Thank you

Thank you for your quick reply.  But the book says that this will either have a runtime exception or produce at least one line of code.
That is what I don't understand.  My example shows that it will successfully run and produce no output, which is in contradiction with
the answer in the book.
Thank you again, Lisa
 
Sheriff
Posts: 13454
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Since the body of the loop never gets executed, neither will the update or increment clause.

When posting code, please UseCodeTags (←click that, it's a link to a wiki page that explains how to use them)
 
Alisa Crowe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply.  But the book says that this will either have a runtime exception or produce at least one line of code.
That is what I don't understand.  My example shows that it will successfully run and produce no output, which is in contradiction with
the answer in the book.
Thank you again, Lisa
 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch (again).

Alisa Crowe wrote:. . .  But the book

Which book? Please also give page numbers.

says that this will either have a runtime exception

Remember that all exceptions occur at runtime. Beware of confusion with this class, one of the worst‑named classes in the standard API.

or produce at least one line of code.

You mean one line of output, not code. Please be precise with your nomenclature. If the book says, “code,” that would worry me a lot.

. . . . My example shows that it will successfully run and produce no output, which is in contradiction with the answer in the book.
Thank you again, Lisa

That either means you have changed something from what the book says, or there is a mistake in the book. Please check carefully because it is more likely that you haven't quite copied the page exactly.
 
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alisa Crowe wrote:I realize this has been explained, but I still have a question.  

If house.getChickens().size() = 0, then the for loop is never executed, resulting in no output- happily so even.
I thought the update statements aren't touched until after the body executes, which it never does.



This compiles and runs without any output.  What am I missing?  Thank you

Thank you for your quick reply.  But the book says that this will either have a runtime exception or produce at least one line of code.
That is what I don't understand.  My example shows that it will successfully run and produce no output, which is in contradiction with
the answer in the book.
Thank you again, Lisa

condition is there, so body executed well but it is not updating rather it is the value of i is updated and assigned to calling method get(),as formal parameter, I am not sure that you are not adding values to arraylist,before for loop, may be get(), does add values but it should be before condition of the for loop, so that it check for condition in for loop, and define the variable i outside of for loop that you can write your code before condition of for loop if you are use commend line argument its OK, I am not sure get() will return the updated value because it is i has  postfix increment operator and you are assigning to just variable j, not to list
 
Campbell Ritchie
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . condition is there, so body executed well but it is not updating rather it is the value of i is updated and assigned to calling method get(),as formal parameter, I am not sure that you are not adding values to arraylist,before for loop, may be get(), does add values but it should be before condition of the for loop, so that it check for condition in for loop, and define the variable i outside of for loop that you can write your code before condition of for loop if you are use commend line argument its OK, I am not sure get() will return the updated value because it is i has  postfix increment operator and you are assigning to just variable j, not to list

Please don't quote the whole of a previous thread; that adds nothing to the discussion.

I don't understand what you are saying there; it appears confused, but I don't think it is correct. Let's have another look at it:-

. . . condition is there, so body executed well . . .

That isn't correct. Because the List is empty, the body of the loop isn't entered nor executed, nor is the i++ part of the increment part reached.

the value of i is updated and assigned to calling method get(),as formal parameter,

No, i is not updated. It is not assigned as a formal parameter; if anything happened, the value of i would be passed as an argument. Please be careful with the nomenclature.

may be get(), does add values

No, the get() method does not add anything to a List.

define the variable i outside of for loop

That is usually not necessary; it is actually usually better to declare the loop variable as part of the loop; it will go out of scope sooner.

if you are use commend line argument its OK

What does that mean? What difference would a command line argument make?

I am not sure get() will return the updated value

If you managed to get into the increment part of the loop, you wouldn't get a value from the List; in fact it would throw an exception for trying to get a non‑existent value from a List.

. . . you are assigning to just variable j, not to list

Yes, that part is correct
 
sohail hussain
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I compiled it, I got nothing output because an empty array list is created which is fine, and a variable j is declared, and in for loop we are checking the variable i is less than list size(), which is false so come out of the loop and display nothing, is that OK to read the code like this and according to the code output is different, may be        wrong question  
 
I think I'll just lie down here for a second. And ponder this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!