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 calculated as follows: Distance = Speed x Tim

ID: 3767429 • Letter: T

Question

The distance a vehicle travels can calculated as follows: Distance = Speed x Time. For example, if a train travels 40 miles per hour for three hours, the distance traveled is 120 miles.
Design a program that asks the user for the speed of vehicle (in miles per hour) and how many hours it has traveled. It should then use a loop display the distance the vehicle has traveled for each hour of that time period. You should validate your entry for negative values (if the user will enter a negative value, the program should output an error message and keep asking for a correct value) After making sure that your code works perfectly, insert your code within a menu driven code that will allow you to choose to run the code again and again until you chose to exit.
Here is an example of the output:
Welcome to Distance Calculation Code Company.
Please select from the following:
1- Run the code 2- Exit
What do you want to do? 1 [enter]
What is the speed of the vehicle in MPH? 40 [enter]
How many hours has the vehicle traveled? 3 [enter]
Hour                           Distance traveled ______________________________
1 40 2 80 3 120
Welcome to Distance Calculation Code Company.
Please select from the following:
1- Run the code 2- Exit
What do you want to do? -1 [enter]
Invalid entry, please try again.
Welcome to Distance Calculation Code Company.
Please select from the following:
1- Run the code 2- Exit
What do you want to do? 2 [enter]
Thank you. Good bye.
Use methods (functions) this time. A minimum of these four functions:
menu     will display the main menu. Takes no parameters and does not return back anything.
validate    will validate the user’s entry for speed and time. Will take one integer value and returns true if the value is greater than zero, false otherwise.
CalculateD      will calculate the distance. Takes two integer values (time, and speed) and returns the calculated distance
getVal        will return a positive value from where it was called. Takes no parameter, and returns back an integer value.

"Use "import java.util.Scanner;""

Explanation / Answer

import java.util.Scanner;

public class DistanceCalculation {

  

private static Scanner sc;

   static void menu() {

       System.out.println();

       System.out.println("Welcome to Distance Calculation Code Company. ");

       System.out.println("Please select from the following:");

       System.out.println("1- Run the code 2- Exit");

       System.out.print("What do you want to do? ");

   }

  

   static int CalculateD(int speed, int time) {

       return speed*time;

   }

  

   static boolean validate(int n) {

       return (n > 0 ? true : false);

   }

  

   static int getVal() {

       int in = sc.nextInt();

       if (validate(in)) {

           return in;

       }

      

       System.out.println("Invalid entry, please try again.");

       return getVal();

   }

  

   public static void main(String[] args) {

       sc = new Scanner(System.in);

      

      

       while (true) {

           menu();

           int in = sc.nextInt();

          

           if (in == 1) {

               System.out.print("What is the speed of the vehicle in MPH? ");

               int speed = getVal();

              

              

               System.out.print("How many hours has the vehicle traveled? ");

               int hours = getVal();

              

               System.out.println("Hour Distance traveled ____________________________________ ");

               for (int i=1; i<=hours; i++) {

                   System.out.println(i+" "+CalculateD(speed, i));

               }

           } else if (in == 2) {

               System.out.println("Thank you. Good bye. ");

               break;

           } else {

               System.out.println("Invalid entry, please try again. ");

           }

       }

   }

}

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