help needed with solving a problem in my constructor,please!
good day all, i have a problem with one of my constructors, not sure what to do exactly,please could someone help me!
thanks
here it is:
Code:
public dropTargetsAbove(Target aboveThis, Airspace inWorld)
{
int myX=aboveThis.getX();
int myY=aboveThis.getY();
List<Target> targets = inWorld.getObjects(Target.class);
for (Target t : targets)
{
if ( (t.getX() == myX) && (t.getY()<myY) )
t.setLocation(t.getX(), t.getY()+30);
}
and this is the error message : invalid method declaration; return type required
Re: help needed with solving a problem in my constructor,please!
by the way i am using greenfoot*
Re: help needed with solving a problem in my constructor,please!
What do you find confusing? The error message clearly says your method needs to declare a return type, which currently it does not.
Re: help needed with solving a problem in my constructor,please!
This is one of my first times creating a constructor so the thing is that i am not sure what to return exactly, if i should return "target" or ....?
Re: help needed with solving a problem in my constructor,please!
Quote:
Originally Posted by
Bentino
This is one of my first times creating a constructor so the thing is that i am not sure what to return exactly, if i should return "target" or ....?
The compiler sees this as being a method declaration:
Code:
public dropTargetsAbove(Target aboveThis, Airspace inWorld) {
//....
}
So why isn't it seeing this as a constructor? One possibility is that the name of your constructor above doesn't match the class name exactly, including spelling *and* capitalization. You will want to check this and correct this if so.
Regardless please get back to us to let us know what happens.
Re: help needed with solving a problem in my constructor,please!
oh alright,that makes sense, maybe i should of shown you the whole class, here it is:
Code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class Airspace here.
*
* @author Benjamin Mpungu
* @version 3.16.2012
*/
public class Airspace extends World
{
private ScoreBox scoreDisplay;
private Bomber bomber;
private int Target;
/**
* Constructor for objects of class Airspace.
*
*/
public Airspace()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
bomber = 2;
int nBombs = 30;
int speed = 2;
bomber = new Bomber();
addObject(Bomber, 40, 40);
// The added targets in the Airspace()
for (int x=75; x<=525; x+=30)
for(int y=250; y<=world.getHeight()-30; y+=30)
world.addObject(new Target((y-220)/30*100), x, y);
// New Scorbox is created with 30 bombs and is added to the world (Airspace)
ScoreBox = new ScoreBox(30);
addObject(ScoreBox,(Airspace.getWidth()/2),(4 * (Airspace.getHeight()/10)) );
}
// This method returns the ScoreBox created in the Airspace()
public void getScoreBox()
{
return ScoreBox;
}
// This method returns the Bomber created in the Airspace()
{
return Bomber;
}
// This method will ensure if there are or if there aren't any bombs dropping from the bomber
public boolean activeBombs()
{
int numActiveBombs;
if (numActiveBombs(inWorld)!=0)
return true;
else
return false;
}
// This method is used to drop the targets
public dropTargetsAbove(Target aboveThis, Airspace inWorld)
{
int myX=aboveThis.getX();
int myY=aboveThis.getY();
List<Target> targets = inWorld.getObjects(Target.class);
for (Target t : targets)
{
if ( (t.getX() == myX) && (t.getY()<myY) )
t.setLocation(t.getX(), t.getY()+30);
}
}
Re: help needed with solving a problem in my constructor,please!
dropTargetsAbove is obviously not a constructor and not supposed to be a constructor (I'm not sure why you said that you were creating one?). The answer has already been given to you by ojn: It's a method, and all methods have to have return types as has already been mentioned. Check the Java tutorials method section for this and for more on writing methods.
The Java Tutorials: The Really Big Index