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

On your mark. Get Set. Go [20 Points] Write a program that asks for the names of

ID: 3674187 • Letter: O

Question

On your mark. Get Set. Go [20 Points]

Write a program that asks for the names of three runners and the time it took each of them to finish a race.

The program should display who came in first, second, and third place.

Assume time is different for each one of them, so there will not be any tie.

Input Validation: Only accept positive numbers for the times. INPUT FORMAT (values are example):

Enter name of runner 1: Usain Bolt Time taken by runner 1: 9.58

Enter name of runner 2: Asafa Powell Time taken by runner 2: 10.10

Enter name of runner 3: Tyson Gay Time taken by runner 3: 9.69

OUTPUT FORMAT: First Place: Usain Bolt Second Place: Tyson Gay Third Place: Asafa Powell

Explanation / Answer

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class RunningRace {

   /**
   * @param args
   */
   public static void main(String[] args) {

       try {

           String runner[] = new String[3];
           double timeTaken[] = new double[3];
           Scanner scanner = new Scanner(System.in);

           System.out.print("Enter name of runner 1 :");
           runner[0] = scanner.next();
           System.out.print("Time taken by runner 1 :");
           timeTaken[0] = validInput(scanner);

           System.out.print("Enter name of runner 2 :");
           runner[1] = scanner.next();
           System.out.print("Time taken by runner 2 :");
           timeTaken[1] = validInput(scanner);

           System.out.print("Enter name of runner 3 :");
           runner[2] = scanner.next();
           System.out.print("Time taken by runner 3 :");
           timeTaken[2] = validInput(scanner);

           // sorting

           for (int i = 0; i < timeTaken.length - 1; i++) {
               for (int j = 0; j < timeTaken.length - 1 - i; j++) {
                   if (timeTaken[j] < timeTaken[j + 1]) {

                       double timeTemp = timeTaken[j];
                       timeTaken[j] = timeTaken[j + 1];
                       timeTaken[j + 1] = timeTemp;

                       String nameTemp = runner[j];
                       runner[j] = runner[j + 1];
                       runner[j + 1] = nameTemp;

                   }

               }
           }

           System.out.println("First Place :" + runner[0]);
           System.out.println("Second Place :" + runner[1]);
           System.out.println("Third Place :" + runner[2]);

           scanner.close();
       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }

   }

   /**
   * validate time with out enetering negative
   *
   * @param scanner
   * @return
   */
   public static double validInput(Scanner scanner) {
       double time = 0.0d;
       do {
           time = scanner.nextDouble();
           if (time > 0.0d)
               break;
           else
               System.out.println("Time should be positive:");

       } while (true);

       return time;
   }

}

OUTPUT:

Enter name of runner 1 :Rajesh
Time taken by runner 1 :9.58
Enter name of runner 2 :Pavan
Time taken by runner 2 :10.10
Enter name of runner 3 :Rajesh
Time taken by runner 3 :9.69

First Place :Pavan
Second Place :Rajesh
Third Place :Rajesh

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