Tino APCS

Lab 13.2 Average

Background

  1. Download numbers.txt. The text file contains a large amount of integers. The integers range in value from 0 to 100. The text file has been created with one value on each line.

  2. The number of integers in the file is unknown. You must continue reading the file until there are no more integers to read. Your program should work with any number of integers, however the sum of the integers will not be larger than the maximum int value.

  3. Your program must find the average of the list of numbers.

Assignment

  1. Your program should print out the average of the data in numbers.txt to the console window. Format the average to print to the console rounded to 2 decimal places (Hint: you can use printf or use a calculation to find the rounded value).

  2. Your program should also write the average of the numbers to a new text file called "output.txt". You don't need to format the average written to the file to 2 decimal places (but it's a fun exercise to try!).

  3. Turn in your source code below. You don't need to turn in the output file.

Name your class PX_LastName_FirstName_Average

Gold Challenge

  1. Your program should find the average of all numbers in all .txt files in a user-selected folder.
  2. Your program should ignore anything that isn't a number. A number is any sequence of digits separated by one or more non-digits. No decimals.
    Example: Suppose there are .txt two files. If one file contains "10 5!3 happy2hip hop." and the other contains "1 2 3 4" the average is 3.75 because (10 + 5 + 3 + 2) + (1 + 2 + 3 + 4) = 30 and 30/8 = 3.75
  3. Learn about file choosers here.

You must Sign In to submit to this assignment

Dark Mode

Outline