Implement a class Moth that models a moth flying along a straight line. The moth has a position which is 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.
public Moth(double initialPosition)
public void moveToLight(double lightPosition)
public double getPosition()
All Class are Implemented in Java and executed
import java.io.*;
import java.util.*;
class Moth
{
private double position;
public Moth(double initialPostion)
{
position = initialPostion;
}
public void moveToLight(double lightPosition)
{
position = (position + lightPosition);
}
public double getPosition()
{
return position;
}
}
public class MothTester
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Initial Position");
int n = sc.nextInt();
System.out.println("Enter New Position");
float b = sc.nextFloat();
Moth moth = new Moth(n);
System.out.println("Initial Position of the Moth =" +moth.getPosition());
moth.moveToLight(b);
System.out.println("New Position of Moth =" +moth.getPosition());
}
}
Get Answers For Free
Most questions answered within 1 hours.