• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

help with display routine

 
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Commented in even plainer English:

I just shortened out the variable name in case it was confusing you, being so similar to the class name, Flower. The style in Java is to just spell out the name since single-letter variables are generally frowned upon, with exception of index variables like i, j, k.
 
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Jeremy Wages wrote:I've never seen a for-each loop.  It's similar to a for loop except it compares a flower object to pack array?  I think?


It's a higher-level version of the regular for-loop. It just goes through anything that is iterable, like an entire array or a Collection (List, Map, Set) without the programmer having to explicitly specify a loop index variable. It's what's called "syntactic sugar" because it makes programming certain kinds of loops "sweeter".  It doesn't compare a flower object to a pack array.  The pack variable is declared as an array of Flower objects. That means you can iterate over that array.  The loop variable is the "each", that is, as you iterate over the array, pack, each Flower in the array will be assigned to the loop variable, flower. In the body of the loop, you can use the flower loop variable to refer to the flower that is currently being iterated over.



I also read it helps eliminate mistakes as well.


These two for-loops are equivalent:

The only reason you need the loop index variable i is so you can use it to access a particular element of the array, but that's really just an implementation detail. The for-each version makes your intent clearer: you're really interested in each flower, not so much the index. In the for-each version, you get down to intent and let Java take care of the details of keeping track of where to start (initializing to 0 to start at first element), which element it currently is looking at (the loop index variable, i), moving to the next element (i++), and when to continue or stop (keep going if i < pack.length).



I think I'm starting to understand.  I like the for-each loop better, I think.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:I think I'm starting to understand.  I like the for-each loop better, I think.


Good. There's a lot to like about the for-each loop, also known as the "enhanced for-loop".  Just know that it's not suitable for all iterative (repetitive) tasks. The regular for-loop syntax can be a more natural fit for some algorithms where you need to track and use the index variables for some reason. But sure, when it makes your code clearer and cleaner, prefer using the enhanced for-loop syntax.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Commented in even plainer English:

I just shortened out the variable name in case it was confusing you, being so similar to the class name, Flower. The style in Java is to just spell out the name since single-letter variables are generally frowned upon, with exception of index variables like i, j, k.



One thing. With the for each loop, are you creating a new flower object that is the same as the flower array?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No new Flower objects are created.  The f loop variable is a reference.  It is assigned to each already existing Flower object that is iterated over in the loop.  If the array element is null, then the f reference variable will also be null. That's what I was hinting at before in my followup reply.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:No new Flower objects are created.  The f loop variable is a reference.  It is assigned to each already existing Flower object that is iterated over in the loop.  If the array element is null, then the f reference variable will also be null. That's what I was hinting at before in my followup reply.



Okay, I need some object clarification: is the array an object or is each element in the array an object?
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a more explicit equivalent of the for-each loop:

The first two lines in the above code is the equivalent of this:

As with the regular for-loop, no new Flower objects are created, you're just assigning a reference to an existing object to the reference variable f
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:Okay, I need some object clarification: is the array an object or is each element in the array an object?


You're driving me nuts, man...

A Flower is an object. An array is an object. This array that I'm calling pack just happens to be an array (yes, it's an object) that contains Flowers, which are themselves objects.

It's like how a box is an object and a ball is an object. But a box can also hold a bunch of balls in it.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Here's a more explicit equivalent of the for-each loop:

The first two lines in the above code is the equivalent of this:

As with the regular for-loop, no new Flower objects are created, you're just assigning a reference to an existing object to the reference variable f



So the right half of the for each is automatically referencing the length of the array? I guess I'm so used to seeing "regular" for loops it is hard for me to not need that .length.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you still need further visualization, think of the variable f as your finger. Every time through the loop, you point your finger to the next Flower in the pack. That's all a reference variable really is, it's like a finger that points to an object.  So when you say f.getName(), you're getting the name of the Flower object that your finger is pointing at.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you still need further visualization, think of the variable f as your finger. Every time through the loop, you point your finger to the next Flower in the pack. That's all a reference variable really is, it's like a finger that points to an object.  So when you say f.getName(), you're getting the name of the Flower object that your finger is pointing at.



I think it's starting to come together. Sorry, I know it's probably very frustrating for someone who is skilled, but I am trying.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:So the right half of the for each is automatically referencing the length of the array? I guess I'm so used to seeing "regular" for loops it is hard for me to not need that .length.


If you could see my face right now, you'd see the very definition of "exasperation" ... no worries, I realize it can be difficult to wrap your head around it, especially if you're used to seeing all the nitty-gritty details.

The point of the for-each construct is to relieve you, the programmer, of the burden of having to worry about the length of whatever bunch of things you're trying to go through in the loop. So, just forget about length (an implementation detail) and think about your intent.  You want to go through all the elements of the array. The right half of the for-each is saying "This is the bunch of stuff I want to go through, one by one".  The left side of the for-each is saying "Each thing in that bunch of stuff is going to be a Flower and I want to refer to the current Flower that we picked out of the bunch as f".
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Jeremy Wages wrote:So the right half of the for each is automatically referencing the length of the array? I guess I'm so used to seeing "regular" for loops it is hard for me to not need that .length.


