Ok I did what you said, and I got this error: "cannot find symbol - constructor Asteroid()"
In this section of code:
/**
* Add a given number of asteroids to our world. Asteroids are only added into
* the left half of the world.
*/
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
I've tried adding the parameters "(String s)" into the parentheses after "Asteroid", but it still didn't compile, and came up with this error: ')' expected.
When you call the constructor, you need to provide parameter values, not the parameter types and names (which are provided in the definition).
You are now ignoring the code that Duta provided before:
String word = "icono";
for(int i = 0; i < word.length(); i++)
{
addObject(new Asteroid(""+word.charAt(i)), 100, 100);
}
This shows the correct syntax, though you should replace '100,100' with 'x,y' and add in your lines to choose a random value for both x and y.
Do I put this in my addAsteroids method above or just in the Space constructor? I've implemeted the code into the Space constructor, but it's coming up with the same error for my addAsteroids method. Sorry I'm being such a hassle with this guys. This is the first really big (big for me, at least) programming project I've taken on.
This code adds asteroids, so, the logical place for it is in the addAsteroids method.
One issue is that your addAsteroids method, as you wrote it, takes an int parameter to specify the number of asteroids. This doesn't make sense if the asteroids are supposed to have letters associated with them. Perhaps passing in the value for the word variable (a String) would make more sense?
No problem about 'being a hassle', but just remember to think about what your code is doing and what you want it to do. It seems a lot like you don't understand some fundamental concepts such as how to write methods and constructors, and how to call them. It would probably be beneficial for you to go through the tutorials and watch the videos.
I did what you said with the putting (a String) in place of (int count), but it came up with this error: "cannot find symbol - class a".
Instead of "a String" use "String letter"
Remember to have your variables in the format <Type> <Name> where type is either a primitive data type (int/char/double/float/short/long and so on) or a class (in this case String, which is a class you can access in java without an import statement due to it being in the java.lang package.
Exactly. When I said "a String" I meant the parameter would be a String parameter, i.e. it would be of type String - I didn't meant that you should type "a String" literally in place of "int count". And as Duta says the type comes before the variable/parameter name, which, I was trying to say, could be called "word" (to replace the variable currently in the code).
I'll say it one more time: It would probably be beneficial for you to go through the tutorials and watch the videos. Programming requires understanding what the code that you enter means; you can't get very far by just blindly typing stuff in.
I did what Duta said, and it came up with this compilation error: "addAsteroidsUsingWhile(java.lang.String) in Space cannot be applied to int". I'm assuming it has to do with the "for(int i = 0; i < word.length(); i++)" section of the "addAsteroidsUsingWhile" method, but I'm not sure. Davmac, I would watch the tutorials, but I don't have time. I'm in a high school BEGINNING programming class, and honestly, no one is doing much better than I am. I've got a lot on my plate right now, not just programming. But thank you for your time with helping me.
I did what Duta said, and it came up with this compilation error: "addAsteroidsUsingWhile(java.lang.String) in Space cannot be applied to int". I'm assuming it has to do with the "for(int i = 0; i < word.length(); i++)" section of the "addAsteroidsUsingWhile" method, but I'm not sure.
It sounds like you're trying to call the 'addAsteroidsUsingWhile(...)' method, which requires a parameter of type 'String', but you are instead supplying a parameter of type 'int'.
Davmac, I would watch the tutorials, but I don't have time
Well, it's up to you, but it seems to me you're spending a lot of time trying to get this to work when if you just spent a little time actually learning the principles you'd be much better off. If you're in a beginning programming class, surely it is covering concepts such as calling and defining methods?