Results 1 to 7 of 7
- 03-22-2012, 10:57 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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:
and this is the error message : invalid method declaration; return type requiredJava 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); }
- 03-22-2012, 10:58 PM #2
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
Re: help needed with solving a problem in my constructor,please!
by the way i am using greenfoot*
- 03-22-2012, 11:56 PM #3
Senior Member
- Join Date
- Aug 2008
- Location
- Stockholm, Sweden
- Posts
- 119
- Rep Power
- 0
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.
- 03-23-2012, 12:02 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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!
The compiler sees this as being a method declaration:
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.Java Code:public dropTargetsAbove(Target aboveThis, Airspace inWorld) { //.... }
Regardless please get back to us to let us know what happens.
- 03-23-2012, 12:12 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 17
- Rep Power
- 0
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:
Java 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
Similar Threads
-
Solving a SALARY and TAX.... HAVE A PROBLEM ;(
By irnie1994 in forum JCreatorReplies: 5Last Post: 01-10-2012, 12:51 PM -
Path solving algorithm/method help needed!
By Zee Best in forum Advanced JavaReplies: 3Last Post: 10-18-2011, 01:32 AM -
Help Needed in Solving the Following Isuue.
By raju.i in forum Advanced JavaReplies: 3Last Post: 05-14-2010, 05:01 PM -
I need help solving this problem.
By Felicia in forum New To JavaReplies: 5Last Post: 03-04-2010, 08:40 PM -
Solving this equations problem in Java
By matt_well in forum New To JavaReplies: 17Last Post: 08-30-2008, 09:05 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks