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

The distance a vehicle travels can be calculated as follows: Distance = Speed *

ID: 3806495 • Letter: T

Question

The distance a vehicle travels can be calculated as follows: Distance = Speed * Time For example, if a train travels 40 miles-per-hour for three hours, the distance traveled is 120 miles. Write a program that asks for the speed of a vehicle (in miles-per-hour) and the number of hours it has traveled. It should use a loop to display the distance a vehicle has traveled for each hour of a time period specified by the user. For example, if a vehicle is traveling at 40 mph for a three-hour time period, it should display a report similar to the one that follows: Do not accept a negative number for speed and do not accept any value less than 1 for time traveled. The distance a vehicle travels can be calculated as follows: Distance = Speed * Time Write a method named distance that accepts a vehicle's speed and time as arguments, and returns the distance the vehicle has traveled. Modify the "Distance Traveled" program you wrote in Chapter 4 (Programming Challenge 2) to use the method.

Explanation / Answer

Hello, as you didnt mentioned any of the programming language so here i am using JAVA

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

class CalculateDistance
{
   public static void main (String[] args) throws java.lang.Exception
   {
  
       System.out.println("distance calculated: "+ distance(120,3));
   }
  
   public static float distance(int speed, int time){
       return speed*time;
   }
}

here i have make a function named as distance taking 2 arguments as speed and time whose data type is integer, the function which returns a float distance.

and in the main function i have just called that function which is calculating distance and printing the value of distance by using System.out.println();

this is whole thing that is described in the image that is attached above in this question.

we can write the seperate function as

float distance(int speed, int time){
       return speed*time;
   }

just call this function anywhere in your main class.

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