Please help me code the following in: JAVA Please code with many comments, so I
ID: 3724994 • Letter: P
Question
Please help me code the following in: JAVA
Please code with many comments, so I can understand.
Full points will be awarded, thanks in advance!
Summary You will write a racing report program using object-oriented programming with three classes: Reportoriver RaceReport and Race. The main method of the program will be in the class Reportoriver and will include a loop that allows you to enter in more than one race and have reports written out for all the races you enter.(The user will enter one race then get a report for that race. After that, the user will be asked whether they want to enter in another race and get another rgrt. Tiis rgai u:r ay; thay dh n ant to irny moraxx)Explanation / Answer
package assignment;
import java.util.Scanner;
public class ReportDriver {
public static void main(String[] args) {
String option = "";
Scanner sc = new Scanner(System.in);
do {
Race race = new Race();
race.readIntimes();
RaceReport.writeReport(race);
System.out.println("Enter another race? (y/n): ");
option = sc.nextLine();
} while(!option.equalsIgnoreCase("n"));
}
}
class RaceReport {
public static void writeReport (Race race) {
double times[] = race.getTimes();
System.out.println("First place (time in seconds):"+ times[2]);
System.out.println("Second place (time in seconds): "+times[1]);
System.out.println("Third place (time in seconds): "+times[0]);
System.out.println("The range of the race times (in seconds): "+race.getRange());
System.out.println("The average time of all racers (in seconds):"+race.getAvg());
}
}
class Race {
private double times[];
private double range;
private double avg;
public Race() {
times = new double[3];
for(int i = 0;i<3; i++)
times[0] = 0;
}
public void readIntimes() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the race times (in seconds):");
for(int i = 0; i <3; i++) {
times[i] = sc.nextDouble();
}
sortTimesAscending();
range = times[2]-times[0];
}
private void sortTimesAscending() {
double temp;
for(int i = 0; i <3; i++) {
for(int j = 0; j <3; j++) {
if(this.times[i] < this.times[j]) {
temp = this.times[i];
this.times[i] = this.times[j];
this.times[j] = temp;
}
}
}
}
public double[] getTimes() {
return times;
}
public void setTimes(double[] times) {
this.times = times;
}
public double getRange() {
return range;
}
public double getAvg() {
return (times[0]+times[1]+times[2])/3.0;
}
}
---output-------------
Enter the race times (in seconds):
50 60.6 56.6
First place (time in seconds):60.6
Second place (time in seconds): 56.6
Third place (time in seconds): 50.0
The range of the race times (in seconds): 10.600000000000001
The average time of all racers (in seconds):55.73333333333333
Enter another race? (y/n):
n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.