0

So I am having issues with the multiple constructors that I am using. Basically, I have two WaterDrop constructs where I pass in the initial x and y position (second block of code) into the appropriate constructor. The issue is that it doesn't set int x, y instance variables to the appropriate starting positions. It sets them fine the the first constructor but then when it draws the dots, using the second constructor, it automatically sets them to the (0, 0) position. Is there anyway I could call the first constructor so it sets the x and y position to the appropriate starting location?

public class WaterDrop
{
// instance variables - replace the example below with your own
private int x;
private int y;
private int xVelocity;
private int yVelocity;
private DrawingPanel panel;
private static int DIAMETER = 1;
private int delayStart;
private int bounceLimit;

private static Random rand = new Random();

public WaterDrop(int x, int y){
    this.x = x; //**These assign just fine but I can't get them to get passed**
    this.y = y;  //**into the next WaterDrop constructor**
                 //**i.e. (200, 400)**

}

public WaterDrop(DrawingPanel panel, boolean move)
{
    //initialise instance variables //**In this constructor they are still**
                                    //**initialized to 0**
    this.panel = panel;  //**Since they are initialized at 0, when I draw the**
                         //**waterDrops they appear at the location (0, 0)**

}

    xVelocity = rand.nextInt(3) - 1;
    yVelocity = rand.nextInt(20) + 1 ;
    delayStart = rand.nextInt(100);
    bounceLimit = 0;

}

This is what I'm passing in from the WaterFountain class:

public class WaterFountain{
....

public WaterFountain(DrawingPanel panel, int xLocation, int yLocation)
{
    this.panel = panel;
    this.xLocation = xLocation;
    this.yLocation = yLocation;

    for(int i = 0; i < NUM_WATER_DROPS; i++){
         waterDrop[i] = new WaterDrop(this.xLocation, this.yLocation);
    }
}


....
}
  • How would you know what the *"appropriate starting location"* is when you never give a location in `WaterDrop(DrawingPanel panel, boolean move)`? – Spencer Wieczorek Nov 28 '17 at 03:45
  • I'm confused by this also. I'm in school and the professor said "This starting location should be set by the object creating the WaterDrop object(s). Which means a new constructor, one that allows setting the x and y location, is needed." –  Nov 28 '17 at 03:53
  • I mean you are not proving a location in your constructor, so it's not possible to set the location if you don't change your constructors parameters. As such you should expect it to be the default values, you will want to add parameters for the location. [You can call constructor in a constructor](https://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java), but I would not do it unless there is a good reason to. – Spencer Wieczorek Nov 28 '17 at 03:58
  • Okay, I got it to work doing this. Thanks for the help! –  Nov 28 '17 at 04:01

2 Answers2

1

Your second constructor can't just magically "know" the appropriate values for x and y. You have to give it the appropriate values. And the only way to do that is to add int x and int y arguments to it. Then, you can set the x and y instance variables either by invoking the first constructor:

public WaterDrop(int x, int y, DrawingPanel panel, boolean move)
{
    this(x, y);  // invoke the WaterDrop(int, int) ctor
    this.panel = panel;  
}

or just set x and y directly:

public WaterDrop(int x, int y, DrawingPanel panel, boolean move)
{
    this.x;
    this.y;
    this.panel = panel;  
}
Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21
0

"when it draws the dots, using the second constructor, it automatically sets them to the (0, 0) position" that's because everytime a constructor is initialized a new object is created.

Rishabh Maurya
  • 1,448
  • 3
  • 22
  • 40