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 filesdoubles.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 methodDouble.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 andaverage 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 (and ArrayList) vs. List // you should accumulate the total of the Doubles as you are adding them into // the List - convert each String read from the file into a double by // using Double.parseDouble(line), then add that double to a total (initialized // to 0) and also add it into the List, both inside the file reading loop; after // the loop runs either print a message because no doubles were read or else // print the average in a message where average = total / list.size() /* copy the body of PrintFile.java's main method here and make those changes: */ } }

import java.io.*;

public class PrintFile // this is CopyFile.java in the Sakai Week 12 Source Code folder { public static void main(String[] args) // uses program command line arguments { /* as mentioned above, change this so the program only expects 1 command line argument, and if that's not the case it prints Usage: PrintFile filename */ if (args.length != 2) { // change this if condition System.out.println("Usage: CopyFile inputFile outputFile"); // change this error message System.exit(0); } Scanner170 inStream = IO.inFile(args[0]); if (inStream == IO.inError) { System.out.println("Error: could not read input file " + args[0]); System.exit(0); } /* as mentioned above, remove this output stream processing section, it's not needed for PrintFile: */ PrintStream outStream = IO.newFile(args[1]); // assure output doesn't exist yet if (outStream == IO.outError) { System.out.println("Error: could not create output file " + args[1]); System.exit(0); } // could also use IO.exists(args[1]) first to check if output file exists

/* here is where to add code to create a List with a variable list and instantiate it with an ArrayList - add one line of code here: */ /* as mentioned above, modify the while loop body so that it uses List's add method to add each line from the input file to list: */ while (inStream.hasNextLine()) outStream.println(inStream.nextLine()); // replace this line to use List's add

inStream.close(); // close the input file when done reading from it /* remove the following line since there is no longer an output file stream: */ outStream.close(); // close the output file when done writing to it /* here is where you print out There are n lines in the file: - use list.size() to determine how many lines there are in the file (the number put in the list): */ /* finally, write a for-each loop here to pull out each String that is in list and print it to the monitor: */ } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!