Question: Create an AverageScore program to average the scores in a file: Write a program that reads a text file containing an unknown number of movie
Create an AverageScore program to average the scores in a file:
Write a program that reads a text file containing an unknown number of movie review scores. Read the scores as double values and put them into an instance of ArrayList< Double >, then compute and print the average movie score.
Your program will be tested using text files of double values. You can test your program using files doubles.txt and nodoubles.txt (an empty file).
Hints:
You can start with PrintFile.java and reuse its logic up to the point where you print the lines of the file; instead, add the Doubles to the ArrayList and calculate and print their average. Be sure to change the type of List to be List< Double >.
To convert a String you have read from the input file to a double, use the method Double.parseDouble(yourString).
When you add a double value to an ArrayList< Double > Java automatically wraps the double as a Double object; when you get an item from an ArrayList< Double > Java will unwrap the Double to produce a double value. This is called autoboxing and autounboxing.
Be sure that the size of the list is not 0 before calculating the average!! If no doubles were read in, print No movie review scores to average otherwise print The average of the n movie review scores is average where n is the number of doubles and average is total / n.
import java.util.*; // for List import java.io.*;
public class AverageScore // Week 12 Lab 2 Exercise 2 - Chapter 12 Exercise 9 { public static void main(String[] args) // uses program command line arguments { // copy the main method from PrintFile.java below and reuse its logic up to the // point where you print the lines of the file; instead, add the Doubles into the // ArrayList and calculate and print their average; be sure to change the type of // the List to be List
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
