This site requires JavaScript, please enable it in your browser!
Greenfoot back
rc5pn
rc5pn wrote ...

2016/7/27

Bubbles Lab

rc5pn rc5pn

2016/7/27

#
Having similar issue as referenced in http://www.greenfoot.org/topics/56255/0#post_107931, however instead of using a for loop, we are instructed to use a while loop. In the second loop, consisting of ten bubbles, the size of the bubbles should increase, starting with 10 for the first bubble, then 20, then 30, etc. Use the second Bubble constructor for this ( the one with one parameter). My code in the Space class is as follows: public void setup() { Bubble bubble = new Bubble; int i = 0; while (i < bubble.length) { bubble = new Bubble(); addObject(bubble, i*45, i*30); i++; } int x = 0; while ( x < 10 ) { addObject(new Bubble(),300 + x *40, 100); x = x + 1; } I think I have an understanding of how to write the code- it follows the same logic as the while loop used for the 10 in the Space class but I am unclear on where I should implement this within the second constructor for the Bubble class.
danpost danpost

2016/7/27

#
To view your code properly and with line numbers, I am posting it again, here, with code tags:
rc5pn wrote...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void setup()
{
    Bubble[] bubble = new Bubble[21];
    int i = 0;
 
    while (i < bubble.length)
    {
        bubble[i] = new Bubble();
        addObject(bubble[i], i*45, i*30);
        i++;
    }
    int x = 0;
 
    while ( x < 10 )                  
    {
        addObject(new Bubble(),300 + x *40, 100);
        x = x + 1;                
    }
}
A following post will discuss this code.
danpost danpost

2016/7/28

#
Line 3 creates an array for up to 21 Bubble objects, which is filled and those bubbles placed into the world in a diagonal line from (0, 0) to (900, 600) by lines 4 through 11. All these bubbles will be the same size due to the use of the default Bubble constructor. Then, lines 12 through 19 will add another 10 bubbles into the world in a horizontal line from (300, 100) to (660, 100). Again, because the default constructor is used (no parameter is given in the constructor call), these will also be the same size as the others. If you want different size (or increasing size) bubbles to be made, you must use the other Bubble constructor (the one that requires an 'int' value for its parameter).
rc5pn rc5pn

2016/7/28

#
yes, I understand i need to use the second bubble constructor in the Bubble class. But I guess I am a little confused on where I should put the following code to change the size of the bubbles by ten; in the Bubble constructor in the Bubble class? i = 0; while ( i < 10 ) { addObject(new Bubble(10 + i * 10),300 + i * 40,100); i = i + 1; }
danpost danpost

2016/7/28

#
rc5pn wrote...
yes, I understand i need to use the second bubble constructor in the Bubble class. But I guess I am a little confused on where I should put the following code to change the size of the bubbles by ten; in the Bubble constructor in the Bubble class? < Code Omitted >
If you want them to be placed in the world at the start, then you would want it in your world class constructor; or, if the 'setup' method is called from that constructor, you could place it in that method.
You need to login to post a reply.