Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By ojn

Thread: help needed with solving a problem in my constructor,please!

  1. #1
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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:
    Java 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

  2. #2
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: help needed with solving a problem in my constructor,please!

    by the way i am using greenfoot*

  3. #3
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default 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.
    Fubarable likes this.

  4. #4
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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 ....?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: help needed with solving a problem in my constructor,please!

    Quote Originally Posted by Bentino View Post
    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:

    Java 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.

  6. #6
    Bentino is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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);
            }
            
        }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Solving a SALARY and TAX.... HAVE A PROBLEM ;(
    By irnie1994 in forum JCreator
    Replies: 5
    Last Post: 01-10-2012, 01:51 PM
  2. Path solving algorithm/method help needed!
    By Zee Best in forum Advanced Java
    Replies: 3
    Last Post: 10-18-2011, 02:32 AM
  3. Help Needed in Solving the Following Isuue.
    By raju.i in forum Advanced Java
    Replies: 3
    Last Post: 05-14-2010, 06:01 PM
  4. I need help solving this problem.
    By Felicia in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 09:40 PM
  5. Solving this equations problem in Java
    By matt_well in forum New To Java
    Replies: 17
    Last Post: 08-30-2008, 10:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •