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

C In many races competitors g.com ve up to 90.. The W3C Markup Val... Dashboard

ID: 3707859 • Letter: C

Question

C In many races competitors g.com ve up to 90.. The W3C Markup Val... Dashboard Page How to Cite a Website... G Google URL Shortener Course Hame Study EXPERT Q&A; Cheg TEXTBOOK SOLUTIONS g solutions manuals/ java /7th edition / chapter 10/ problem 10p home / study /engineering /computer science/programming /programming solutions Java (7th Edition) Chapter 10, Problem 10PROP C] ?RBoomar Show all steps Problem In many races competitors wear a RFID tag on their shoes or bibs, When the racer crosses a sensor a computer logs the racer's number along with the current time sensors can be placed along the course to accurately calculate the racers finish time or pace and also to venity that the racer crossed key checkpoints. Consider such a system in use for a half-marathon running race, which is 13 1 miles. In this problem there are only three sensors at the start, at the 7-mile point, and at the finish line Here is sample data for three racers. The first line is the gun time in the 24-hour time format (HH MM SS). The gun time is when the race begins. Subsequent lines are recorded by sensors and contain the sensor ID (0-start, 1 midpoint, 2-finish) followed by the racer's number followed by the time stamp. The start time may be different than the gun time because sometimes it takes a racer a little while to get to the starting line when there is a large pack 08 00 00 0 100 08 00 00 0 132 08 00 13 0 182 08 00 15 1 100 08 50 46 1182 |08 51 15 1132 08 51 18

Explanation / Answer

CODE

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

