• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using nested loops to create instances of a class

 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am being asked to use nested loops to create instances of the class (EOHoverFrog) with states covering all possible combinatinos of the colours red and green, all visible positions (1 to 11), and all heights (0 to 6) - there are 154 combinations. All of these instances of EOHoverFrog should be added to the collection provided as the method's argument. Your method should then print to the Display Pane: "Number of EOHoverFrogs in collection = "

Basically I have no idea how to start. The only way i know of creating a new instance is EOHoverFrog f1 = new EOHoverFrog. Any clues as to how to go about it using a loop just to get me started?

Thanks, Kim.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
 
Stephan van Hulst
Bartender
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kim,

Do you know how for-loops work? If you put a loop in another loop, it will repeat the entire second loop for each iteration of the first one.

This example will print the tables of 1-3:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tackle the problem in SMALL pieces...do NOT try and do it all at once.

I would approach it this way...

First, write a single loop that simply prints all possible colors:


Once you have that printing out both red and green, add in a way to print all the positions for each...it would look something like


once THAT works, add in the code for height. Once THAT works, add in code to create the actual object you need. Once THAT works, add in the code to save the objects as you create them...etc.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought a for-each loop (=enhanced for loop) easier. Let's rewrite Fred's last example:You can see it works for colors where you are using an array. You can't use it for p because p isn't an array.
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly I want to apologise in case this doesn't work properly, I don't have internet at home yet so I'm using my phone now...

But so far I've got this


I know that isn't right because it's not compiling. First error message is “cannot find symbol - variable HashSet“ again I didn't think it was HashSet but I don't know what else to put there.
Also, where I've put .... I'm not sure what to put there. I need to add all of these instances of EOHoverFrog to collection provided as the method's argument (I don't understand that bit), and finally return the collection...oh and if I comment out where I get the first error message then I get a second one on the last line “cannot find symbol - variable populatePond“.
 
kim george
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I just re-read the question. My method should have a single argument, whose type should work equally well for a list, or a set, or a sorted set with elements of type EOHoverFrog. The returned collection should be of the same type. I've tried changing it to
Public static EOHoverFrog populatePond(EOHoverFrog frog), that compiles but I don't know if its actually right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic