Write a method called computeScores that receives as it\'s parameter a String va
ID: 3714017 • Letter: W
Question
Write a method called computeScores that receives as it's parameter a String variable which is the name of a file. The file contains a list of test scores which are all doubles and each is separated by a new line. The method will print information to System.out so there is no return type. The method declaration should be the following: public static void computeScores (String fileName) throws FileNotFoundException f IYour code here. . The method should do the following: Check to see if the file exists and only continue if it does Compute the average and count of the test scores that are in the file ·Output the result to the screen For example, if the file contained the following: 87.0 95.3 74.6 Then the program should output: Read 3 test scores, average score is 85.63 You are only required to write the method, not the whole program and not an input file.Explanation / Answer
public static void computeScores(String fileName) { File file = new File(fileName); if(file.exists()) { try { Scanner fin = new Scanner(file); double total = 0; int count = 0; while (fin.hasNextDouble()) { total += fin.nextDouble(); count++; } total /= count; System.out.printf("Read %d test scores, average score is %.2f ", count, total); fin.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.