Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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... blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elementary Statistics

Authors: Robert R. Johnson, Patricia J. Kuby

11th Edition

978-053873350, 9781133169321, 538733500, 1133169325, 978-0538733502

More Books

Students also viewed these Programming questions

Question

Equipment alignment and calibration

Answered: 1 week ago

Question

Test procedures

Answered: 1 week ago

Question

What are some common SITE challenges and issues?

Answered: 1 week ago