Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

using nested loops to create instances of a class

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for Statement explains how to make loops.
 
Saloon Keeper
Posts: 13505
305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
lowercase baba
Posts: 13025
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 74780
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic