Implement a class Moth that models a moth flying across a straight line. The moth has a position, the distance from a fixed origin. When the moth moves toward a point of light, its new position is halfway between its old position and the position of the light source. Supply a conductor
public Moth(double initialPosition)
and methods
public void moveToLight(double lightPosition)
public double getPosition
public class Test
{
publicstatic void main(String[] args)
{
Mothmoth1=new Moth(1.0);
Mothmoth2=new Moth(3.0);
doublelightPos=2.0;
moth1.moveToLight(lightPos);
moth2.moveToLight(lightPos);
System.out.println("moth1"+moth1.getPosition());
System.out.println("moth2"+moth2.getPosition());
}
}
public class Moth
{
privatedouble position;
publicMoth(double initialPosition)
{
this.position= initialPosition;
}
publicvoid moveToLight(double lightPosition)
{
position=(lightPosition+position)/2;
}
publicdouble getPosition()
{
returnposition;
}
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
JavaJSPJSF