Re: Rép. : [Bluej-discuss] exercises 5.49 to 5.53
julio
jamoros2000 at yahoo.com
Wed Mar 8 12:12:36 GMT 2006
Hi. (About the solution of the bounce method when we want random behaviour)
If I want to follow more or less the same "idea" than in the Kolling & Barnes
solution for the bounce method with n balls (without random) we have to know the distance between the first ball and the last one.
if (ball.getXPosition() >= 550 + 32 * numberOfBalls) {
finished = true;
}
Without random behaviour 32 * numberOfBalls is a good distance between the first ball and the last one.
So if we want to find out the distance between the first ball and the last one,
could it be an idea declaring 2 variables for knowing the ball with the minimum x and the max x?
We could ask it inside the for loop after the instruction:
int x = random.nextInt( (int) size.getWidth() );
After that we know this 2 values, and the distance is (xMax -xMin)
Finally the code could be:
if ( ball.getXPosition() >= 550 + (xMax -xMin) ) {
finished = true;
}
On other hand good idea about the Yves counter. And Michael, when you say remove is remove, isn't ?
Thanks
P.D: Thanks for the very interesting answers about last question I did
Michael Kölling <M.Kolling at kent.ac.uk> escribió: The counter method is good and should work.
If you also want to stop moving the balls when they reach the edge,
you could just remove every ball from the ArrayList when it reaches
the 550 point (you do not need to remove it from screen, if you want).
Then, when the ArrarList is empty, you know it's time to stop.
Michael
On 7 Mar 2006, at 13:43, Yves Hamel wrote:
> Hi Luis,
>
> How about adding a counter? Each time a ball hits 550 pixels, that
> counter increases and then you compare it to the size of the
> Arraylist. Actually, going through each element of the ArrayList is
> a O(n) problem size where n is the number of elements in the
> ArrayList , while using a counter turns it into a O(1) problem size
> where this is a constant size no matter how many elements you have
> in your ArrayList. Big O notation is a way to describe the problem
> size in a matter of time execution and memory space. So,
> O(n) > O(1) and a O(n) problem can be very consuming for a large
> set of elements (but most of the time you can't avoid it!)
>
> Yves
>
>>>> Luis.Moreira at jet.uk 03/07/06 4:03 am >>>
> Hi Guys,
>
> I am trying to do the exercises 5.49 to 5.53 and I am finding a few
> problems, so I decided to check with you.
>
> I have done exercise 5.49 by using an ArrayList as it seemed the best
> solution here, I also done exercise 5.51 and both seem to work OK. The
> problem I am having is with the erase() and how to finish the program.
> At the moment when the first ball gets to 550 pixels the program
> execution will stop I would like to stop it when the last ball reaches
> 550 pixels.
>
> I can see that I can do something like Iterate through the
> ArrayList and
> check if all the balls have reached 550 pixels and then finish, but
> that
> seems to me as using to many resources...
>
> Any thoughts welcomed.
>
> Thanks for all the help.
>
> Best regards
>
> Luis
>
>
>
>
>
>
>
>
> _______________________________________________
> mailing list bluej-discuss at bluej.org
> To unsubscribe or change your preferences, go to
> http://lists.bluej.org/mailman/listinfo/bluej-discuss
_______________________________________________
mailing list bluej-discuss at bluej.org
To unsubscribe or change your preferences, go to
http://lists.bluej.org/mailman/listinfo/bluej-discuss
_______________________________________________________________________
"Es mejor estar callado y parecer tonto que hablar y despejar las dudas definitivamente"
---------------------------------
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060308/f8bd33bd/attachment.html
More information about the bluej-discuss
mailing list