Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program to solve the following problem. An elastic rope starts out

ID: 3633518 • Letter: W

Question


Write a Java program to solve the following problem.
An elastic rope starts out with length 100 meters. The start of the rope is fixed to a pole and a worm is placed on the rope at the start and the worm starts crawling towards the end. Each day the worm crawls 6 meters along the rope. Each day the rope is stretched by 100 meters. After how many days does the worm reach the end of the rope?
Sample Output:
Day = 0 Million. Distance from end = 0 Million meters.
Day = 1 Million. Distance from end = 19 Million meters.
Day = 2 Million. Distance from end = 30 Million meters.
Day = 3 Million. Distance from end = 39 Million meters.
Day = 4 Million. Distance from end = 45 Million meters.
Day = 5 Million. Distance from end = 49 Million meters.
……

When the worm crawls 6 meters per day:
the worm reaches the end of the rope on day = 26415221
_____________________________________________________________________________

Note: Since the value of the number of days could grow larger than the maximum value an int variable can represent, you need to use a variable of long type instead of int type.
Below is the partial println statement that produced the sample output. Here the double variable distanceFromEnd must be casted into a long type in order to carry out the integer division.
System.out.println( "Distance from end = " +
((long)distanceFromEnd / 1000000) + " Million meters.");

more explanation of this program
On day 0, the rope is 100 meters and the worm is at the beginning of the rope.

On day 1, the rope is stretched to 200 meters, and the worm remains at the beginning of the rope. Then, the worm crawls 6 meters toward the end, so the new position of the worm is 6 meter from the beginning of the 200-meter rope at the end of the day.

On day 2, the rope is stretched to 300 meters and the worm is carried along with the stretching. As the position of the worm remains proportional to the rope length, the worm is now at 6/200 * 300 = 9 meters from the beginning. Given the daily crawl speed, the new position of the worm is 9 + 6 = 15 meters from the beginning of the 300-meter rope at the end of the day.

You need to verify with a program if the worm will eventually reach to the end of the elastic rope. The output will include some intermediate results that contain the whole day in millions and an approximate distance to the end in million meters.

Explanation / Answer

public class Worm { public static void main(String[] arg) { long day, distance, length, distancefrend; day = 1; length = 100; distance = 0; distancefrend = 0; while (distance
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote