# Spaceship Distance Calculation in Java Homework Sample

Assume we have a spaceship that can double it’s speed each day, and by the end of day 1 it has a velocity of 1000 miles per day. You can avoid any relativistic calculations, so you don’t need to check if it gets near the speed of light. After 4 days it will be traveling at 8000 miles per day, and so for a given number of days how fast is the ship traveling and how far would it have traveled. For help with your Java homework assignment contact us for a quote.

Solution:

//check if the input is 0, or a negative, because the space craft would not have traveled

if (daysInput <= 0)

{

distance = 0;

speed = 0;

}

//it was not a 0 or negative number

else

{

speed = 1000; // end of day 1

distance = 500; // distance at end of day 1

//begin a loop, that starts at 1 and is less than days entered

for (int i = 1; i < daysInput; i++)

{

//the space craft doubles it’s speed every day

speed *= 2;

//distance is calculated with speed * time.

//divide by 2 to get the average

distance = distance + 3*speed/4;

}

}

3*speed/4 is equivalent to ((daystartspeed)+ (dayendspeed))/2 . it has to be added to previous days distance.