public class SensorRecordings
{
   public static void main(String args[])
   {
       boolean isFileOpened;
       String filename = "SensorRecordings.txt";
       Scanner infile = null;
       int arrReadings[][] = new int[10][5];
       int linenumber = 0;
       int colNumber =0;

       /* READ THE LOG DATA INTO APPROPRIATE ARRAY STRUCTURE arrReadings */
       try
       {
           infile = new Scanner(new File(filename));
          
           while (infile.hasNextLine())
           {
               String reading = infile.nextLine(); //read string from infile
               System.out.printf("%s ",reading);
               String[] values = reading.split("\s+");
               arrReadings[linenumber][0] = Integer.parseInt(values[0]);
               arrReadings[linenumber][1] = Integer.parseInt(values[1]);
               arrReadings[linenumber][2] = Integer.parseInt(values[2]);

               if (linenumber == 0)
               {
                   /* line stores start time*/
                   arrReadings[linenumber][3] = -1;
                   arrReadings[linenumber][4] = -1;
               }
               else
               {
                   arrReadings[linenumber][3] = Integer.parseInt(values[3]);
                   arrReadings[linenumber][4] = Integer.parseInt(values[4]);
               }
               linenumber++;
           }

           System.out.println("DISPLAY ARRAY READINGS");
           for (linenumber=0;linenumber<10;linenumber++)
           {
               for(colNumber=0;colNumber<5;colNumber++)
               {  
                   System.out.print(arrReadings[linenumber][colNumber] + " ");
               }

               System.out.println("");
           }


           /*STORES RACER NUMBER, READING 1, READING 2, READING 3, FINISH PLACE, SPLIT TIME 1, SPLIT TIME 2, OVERALL RACE TIME, OVERALL RACE PACE, TOTAL SECONDS*/
          
           String racerInfo[][] = new String[3][10];
          
           /*initialize racer number*/
           racerInfo[0][0] = "100";
           racerInfo[1][0] = "132";
           racerInfo[2][0] = "182";
          

           /*calculate racer total race time*/
           int racerNumberBeenEvaluated = -1;
           int racerNum =-1;
           int readingNumber = -1;
           int readingHour = -1;
           int readingMin = -1;
           int readingSec = -1;

           int racerStartHour = -1;
           int   racerStartMin = -1;
           int   racerStartSec = -1;

           int racerMidHour = -1;
           int   racerMidMin = -1;
           int   racerMidSec = -1;

           int racerEndHour = -1;
           int   racerEndMin = -1;
           int   racerEndSec = -1;
                      

           int racerTotalHourInSeconds = -1;
           int   racerTotalMinInSeconds= -1;
           int racerTotalTimeInSeconds = -1;

           double hours, minutes, seconds, totalSeconds = -1;
           int racercount =-1;
           double racerMilesPerMinutes = -1;
           double racerMilesPerHour = -1;
          
           /* SAMPLE DATA
               0   100   08   00   00
               1   100   08   50   46
               2   100   09   35   10

               0   132   08   00   13
               1   132   08   51   18
               2   132   09   34   16
           */

           for(racercount=0; racercount<3;racercount++)
           {
               racerNumberBeenEvaluated = Integer.parseInt(racerInfo[racercount][0]);
              
               for (linenumber=0;linenumber<10;linenumber++)
               {
                   for(colNumber=0;colNumber<5;colNumber++)
                   {  
                       /*System.out.print(arrReadings[linenumber][colNumber] + " ");*/

                       if (linenumber == 0)
                           continue; /* ignore start time */
                       else
                       {
                           readingNumber = arrReadings[linenumber][0];
                           racerNum = arrReadings[linenumber][1];
                           readingHour = arrReadings[linenumber][2];
                           readingMin = arrReadings[linenumber][3];
                           readingSec = arrReadings[linenumber][4];

                           if (racerNum == racerNumberBeenEvaluated)
                           {
                               /*UPDATE THE READING */
                               String s ="";

                               s= String.valueOf(readingHour);
                               s = s.concat(":");
                               s = s.concat(String.valueOf(readingMin));
                               s = s.concat(":");
                               s = s.concat(String.valueOf(readingSec));

                               racerInfo[racercount][readingNumber+1] = s;
                              
                              

                               if (readingNumber == 0)
                               {
                                   racerStartHour = readingHour;
                                   racerStartMin = readingMin;
                                   racerStartSec = readingSec;

                                  
                                  
                               }
                               else if (readingNumber == 1)
                               {
                                   racerMidHour = readingHour;
                                   racerMidMin = readingMin;
                                   racerMidSec = readingSec;

                                   /*CALCULATE SPLIT 1*/
                                  
                                   racerTotalHourInSeconds = (racerMidHour - racerStartHour) * 60 * 60;
                                   racerTotalMinInSeconds= (racerMidMin-racerStartMin) * 60 ;
                                  
                                   racerTotalTimeInSeconds = racerTotalHourInSeconds + racerTotalMinInSeconds + Math.abs(racerMidSec-racerStartSec);

                                   totalSeconds = racerTotalTimeInSeconds;

                                   hours = racerTotalHourInSeconds /(60*60);
                                   minutes = racerTotalMinInSeconds /60;
                                   seconds = Math.abs((racerMidSec-racerStartSec));

                                   String racermidSplitTime = hours + " hours " + minutes + " minutes " + seconds + " seconds";
                                  
                                   racerMilesPerHour = 7/(totalSeconds/3600);

                                   racerInfo[racercount][5]= String.format("%.2f", racerMilesPerHour ) + " MILES PER HOUR";

                               }
                               else if (readingNumber == 2)
                               {
                                   racerEndHour = readingHour;
                                   racerEndMin = readingMin;
                                   racerEndSec = readingSec;

                                   /*CALCULATE SPLIT 2 TIME*/
                                  
                                   racerTotalHourInSeconds = (racerEndHour - racerMidHour) * 60 * 60;
                                   racerTotalMinInSeconds= (racerEndMin-racerMidMin) * 60 ;
                                  
                                   racerTotalTimeInSeconds = racerTotalHourInSeconds + racerTotalMinInSeconds + Math.abs(racerMidSec-racerStartSec);

                                   totalSeconds = racerTotalTimeInSeconds;
                                   racerInfo[racercount][9]= String.valueOf(totalSeconds);

                                   hours = racerTotalHourInSeconds /(60*60);
                                   minutes = racerTotalMinInSeconds /60;
                                   seconds = Math.abs((racerMidSec-racerStartSec));

                                   String racerendSplitTime = hours + " hours " + minutes + " minutes " + seconds + " seconds";
                                  
                                   racerMilesPerHour = 6.1/(totalSeconds/3600);
                                   racerInfo[racercount][6]= String.format("%.2f", racerMilesPerHour ) + " MILES PER HOUR";

                                  
                                   /*CALCULATE TOTAL TIME*/
                                  
                                   racerTotalHourInSeconds = (racerEndHour - racerStartHour) * 60 * 60;
                                   racerTotalMinInSeconds= (racerEndMin-racerStartMin) * 60 ;
                                   racerTotalTimeInSeconds = racerTotalHourInSeconds + racerTotalMinInSeconds + Math.abs(racerStartSec-racerEndSec);

                                   /*System.out.println("Racer " + racerNum +"::" + racerTotalHourInSeconds + ":"+racerTotalMinInSeconds+":"+Math.abs(racerStartSec-racerEndSec));*/
                                  
                                   totalSeconds = racerTotalTimeInSeconds;
                                      
                                   hours = racerTotalHourInSeconds /(60*60);
                                   minutes = racerTotalMinInSeconds /60;
                                   seconds = Math.abs((racerStartSec-racerEndSec));

                                   String racertotalTime = hours + " hours " + minutes + " minutes " + seconds + " seconds";

                                   racerInfo[racercount][7]=racertotalTime;

                                   /*CALCULATE TOTAL PACE TIME*/
                                   racerMilesPerMinutes = 13.1/(totalSeconds/60);
                                   racerMilesPerHour = 13.1/(totalSeconds/3600);


                                   racerInfo[racercount][8]= String.format("%.2f", racerMilesPerHour ) + " MILES PER HOUR";

                               }
                              
                           }
                       }
                      
                   }
               }
           }

           /* SORTING THE ARRAY TO CALCULATE RANKINGS */
           sortbyColumn(racerInfo, 9);
           int rank = 1;
           for(racercount=0; racercount<3;racercount++)
           {
               racerInfo[racercount][4] = String.valueOf(rank);
               rank++;
           }


           /* READ RACER NUMBER */
           racerNum =-1;
           while(true)
           {
               System.out.print("Please Enter Racer number (-1 to exit): ");
               Scanner in = new Scanner(System.in);
               racerNum = in.nextInt();
              
               if (racerNum == -1)
                   break;

               for(racercount=0; racercount<3;racercount++)
               {
                   if ((racerInfo[racercount][0]).equals(String.valueOf(racerNum)))
                   {
                       /*STORES RACER NUMBER, READING 1, READING 2, READING 3, FINISH PLACE, SPLIT TIME 1, SPLIT TIME 2, OVERALL RACE TIME, OVERALL RACE PACE*/
                       /* PRINT RACER INFORMATION */
                      
                       System.out.println("");
                       System.out.println("--------------------------------------------------------");
                       System.out.println("");
                       System.out.println("RACER NUMBER:                 " + racerInfo[racercount][0]);
                       System.out.println("");
                       System.out.println("READING 1:                    " + racerInfo[racercount][1]);
                       System.out.println("");
                       System.out.println("READING 2:                    " + racerInfo[racercount][2]);
                       System.out.println("");
                       System.out.println("READING 3:                    " + racerInfo[racercount][3]);
                       System.out.println("");
                       System.out.println("FINISH PLACE:                 " + racerInfo[racercount][4]);
                       System.out.println("");
                       System.out.println("SPLIT TIME 1 (MILES/HR):      " + racerInfo[racercount][5]);
                       System.out.println("");
                       System.out.println("SPLIT TIME 2 (MILES/HR):      " + racerInfo[racercount][6]);
                       System.out.println("");
                       System.out.println("OVERALL RACE TIME (MILES/HR): " + racerInfo[racercount][7]);
                       System.out.println("");
                       System.out.println("OVERALL RACE PACE:            " + racerInfo[racercount][8]);
                       System.out.println("");
                       System.out.println("TOTAL SECONDS:                " + racerInfo[racercount][9]);
                       System.out.println("");
                       System.out.println("--------------------------------------------------------");
                       System.out.println("");
                   }
               }              
              
           }

          
       }
       catch(Exception e)
       {
           System.out.print("Exception caught: ");
           e.printStackTrace();
       }

   }

