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 constructor and methods

moveToLight
getPosition

Complete the following file:

Moth.java

public class Moth {
. . . // instance variable
public . . . // constructor
{
. . .
}
public . . . moveToLight(. . .)
{
. . .
}
public . . . getPosition(. . .)
{
. . .
} }

Use the following files:

MothTester.java

public class MothTester
{
   public static void main(String[] args)
   {
      Moth gypsy = new Moth(10);
      gypsy.moveToLight(0);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 5");
      gypsy.moveToLight(10);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 7.5");
      gypsy.moveToLight(0);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 3.75");
   }
}

MothTester2.java

public class MothTester2
{
   public static void main(String[] args)
   {
      Moth gypsy = new Moth(20);
      gypsy.moveToLight(10);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 15.0");
      gypsy.moveToLight(10);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 12.5");
      gypsy.moveToLight(-10);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 1.25");
      gypsy.moveToLight(0);
      System.out.println(gypsy.getPosition());
      System.out.println("Expected: 0.625");
   }
}