Question
Moth.java file /** A class that simulates a moth flying. */ public class Moth { private double position; /** Initializes the moth given a position.
Moth.java file
/**
A class that simulates a moth flying.
*/
public class Moth
{
private double position;
/**
Initializes the moth given a position.
@param initialPosition the initial position
*/
public Moth(double initialPosition)
{
//-----------Start below here. To do: approximate lines of code = 1
// set the position to the given parameter initialPosition
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
/**
Moves the moth halfway to the lightPosition.
*/
public void moveToLight(double lightPosition)
{
//-----------Start below here. To do: approximate lines of code = 1
// update the current position to be halfway to (i.e. closer so position should be getting smaller) the lightPosition
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
/**
Reports the current position of the moth.
@return the current position
*/
public double getPosition()
{
return position;
}
public static void main(String[] args)
{
Moth myMoth = new Moth(10);
myMoth.moveToLight(20);
System.out.println("Expected position 15.0; Current position " + myMoth.getPosition());
myMoth.moveToLight(0);
System.out.println("Expected position 7.5; Current position " + myMoth.getPosition());
myMoth.moveToLight(0);
System.out.println("Expected position 3.75; Current position " + myMoth.getPosition());
myMoth.moveToLight(0);
System.out.println("Expected position 1.875: Current position " + myMoth.getPosition());
}
}
Please include screenshot of the output that shows all operates well
Step by Step Solution
3.44 Rating (125 Votes )
There are 3 Steps involved in it
Step: 1
Mothjava A class thatsimulates a moth flying public class Moth private double position Initializes t...Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started