   // Function to sort by column
    public static void sortbyColumn(String arr[][], int col)
    {
        // Using built-in sort function Arrays.sort
        Arrays.sort(arr, new Comparator<String[]>() {
         
          @Override           
          // Compare values according to columns
          public int compare(final String[] entry1,
                             final String[] entry2) {

            // To sort in descending order revert
            // the '>' Operator

            if (Double.parseDouble(entry1[col]) > Double.parseDouble(entry2[col]))
                return 1;
            else
                return -1;
          }
        }); // End of function call sort().
    }
}

SENSORRECORDINGS.TXT

08   00   00
0   100   08   00   00
0   132   08   00   13
0   182   08   00   15
1   100   08   50   46
1   182   08   51   15
1   132   08   51   18
2   132   09   34   16
2   100   09   35   10
2   182   09   45   15


OUTPUT


D:Program FilesJavajdk1.8.0_91in>javac SensorRecordings.java

D:Program FilesJavajdk1.8.0_91in>java SensorRecordings
08      00      00
0       100     08      00      00
0       132     08      00      13
0       182     08      00      15
1       100     08      50      46
1       182     08      51      15
1       132     08      51      18
2       132     09      34      16
2       100     09      35      10
2       182     09      45      15
DISPLAY ARRAY READINGS
8       0       0       -1      -1
0       100     8       0       0
0       132     8       0       13
0       182     8       0       15
1       100     8       50      46
1       182     8       51      15
1       132     8       51      18
2       132     9       34      16
2       100     9       35      10
2       182     9       45      15
Please Enter Racer number (-1 to exit): 100

