Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider writing a Java program, AveragesInFile, for computing the average of all strictly positive numbers appearing in a file and the average of all strictly

image text in transcribed

image text in transcribed

Consider writing a Java program, AveragesInFile, for computing the average of all strictly positive numbers appearing in a file and the average of all strictly negative numbers appearing in the same file. The numbers that are equal to 0 must be excluded from the average calculation. The program receives the path to the text file as the leading element of args. Suppose "/Users/mitsu/data/inputFile.txt" is the path to a text file whose contents are as follows: 12.0 2.5 10 20.5 0 2 7.0 0.00 31% Then executing the program with the paths as the first element of args produces the following: % java Average1nFile /Usersitsu/data/inputFile.txt 2"inputFile. txt" has 3 positive numbers. 3 Their average is 11.000 4"inputFile.txt" has 2 negative numbers Their average is -4.500 Suppose we will use the source code on the next page for this program, with _AA_ through _MM as placeholders. In that code, totalPos and totalNeg are for computing storing running totals from the two number groups, and countPos and countNeg are for counting encounters to the two groups. Also,-11-,-J., and LL represent formats String values and-MM-is for handling an run-time error that may be caused when Line 12 is executed. 8 points; 6 points for FF and 1 point each for the rest) State what should be in AA through MM, for the code to compile and produce the desired behavior of the program. (b) (2 points) State the type of error that may occur at Line 18. 1 import java.util. 2import java.io.*; 3 public class AveragesInFile 5 public static void main( String[] args) try // initialization and instantiation String fileNameAA_; File f B; Scanner scanner _cc int countPos-0, countNeg0; double totalPos 0.0, totalNeg . 0.0; while _DD) 10 12 13 14 15 16 17 18 19 20 21 // read the next token inputEE // updates 23 24 25 26 / compute the averages double averagePos-_GG-; double averageNeg_HH_; System.out.printf ""_II\" hasJJ positive numbers. ", f. _KK_, countPos averagePos ""_II\" hasJJ negative numbers. ", System.out .printf "Their average is _LL_. " , 29 30 31 32 System.out.printf f. _KK, countNeg System.out .printf " Their average is _LL_ .In", catch MM e ) // nothing to do. Quietly terminate the program 34 35 averageNeg ); 37 40

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions