Convert this java code so there can be return statements so that the number of L
ID: 3569590 • Letter: C
Question
Convert this java code so there can be return statements so that the number of Laps can be returned as well as the Total Time, Lap Time, and Difference for each lap (basically so I can write a command to return just one of these if I wished, some of them might already have return statements set up).
import java.text.DecimalFormat;
import java.util.Scanner;
public class Splits
{
private static DecimalFormat df = new DecimalFormat("00.00");
{
Scanner keyboard = new Scanner(System.in);
boolean dataOk=false;
int numLaps = 0;
System.out.println("Enter the number or laps.");
numLaps=keyboard.nextInt();
keyboard.nextLine();
String[] timesString = new String[numLaps];
double[] timesDouble = new double[numLaps];
for(int i=0;i {
System.out.println("Enter time for lap "+(i+1));
timesString[i]=keyboard.nextLine();
timesDouble[i]=stringToSec(timesString[i]);
}
double[] singleLaps = new double[numLaps];
singleLaps[0]= timesDouble[0];
for(int i=1;i {
singleLaps[i] = timesDouble[i]-timesDouble[i-1];
}
String[] lapChange = new String[numLaps];
lapChange[0]= "---";
for(int i=1;i {
if(singleLaps[i]>=singleLaps[i-1])
{
lapChange[i]="+"+df.format(singleLaps[i]-singleLaps[i-1]);
}
else
{
lapChange[i]="-"+df.format(singleLaps[i-1]-singleLaps[i]);
}
}
System.out.println("Lap Total Time Lap Time Difference");
for(int i=0;i {
System.out.println("Lap "+(i+1) + " " +timesString[i] +
" " +secToString(singleLaps[i]) + " " +lapChange[i]);
}
}
public static double stringToSec(String input)
{
input = input.trim();
int colon = input.indexOf(":");
String temp = input.substring(0, colon);
int minutes = Integer.parseInt(temp);
temp = input.substring(colon+1);
double seconds = Double.parseDouble(temp);
seconds = seconds + minutes*60;
return seconds;
}
public static String secToString(double input)
{
int minutes = (int)input/60;
double seconds = input - (60*minutes);
//DecimalFormat df = new DecimalFormat("00.00");
String s = minutes+":"+df.format(seconds);
return s;
}
}
Explanation / Answer
import java.text.DecimalFormat;
import java.util.Scanner;
/*the number of Laps can be returned as well as the
Total Time, Lap Time, and Difference for each lap
*/
public class chegg
{
private static DecimalFormat df = new DecimalFormat("00.00");
int getnooflaps(int numLaps){
return numLaps;
}
String[] totaltime(String[] timesString){
return timesString;
}
String[] laptimearray(Double[] singleLaps,int numLaps){
String[] laptime = new String[numLaps];
for(int i=0;i<numLaps;i++) {
laptime[i]=secToString(singleLaps[i]);
}
return laptime;
}
String[] Difference(String[] lapChange){
return lapChange;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
boolean dataOk=false;
int numLaps = 0;
System.out.println("Enter the number or laps.");
numLaps=keyboard.nextInt();
keyboard.nextLine();
String[] timesString = new String[numLaps];
double[] timesDouble = new double[numLaps];
for(int i=0;i<numLaps;i++) {
System.out.println("Enter time for lap "+(i+1));
timesString[i]=keyboard.nextLine();
timesDouble[i]=stringToSec(timesString[i]);
}
double[] singleLaps = new double[numLaps];
singleLaps[0]=timesDouble[0];
for(int i=1;i<numLaps;i++) {
singleLaps[i] = timesDouble[i]-timesDouble[i-1];
}
String[] lapChange = new String[numLaps];
lapChange[0]= "---";
for(int i=1;i<numLaps;i++){
if(singleLaps[i]>=singleLaps[i-1])
{
lapChange[i]="+"+df.format(singleLaps[i]-singleLaps[i-1]);
}
else
{
lapChange[i]="-"+df.format(singleLaps[i-1]-singleLaps[i]);
}
}
System.out.println("Lap Total Time Lap Time Difference");
for(int i=0;i<numLaps;i++){
System.out.println("Lap "+(i+1) + " " +timesString[i] +
" " +secToString(singleLaps[i]) + " " +lapChange[i]);
}
}
public static double stringToSec(String input)
{
input = input.trim();
int colon = input.indexOf(":");
String temp = input.substring(0, colon);
int minutes = Integer.parseInt(temp);
temp = input.substring(colon+1);
double seconds = Double.parseDouble(temp);
seconds = seconds + minutes*60;
return seconds;
}
public static String secToString(double input)
{
int minutes = (int)input/60;
double seconds = input - (60*minutes);
//DecimalFormat df = new DecimalFormat("00.00");
String s = minutes+":"+df.format(seconds);
return s;
}
}