--------------------------------------------------------

RACER NUMBER:                 100

READING 1:                    8:0:0

READING 2:                    8:50:46

READING 3:                    9:35:10

FINISH PLACE:                 2

SPLIT TIME 1 (MILES/HR):      8.27 MILES PER HOUR

SPLIT TIME 2 (MILES/HR):      8.00 MILES PER HOUR

OVERALL RACE TIME (MILES/HR): 1.0 hours 35.0 minutes 10.0 seconds

OVERALL RACE PACE:            8.26 MILES PER HOUR

TOTAL SECONDS:                2746.0

--------------------------------------------------------

Please Enter Racer number (-1 to exit): 132

--------------------------------------------------------

RACER NUMBER:                 132

READING 1:                    8:0:13

READING 2:                    8:51:18

READING 3:                    9:34:16

FINISH PLACE:                 1

SPLIT TIME 1 (MILES/HR):      8.22 MILES PER HOUR

SPLIT TIME 2 (MILES/HR):      8.50 MILES PER HOUR

OVERALL RACE TIME (MILES/HR): 1.0 hours 34.0 minutes 3.0 seconds

OVERALL RACE PACE:            8.36 MILES PER HOUR

TOTAL SECONDS:                2585.0

--------------------------------------------------------

Please Enter Racer number (-1 to exit): 182

--------------------------------------------------------

RACER NUMBER:                 182

READING 1:                    8:0:15

READING 2:                    8:51:15

READING 3:                    9:45:15

FINISH PLACE:                 3

SPLIT TIME 1 (MILES/HR):      8.24 MILES PER HOUR

SPLIT TIME 2 (MILES/HR):      6.78 MILES PER HOUR

OVERALL RACE TIME (MILES/HR): 1.0 hours 45.0 minutes 0.0 seconds

OVERALL RACE PACE:            7.49 MILES PER HOUR

TOTAL SECONDS:                3240.0

--------------------------------------------------------

Please Enter Racer number (-1 to exit): -1

D:Program FilesJavajdk1.8.0_91in>