If you could see my face right now, you'd see the very definition of "exasperation" ... no worries, I realize it can be difficult to wrap your head around it, especially if you're used to seeing all the nitty-gritty details.

The point of the for-each construct is to relieve you, the programmer, of the burden of having to worry about the length of whatever bunch of things you're trying to go through in the loop. So, just forget about length (an implementation detail) and think about your intent.  You want to go through all the elements of the array. The right half of the for-each is saying "This is the bunch of stuff I want to go through, one by one".  The left side of the for-each is saying "Each thing in that bunch of stuff is going to be a Flower and I want to refer to the current Flower that we picked out of the bunch as f".



Thank you. That helps immensely.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Time for a break... please excuse this little indulgence but I feel we need to have a bit of a laugh...

A little girl goes up to her grandfather one day and asks him, "Grandpa, what's exasperation? Is that the same thing as frustration?" Grandpa stands his granddaughter up on a chair by the window and says, "See your dad out there, relaxing and reading the newspaper in the warm sun on the patio? Watch this..." Grandpa picks up his cell phone and calls his son. His son, whose name is Jim, picks up and Grandpa asks in a fake voice, "Hello, is Bob there?" The son, visibly annoyed to have his relaxing morning interrupted, says "No, there's no Bob here. You have the wrong number" and hangs up.

Little girl asks, "Was that exasperation?"

Grandpa says, "No that was just annoyed."

Grandpa calls his son and asks for Bob again. Again the son says no and hangs up. Grandpa calls a couple more times, doing the same thing. At this point, Jim has had enough and yells into the phone, "FOR THE LAST TIME, YOU HAVE THE WRONG NUMBER! THERE'S NO BOB HERE!"

Little girl asks, "Was that exasperation?"

Grandpa says, "No, but that was a pretty good frustration."

Grandpa calls Jim one more time and says, "Hello, this is Bob. Did anybody call for me?"

Pointing to her dad, he tells his granddaughter, "That's exasperation."

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha, probably explains how you were feeling perfectly!
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about this.  Seems to be working.

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ugh, always forget that [/code].
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also realize now I don't need the boolean.  I have removed it.
 
Junilu Lacar
Sheriff
Posts: 17073
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 12, you don't need to check for != null because that's already guaranteed by the code above it, so the != null check is just redundant.

Seems to me the code doesn't actually work properly if the first flower it examines isn't a match. Don't get used to using break in your for loops. The break in the else part on line 20 is a problem because it terminates the for-loop prematurely and incorrectly.

That whole else part should be outside of the for-loop, without the break statement and of course without the "else".
 
Marshal
Posts: 76406
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . he tells his granddaughter, "That's exasperation."

He shou‍ld know better than to answer the phone.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:On line 12, you don't need to check for != null because that's already guaranteed by the code above it, so the != null check is just redundant.

Seems to me the code doesn't actually work properly if the first flower it examines isn't a match. Don't get used to using break in your for loops. The break in the else part on line 20 is a problem because it terminates the for-loop prematurely and incorrectly.

That whole else part should be outside of the for-loop, without the break statement and of course without the "else".



That makes sense.  I see why I don't need the != null.  I thought I checked flowers in different positions, but I'll check again this afternoon.
 
Marshal
Posts: 8385
594
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeremy Wages wrote:

I'm sorry, but what I'm missing here? You might writing all this to a wrong method by mistake?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Jeremy Wages wrote:

I'm sorry, but what I'm missing here? You might writing all this to a wrong method by mistake?



I can't confirm until I'm home, but I'm almost positive I copied the wrong method when I went to post it and when I deleted it, just wasn't thinking and copied the body of the method leaving the wrong method name up top.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally finished! Thank you for all the help!

 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great job!  Here are some things to think about:

  • What happens when you are asking for a number and the user types a letter?
  • In displayFlowers(), consider using a Map instead of two parallel arrays

  • Also, I noticed that you are opening Scanner in many places.  I think you should create an instance variable and open Scanner only once.
     
    Jeremy Wages
    Ranch Hand
    Posts: 141
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Great job!  Here are some things to think about:

  • What happens when you are asking for a number and the user types a letter?
  • In displayFlowers(), consider using a Map instead of two parallel arrays

  • Also, I noticed that you are opening Scanner in many places.  I think you should create an instance variable and open Scanner only once.



    I thought about the user typing the wrong thing and I started adding code to circumvent it, but figured it was best to just get it turned it because the program does what the assignment states to do.

    I was instructed to use two parallel arrays.

    Thank you for the suggestion about Scanner!
     
    I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic