I am having trouble with this problem from Java - Savitch 7th edition. Chegg doe
ID: 666284 • Letter: I
Question
I am having trouble with this problem from Java - Savitch 7th edition.
Chegg does not have a solution for it, it is programming project 10 for chapter 10.
I will upload a picture from the book since it is a lengthy problem. [IMG]http://i61.tinypic.com/2el9v1t.jpg[/IMG]
Here is another link in case it doesnt work through chegg: http://postimg.org/image/cybaclei9/
I have been able to read the input file (I scanned the given text file) and make an int array out of the information, but do not know where to go from here.
Any help will be greatly appreciated. Here is the given log from the problem:
08 00 00
0,100,08 00 00
0,132,08 00 03
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
Explanation / Answer
/* problem almost solved here, Just you program for racer finish time and crossed check points */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.in;
import java.util.Scanner;
public class JavaApplication6 {
public static void main(String[] args) {
// TODO code application logic here
double distanceInMiles=13.1;
String fin="raceLog.txt";
String racerRecords[]=new String[20];
try{
FileInputStream fis = new FileInputStream(fin);
//Construct BufferedReader from InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count=-1;
while ((line = br.readLine()) != null) {
System.out.println(line);
racerRecords[++count]=line;
}
//int racerId[]=new int[20];
int sensorAid[]=new int[20];
int sensorAh[]=new int[20];
int sensorAm[]=new int[20];
int sensorAs[]=new int[20];
int sensorBid[]=new int[20];
int sensorBh[]=new int[20];
int sensorBm[]=new int[20];
int sensorBs[]=new int[20];
int sensorCid[]=new int[20];
int sensorCh[]=new int[20];
int sensorCm[]=new int[20];
int sensorCs[]=new int[20];
//getting gun time
int gunH, gunM,gunS;
gunH=Integer.parseInt(racerRecords[0].substring(0, 1));
gunM=Integer.parseInt(racerRecords[0].substring(3, 4));
gunS=Integer.parseInt(racerRecords[0].substring(6 ,7));
int a=0,b=0,c=0;
for(int j=1;j<=count;j++)
{
int s=Integer.parseInt(racerRecords[j].substring(0, 0));
int id=Integer.parseInt(racerRecords[j].substring(2 ,4));
if(s==0)
{
sensorAid[a]=id;
sensorAh[a]=Integer.parseInt(racerRecords[j].substring(6, 7));
sensorAm[a]=Integer.parseInt(racerRecords[j].substring(9, 10));
sensorAs[a]=Integer.parseInt(racerRecords[j].substring(12, 13));
a++;
}
else if(s==1)
{
sensorBid[b]=id;
sensorBh[b]=Integer.parseInt(racerRecords[j].substring(6, 7));
sensorBm[b]=Integer.parseInt(racerRecords[j].substring(9, 10));
sensorBs[b]=Integer.parseInt(racerRecords[j].substring(12, 13));
b++;
}
if(s==2)
{
sensorCid[c]=id;
sensorCh[c]=Integer.parseInt(racerRecords[j].substring(6, 7));
sensorCm[c]=Integer.parseInt(racerRecords[j].substring(9, 10));
sensorCs[c]=Integer.parseInt(racerRecords[j].substring(12, 13));
c++;
}
}
//racer details
br.close();
System.out.println(" Enter racer id:");
int racerNum;
Scanner sc = new Scanner(System.in);
racerNum=sc.nextInt();
double minA,paceA,minB,paceB,minC,paceC;
double totmin=0.0,totpace=0.0;
for(int k=0;k<a;k++)
{
if(sensorAid[k]==racerNum)
{
minA=sensorAh[k]*60 +sensorAm[k]+sensorAs[k]/60;
paceA=minA/distanceInMiles;
System.out.println("Split time in minutes /distance in miles: "+paceA);
totmin=totmin+minA;
totpace=totpace+paceA;
break;
}
}
for(int k=0;k<b;k++)
{
if(sensorBid[k]==racerNum)
{
minB=sensorBh[k]*60 +sensorBm[k]+sensorBs[k]/60;
paceB=minB/distanceInMiles;
System.out.println("Split time in minutes /distance in miles: "+paceB);
totmin=totmin+minB;
totpace=totpace+paceB;
break;
}
}
for(int k=0;k<c;k++)
{
if(sensorCid[k]==racerNum)
{
minC=sensorCh[k]*60 +sensorCm[k]+sensorCs[k]/60;
paceC=minC/distanceInMiles;
System.out.println("Split time in minutes /distance in miles: "+paceC);
totmin=totmin+minC;
totpace=totpace+paceC;
break;
}
}
System.out.println("Overall pace:"+ totpace/3);
System.out.println("Overall time:"+ totmin/3);
}
catch(IOException | NumberFormatException e)
{}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.