Question: I have this following code here (3 different classes) and I would like my average to be returned rounded to two decimal points. Hope someone
I have this following code here (3 different classes) and I would like my average to be returned rounded to two decimal points. Hope someone can help.
Also, how could I change the method public static void writeReport() so that it is not static anymore?
Thank you!
//necessary imports for program import java.util.Scanner; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class HW_Classes here. * * @author * @version */ public class Race { //declaring instance variables private double raceTime[]; private double average; private double range; /* Race method creates array with for loop. * raceTime array is constructred with the help of the for loop. * All elements of the array are initialized and set to the value of * 0 in the for loop. For loop will loop through raceTime.length times, * thus, 3 times. * no return value. */ public Race () { //declaring raceTime array raceTime = new double[3]; //forloop to initialize array: values set to 0 for (int i = 0; i < raceTime.length; i++) { raceTime[0] = 0; } }
/* readInTimes method asks user to enter 3 times. * User is asked to enter 3 race times in seconds. The for-loop * fills raceTime array values with the help of the construction of * scanner s. * no return value. */ public void readInTimes() { Scanner s = new Scanner(System.in); System.out.println("Enter the race times (in seconds)"); //for loop to have user enter raceTimes for (int i = 0; i < raceTime.length; i++) { raceTime[i] = s.nextDouble(); } //call to method to sort raceTimes sortTimesAscending(); }
/* * sortTimesAscending sorts numbers in ascending order. * * */ private void sortTimesAscending() { double temp; //holds values temporarliy until reassigned //nested forloop to sort values of raceTime array in //ascending order. for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (this.raceTime[i] < this.raceTime[j]) { temp = this.raceTime[i]; this.raceTime[i] = this.raceTime[j]; this.raceTime[j] = temp; } } } }
/* get Times average * * * returns double raceTime */ public double[] getTimes() { return raceTime; }
public void setTimes(double[] times) { // this.raceTime = raceTime; }
/* getRange method */ public double getRange() { range = raceTime[2] - raceTime[0]; return range; }
/* getAverage method caculates the average of all * three raceTimes entered by the user with the help of a for * loop. * return: double average; */ public double getAverage() { //declare variable sum to help calculate average double sum = 0.0; //for loop to calculate sum & average of raceTimes for (int i = 0; i < raceTime.length; i++) { sum = (raceTime[i] + sum); average = (sum / raceTime.length); }
//returns average of raceTime return average; } public void getTestVariableValues() { //testAverage raceTime[0] = 3; raceTime[1] = 2; raceTime[2] = 5; getAverage(); System.out.println ("The averagetime of all racers should be 3.34."); } //end of class }
import java.util.Scanner; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /**ReportDriver * */ public class ReportDriver { /* * Race method: */ public static void main(String[] args) { //declaring scanner variable Scanner s = new Scanner(System.in); char choice = 'y'; //will return true for at least one race calculation while ((choice == 'y') || (choice == 'Y')) { Race race = new Race(); race.readInTimes(); //calling method readInTimes() from Race class RaceReport.writeReport(race); //call to testVariableValues method race.getTestVariableValues(); System.out.println("Enter another race (y/n): "); choice = s.next().charAt(0); //user input to enter another race } } //end of program }
//345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * * @author */ class RaceReport { /* writeReport prints results determined in class Race. * Prints first, second & third place and additionally prints average * and range */ public static void writeReport (Race race) { //prints first, second & third place double raceTime[] = race.getTimes();
if ((raceTime[0] == raceTime[1]) && (raceTime[0] != raceTime[2])) { System.out.println ("Two racers shared first place: " + raceTime[0]); System.out.println ("Second place (time in seconds): " + raceTime[2]); } else if ((raceTime[1] == raceTime[2]) && (raceTime[0] != raceTime[1])) { System.out.println ("First place (time in seconds): " + raceTime[0]); System.out.println ("Two racers shared second place: " + raceTime[1]); } else if ((raceTime[0] == raceTime[1]) && (raceTime[1] == raceTime[2])){ System.out.println ("Three racers shared first place: " + raceTime[0]); } else { System.out.println ("First place (time in seconds): " + raceTime[0]); System.out.println ("Second place (time in seconds): " + raceTime[1]); System.out.println ("Third place (time in seconds): " + raceTime[2]); }
//prints average & range 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.getAverage()); } //end of class & method }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
