HOW DO I GET THE PROGRAM TO READ THE INFORMATION FROM US.txt AND THEN PUT IT INTO AN ARRAY SO THAT THE REST OF CODE WORKS AND THE END RESULT IS THIS

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

import java.util.*;
import java.io.*;

class PopulationData {
private int[] population;

// Constructor with array object as parameter
public PopulationData(int[] population) {
this.population = population;
}

// Method to calculate average annual change in population
public int averageAnnualChange() {
int sum = 0;
for (int i = 0; i < population.length - 1; i++) {
sum += population[i + 1] - population[i];
}
return sum / (population.length - 1);
}

// Method to find year with greatest increase in population
public int yearWithGreatestIncrease() {
int maxIncrease = 0;
int maxYear = 0;
for (int i = 0; i < population.length - 1; i++) {
int increase = population[i + 1] - population[i];
if (increase > maxIncrease) {
maxIncrease = increase;
maxYear = i + 1 + 1950; // convert index to year
}
}
return maxYear;
}

// Method to find year with smallest increase in population
public int yearWithSmallestIncrease() {
double minIncrease = Double.MAX_VALUE;
int minYear = 0;
for (int i = 0; i < population.length - 1; i++) {
int increase = population[i + 1] - population[i];
if (increase < minIncrease) {
minIncrease = increase;
minYear = i + 1 + 1950; // convert index to year
}
}
return minYear;
}

// Method to find population in a specific year
public int populationInYear(int year) {
return population[year - 1950]; // convert year to index
}

// Method to display or return population in a specific period
public void populationInPeriod(int startYear, int endYear) {
System.out.printf("The population in thousands from %d through %d:\n", startYear, endYear);
for (int i = startYear - 1950; i <= endYear - 1950; i++) { // convert years to indices
System.out.printf("%.0f ", population[i]);
}
System.out.println();
}
}

public class PopulationDemo {
private static final int START_YEAR = 1950;
private static final int END_YEAR = 1990;

public static void main(String[] args) throws IOException {
int numYears = 1990 - 1950 + 1;


File file = new File("US.txt");
FileReader inputFile = new FileReader(file);
BufferedReader in = new BufferedReader(inputFile);

int[] population = new int [numYears];
int i = 0;
String s =in.readLine();

while(s!=null)
{
  
population[i] = Integer.parseInt(s); //this is line 19
  
s = in.readLine();
}
System.out.println(population[i]);
PopulationData p = new PopulationData(population);
  

System.out.printf("In population from %d through %d\n", START_YEAR, END_YEAR);
System.out.println("-------------------------------------------------------");
System.out.printf("The number of year: %d\n", population.length);
System.out.printf("The average annual change was %.2f\n", p.averageAnnualChange());
System.out.printf("The year with the greatest increase was %d. The population was %.0f thousands.\n",
p.yearWithGreatestIncrease(), p.populationInYear(p.yearWithGreatestIncrease()));
System.out.printf("The year with the smallest increase was %d. The population was %.0f thousands.\n",
p.yearWithSmallestIncrease(), p.populationInYear(p.yearWithSmallestIncrease()));

Scanner scanner = new Scanner(System.in);
String choice = "y";
while (choice.equals("y")) {
System.out.print("\nPlease enter a year you are looking for: ");
int year = scanner.nextInt();
if (year >= START_YEAR && year <= END_YEAR) {
System.out.printf("The population in %d was %.0f thousands.\n", year, p.populationInYear(year));
} else {
System.out.printf("Invalid year. Please enter a year between %d and %d.\n", START_YEAR, END_YEAR);
}
System.out.print("Do you want to continue searching? (y/n): ");
choice = scanner.next();
}

System.out.println();
p.populationInPeriod(START_YEAR, END_YEAR);
}
}

US.txt

151868
153982
156393
158956
161884
165069
168088
171187
174149
177135
179979
182992
185771
188483
191141
193526
195576
197457
199399
201385
203984
206827
209284
211357
213342
215465
217563
219760
222095
224567
227225
229466
231664
233792
235825
237924
240133
242289
244499
246819
249623

 

HOW DO I GET THE PROGRAM TO READ THE INFORMATION FROM US.txt AND THEN PUT IT INTO AN ARRAY SO THAT THE REST OF CODE WORKS AND THE END RESULT IS THIS

 
CTP 150 Computer Science 1
For example, I made a file (called USPopulation2.txt) only stores a few records with small
values (15, 18, 30, 54, 60, 76, 88) from 1950 through 1956. It is easy to see that,
increases/changes (the first year 1950 is not included) in six years are 3, 12, 24, 6, 16 and 12, and
average of changes is (3 1212416 16+12)/6. The running results:
In population from 1950 through 1956
The number of year:
The average annual change was 12.17
The year with the greatest increase was 1953. The population was 54 thousands.
The year with the smallest increase was 1951. The population was 18 thousands.
Please enter a year you are looking for: 1954
Population in the year 1954 is 60 thousands.
Do you want to continue searching? (y/n): y
Please enter a year you are looking for: 1955
Population in the year 1955 is 76 thousands.
Do you want to continue searching? (y/n): n
Search populations in a specific period:
Please enter starting year: 1951
Please enter ending year: 1955
The population in thousands from 1951 through 1955:
18 30 54 60 76
Do you want to continue searching? (y/n): n
If your program works for this small set of data, then it works for any size of data in any period!
In the application, to test data from different files, just remember to change the starting and
ending years, and make sure you have a right file name.
4
Transcribed Image Text:CTP 150 Computer Science 1 For example, I made a file (called USPopulation2.txt) only stores a few records with small values (15, 18, 30, 54, 60, 76, 88) from 1950 through 1956. It is easy to see that, increases/changes (the first year 1950 is not included) in six years are 3, 12, 24, 6, 16 and 12, and average of changes is (3 1212416 16+12)/6. The running results: In population from 1950 through 1956 The number of year: The average annual change was 12.17 The year with the greatest increase was 1953. The population was 54 thousands. The year with the smallest increase was 1951. The population was 18 thousands. Please enter a year you are looking for: 1954 Population in the year 1954 is 60 thousands. Do you want to continue searching? (y/n): y Please enter a year you are looking for: 1955 Population in the year 1955 is 76 thousands. Do you want to continue searching? (y/n): n Search populations in a specific period: Please enter starting year: 1951 Please enter ending year: 1955 The population in thousands from 1951 through 1955: 18 30 54 60 76 Do you want to continue searching? (y/n): n If your program works for this small set of data, then it works for any size of data in any period! In the application, to test data from different files, just remember to change the starting and ending years, and make sure you have a right file name. 4
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education