15. Population Data If you have downloaded this book\'s source code (the compani
ID: 3728697 • Letter: 1
Question
15. Population Data If you have downloaded this book's source code (the companion website is available at www.pearsonhighered.com/gaddis), you will find a file named USPopulation.txt in the Chapter 7 folder. The file contains the midyear population of the United States, in thou- sands, during the years 1950 to 1990. The first line in the file contains the population for 1950, the second line contains the population for 1951, and so forth. Write a program that reads the file's contents into an array, or an ArrayList. The program should display the following data . The average annual change in population during the time period . The year with the greatest increase in population during the time period . The year with the smallest increase in population during the time period
Explanation / Answer
public class PopulationUSA
{
public static void main(String[] args) throws IOException
{
final int ARRAY_SIZE = 50;
int[] population = new int[ARRAY_SIZE];
int i,total = 0;
int populationAvg;
File file = new File("USPopulation.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext() && i < population.length)
{
population[i] = inputFile.nextInt();
total += population[i];
i++;
}
populationAvg = total / population.length;
System.out.println("The Average annual change in population is: " + populationAvg);
inputFile.close();
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.