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

2012/2/14

Help with Letters

1
2
3
davmac davmac

2012/2/28

#
Yep. You should probably assign a letter to each asteroid as it's created. Add a variable to the asteroid class (either a 'char' or 'String') to hold the letter which that Asteroid represents. Add a parameter to the asteroid constructor, of the same type, so that the letter for an asteroid can be specified when the asteroid is created. Then you can create a whole series of asteroids such as: addObject(new Asteroid("i"), 100, 100); addObject(new Asteroid("c"), 100, 100); addObject(new Asteroid("o"), 100, 100); addObject(new Asteroid("n"), 100, 100); addObject(new Asteroid("o"), 100, 100); // etc Of course you could do that programmatically by writing code to iterate through the characters in the string "iconoclast" and create an asteroid for each.
theDoctor theDoctor

2012/3/1

#
Ok, I'm not quite sure how to write an array for something such as this...
davmac davmac

2012/3/1

#
What exactly do you mean by "write an array for"? What would be the purpose of the array - what do you want it to contain? (Why do you want an array anwyay?)
theDoctor theDoctor

2012/3/2

#
Ok, never mind. I thought above you were telling me how to write an array for these, but again, I'm new to programming. How exactly would I add a variable of type String to the Asteroid class? And what is a parameter? I know what the constructor is. And would I add this: addObject(new Asteroid("i"), 100, 100); addObject(new Asteroid("c"), 100, 100); addObject(new Asteroid("o"), 100, 100); addObject(new Asteroid("n"), 100, 100); addObject(new Asteroid("o"), 100, 100); Into my Space class?
Duta Duta

2012/3/2

#
theDoctor, you could replace that code with:
String word = "icono";
for(int i = 0; i < word.length(); i++)
{
    addObject(new Asteroid(""+word.charAt(i)), 100, 100);
}
davmac davmac

2012/3/2

#
How exactly would I add a variable of type String to the Asteroid class? And what is a parameter?
I really think you should go through the tutorials! Also be aware that in the code above '100, 100' was just an example - you'd probably want different co-ordinates for each asteroid, correct? You could probably choose random numbers.
theDoctor theDoctor

2012/3/5

#
That I could! Thanks davmac. You've really helped.
theDoctor theDoctor

2012/3/5

#
And would I add this to the Space class or the Asteroid class for my program? String word = "icono"; for(int i = 0; i < word.length(); i++) { addObject(new Asteroid(""+word.charAt(i)), 100, 100); }
davmac davmac

2012/3/5

#
You want to add the asteroids when the world (Space) is created, correct? So you need to do this in the Space class (in the constructor, or a method that you call from the constructor).
theDoctor theDoctor

2012/3/6

#
Ok! Thanks.
theDoctor theDoctor

2012/3/6

#
When I put the code into the constructor of the Space class, it comes up with this error: "cannot find symbol - constructor Asteroid(java.land.String)". Any suggestions on what I should do here? I'm assuming it means I haven't delcared the symbol for the Asteroid in the Space class.
davmac davmac

2012/3/6

#
It means that your Asteroid class doesn't have a constructor which takes a String parameter: From my post above: Add a parameter to the asteroid constructor, of the same type, so that the letter for an asteroid can be specified when the asteroid is created. Seems like you didn't do that? public Asteroid(String s) { ... }
theDoctor theDoctor

2012/3/6

#
I did this, and it gave me this error: "invalid method declaration; return type required". So the return type cannot be (String s).
davmac davmac

2012/3/6

#
It's a constructor, it doesn't have a return type. Did you put this in the Asteroid class? A constructor for Asteroid instances must go in the Asteroid class.
theDoctor theDoctor

2012/3/8

#
Ok I have the constructor in there, how do I make it take a String as a parameter?
There are more replies on the next page.
1
2
3