aspose file tools*
The moose likes Java in General and the fly likes using nested loops to create instances of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "using nested loops to create instances of a class" Watch "using nested loops to create instances of a class" New topic
Author

using nested loops to create instances of a class

kim george
Greenhorn

Joined: Sep 09, 2011
Posts: 9
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

Joined: Aug 16, 2005
Posts: 14352
    
  22

The for Statement explains how to make loops.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Sep 09, 2011
Posts: 9
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

Joined: Sep 09, 2011
Posts: 9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using nested loops to create instances of a class