KnowledgeBoat Logo

Computer Applications

The driver took a drive to a town 240 km at a speed of 60 km/h. Later in the evening, he drove back at 20 km/h less than the usual speed. Write a program to calculate:

  1. the total time taken by the driver
  2. the average speed during the whole journey

[Hint: average speed = total distance / total time]

Java

Input in Java

53 Likes

Answer

public class KboatJourney
{
    public static void main(String args[]) {
        
        int distance = 240;
        float speed = 60.0f;
        float returnSpeed = speed - 20;
        
        float time2Reach = distance / speed;
        float time2Return = distance / returnSpeed;
        float totalTime = time2Reach + time2Return;
        
        float avgSpeed = (distance * 2) / totalTime;
        
        System.out.println("Total time: " + totalTime);
        System.out.println("Average speed: " + avgSpeed);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of The driver took a drive to a town 240 km at a speed of 60 km/h. Later in the evening, he drove back at 20 km/h less than the usual speed. Write a program to calculate: (a) the total time taken by the driver (b) the average speed during the whole journey [Hint: average speed = total distance / total time]

Answered By

24 Likes


Related Questions