Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction: This assignment is to provide practice in using the Java I/O techn

ID: 3867422 • Letter: I

Question

Introduction:

This assignment is to provide practice in using the Java I/O techniques discussed in the Module 10
video lectures and readings. Although the main focus of this assignment is Java I/O techniques, Java
design and implementation techniques discussed in earlier modules should be incorporated in to this
assignment.

Problem:

Supplied is a data file from the US Census which contains data from US school districts and reports
statistics related to child poverty (https://www.census.gov/did/www/saipe/downloads/sd13/USSD13.txt).
Here for data documentation and layout: https://www.census.gov/did/www/saipe/downloads/sd13/README.txt
It is desired to have a summary report which calculate basic statistics at the state level.

Desired Implementation:

Java 8 implementation to read the supplied text data and produce a report similar to the below:

There should be two separate “programs” (main()), one to read the text data file and write a reformatted
file to be read by the second program which will create the report to standard out. Note before the
report is displayed, a single line with “File: “ then the path of the input file for the report is displayed.

The first program will have 3 run-time parameters, the data source file path, the destination file path,
and the number of records in the data file (13486) .

The second program will have 2 run-time parameters, the input file path and the number of records.

Features and Restrictions:

The programs should use standard (SE) Java 8 code and compile without errors or warnings. It should
also run without errors or warnings when given valid input.

The programs should provide reasonable parameter validation (correct number of parameters,reasonable values, etc.).

The programs should not use any Java collections (ArrayList, Map, Vector, etc.) except standard Java
arrays. Collections are introduced in a later module.
The file produced by the first program should not be deleted after running the report program.
The program's code should be reasonable formatted and commented as demonstrated so far in the course.
Resources:
File: SmallAreaIncomePovertyEstData.txt – contains the small area poverty data. It is a standard 8-bit readable text file. https://www.census.gov/did/www/saipe/downloads/sd13/USSD13.txt

File: SmallAreaIncomePovertyEstLayout.txt – contains information about the field layout of the SmallAreaIncomePovertyEstData.txt file. https://www.census.gov/did/www/saipe/downloads/sd13/README.txt

y-841 t | 1 1 4 7 0 4 1 0 1 0 4 5 0 2 4 6 1 07721570 e -52452537934389946 P- 1 1 28535945 20733932 i | 0 1 7 9 7 3 8 1 506362563 n -38705159 0-21721896 a -56828975085967674 252278 1-0183637227 2141 2 1 0108699711648666 4356923060998408 i ” 3 7 9 9 2 7 6 2 5 3 2 4 2 2 1 36 a | 4 2 2 6 7 2 3 7 0 8 1 6 4 4593 | | 1 3 8 1 6 0 9 4 7 4 2 1 1 2 6 22 98232155 u -81156951 1 1 1 1 n -22935769905460967 66536915 84010049 0-23470674 a | 3 5 8 9 9 8 7 5 6 20424003 | | 3 3 8 5 0 6 4 2 4 5 1 010799 u | 8 7 6 9 9 2 7 9 6 50467508 0-4 82853 90117632 e ” 1 2 4 5 6 8 9 0 1 2 3 5 6 7 8 9 0

Explanation / Answer

Given below are the 2 programs as mentioned in the question. Please pass 3 arguments to ReportMaker.java and 2 arguments for DisplayReport.java. The 2 programs however are not using the last parameter which the number of records (as it is not required). But since the question specifies to pass 3 parameters, have coded the same way.

The output file from ReportMaker should be given as input file for DisplayReport. Please do rate the answer if it helped. Thank you.

ReportMaker.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ReportMaker {
   private static int load_file(String filename, int[] state, int[] population, int[] children, int[] child_poverty) throws FileNotFoundException
   {
       BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
       String line;
       String str;
       int state_code;
       int last_state = 0;
       try {
           while((line = br.readLine()) != null)
           {
               //get the differenet values based on column numbers
               // 1- 2 FIPS State code (00 for US record)
       // 4- 8 District ID
       // 10-81 District Name
       // 83-90 Total Population
       // 92-99 Population of Relevant Children 5 to 17 years of Age
       //101-108 Estimated Number of Relevant Children 5 to 17 years old
       // in Poverty Related to the Householder
              
               //get the state code
               str = line.substring(0, 2).trim();
               state_code = Integer.parseInt(str);
               state[state_code] = state_code;
              
               //get total population
               str = line.substring(82, 90).trim();
               population[state_code] += Integer.parseInt(str);
              
               //get children population
               str = line.substring(91, 99).trim();
               children[state_code] += Integer.parseInt(str);
              
               //get child poverty population
               str = line.substring(100, 108).trim();
               child_poverty[state_code] += Integer.parseInt(str);
              
               if(state_code > last_state)
                   last_state = state_code;
           }
           br.close();
       } catch (IOException e) {
           System.out.println("Exception occured." + e.getMessage());
       }
       return last_state;
   }
   private static void generate_report(String filename, int last_state, int[] state, int[] population, int[] children, int[] child_poverty) throws IOException
   {
       File f = new File(filename);
       BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)));
       double percent;
       String line;
       for(int i = 1; i <= last_state; i++ )
       {
           if(state[i] == 0) continue; //no data for the state code
           //calculate percentages
           percent = child_poverty[i] * 100.0 / children[i];
           line = String.format("%02d %15d %15d %15d %10.2f", state[i], population[i], children[i], child_poverty[i], percent);
           bw.write(line + " ");
       }
       bw.close();
       System.out.println("Report generated in file: " + f.getAbsolutePath());
   }
  
   public static void main(String[] args) {
       if(args.length != 3) //check no. of command line args
       {
           System.out.println("Usage: java ReportMaker <source_file_path> <destination_file_path> <num_records>");
           System.exit(1);
       }
      
       int[] state, population, children, child_poverty;
         
       int last_state = 0;
      
       //create an array for max 100 since state code is 2 digit code , there are max 99 values
       state = new int[100];
       population = new int[100];
       children = new int[100];
       child_poverty = new int[100];
         
      
       try {
           last_state = load_file(args[0], state, population, children, child_poverty);
           generate_report(args[1], last_state, state, population, children, child_poverty);
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
      
   }
}

output

java ReportMaker USSD13.txt poverty.txt 13486

Report generated in file: /Users/raji/Documents/workspace/Test/poverty.txt

Generated Report file:

01 4833722 814377 205023 25.18
02 735132 132740 16118 12.14
04 8688149 1182931 288777 24.41
05 2959373 516950 132920 25.71
06 48909205 6667268 1468715 22.03
08 5268367 902796 139381 15.44
09 3747676 593629 77895 13.12
10 925749 147239 25169 17.09
11 646449 70507 20544 29.14
12 19552860 2948361 678022 23.00
13 10010465 1821201 445608 24.47
15 1404054 216496 29375 13.57
16 1612136 314294 56633 18.02
17 17704060 2224288 427235 19.21
18 6570099 1165146 226599 19.45
19 3090416 529306 77634 14.67
20 2893957 523686 84325 16.10
21 4405846 739127 171418 23.19
22 4625470 804740 212904 26.46
23 1358717 196262 31174 15.88
24 5928814 977312 120049 12.28
25 7107877 1028400 153286 14.91
26 9895622 1672433 351702 21.03
27 5420550 931542 119437 12.82
28 2991207 539006 170629 31.66
29 6044171 1020848 203216 19.91
30 1913094 162709 30655 18.84
31 1868516 334188 49030 14.67
32 2790136 483411 99599 20.60
33 1472055 205461 19714 9.60
34 10552547 1488882 222992 14.98
35 2085287 368816 103790 28.14
36 19901043 3066336 666553 21.74
37 9848060 1673310 386419 23.09
38 723393 113921 12685 11.13
39 11570743 1958998 398688 20.35
40 3851487 682548 144867 21.22
41 3931430 627584 118023 18.81
42 12773801 1999741 342181 17.11
44 1065907 159355 31368 19.68
45 4790785 787482 194639 24.72
46 844877 148002 24675 16.67
47 6778703 1091900 260103 23.82
48 26452422 5101161 1198322 23.49
49 2900872 642722 85745 13.34
50 940840 92223 11990 13.00
51 8260405 1352420 190734 14.10
53 6971406 1151175 197126 17.12
54 1854304 279484 64539 23.09
55 5956920 963445 157356 16.33
56 582360 99290 11701 11.78

DisplayReport.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class DisplayReport {
   public static void main(String[] args) {
       if(args.length != 2)
       {
           System.out.println("Usage: java DisplayReport <source_file_path> <num_records>");
           System.exit(1);
       }
      
       BufferedReader br;
       File f = new File(args[0]);
       System.out.println("File: " + f.getAbsolutePath() + " ");
       int state, population, children, poverty;
       double percent;
       String line;
       try {
           br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
           System.out.printf("State %12s %20s %25s %15s ",
                   "Population", "Child Population", "Child Poverty Population", "% Child Poverty");
           System.out.printf("----- %12s %20s %25s %15s ",
                   "----------", "-----------------", "------------------------", "---------------");
          
           while((line = br.readLine()) != null)
           {
               //split into tokens base on space characters as delimiters ,(s)+ means 1 or more whitespace characters
               String[] tokens = line.split("(\s)+");
               state = Integer.parseInt(tokens[0]);
               population = Integer.parseInt(tokens[1]);
               children = Integer.parseInt(tokens[2]);
               poverty = Integer.parseInt(tokens[3]);
               percent = Double.parseDouble(tokens[4]);
               line = String.format(" %02d", state);
               line += String.format(" %,12d", population);
               line += String.format(" %,20d", children);
               line += String.format(" %,25d", poverty);
               line += String.format(" %15.2f", percent);
               System.out.println(line);
           }
           br.close();
       } catch (FileNotFoundException e) {
           System.out.println(e.getMessage());
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
      
      
   }
}

output

java DisplayReport poverty.txt 51

File: /Users/raji/Documents/workspace/Test/poverty.txt

State Population Child Population Child Poverty Population % Child Poverty
----- ---------- ----------------- ------------------------ ---------------
01 4,833,722 814,377 205,023 25.18
02 735,132 132,740 16,118 12.14
04 8,688,149 1,182,931 288,777 24.41
05 2,959,373 516,950 132,920 25.71
06 48,909,205 6,667,268 1,468,715 22.03
08 5,268,367 902,796 139,381 15.44
09 3,747,676 593,629 77,895 13.12
10 925,749 147,239 25,169 17.09
11 646,449 70,507 20,544 29.14
12 19,552,860 2,948,361 678,022 23.00
13 10,010,465 1,821,201 445,608 24.47
15 1,404,054 216,496 29,375 13.57
16 1,612,136 314,294 56,633 18.02
17 17,704,060 2,224,288 427,235 19.21
18 6,570,099 1,165,146 226,599 19.45
19 3,090,416 529,306 77,634 14.67
20 2,893,957 523,686 84,325 16.10
21 4,405,846 739,127 171,418 23.19
22 4,625,470 804,740 212,904 26.46
23 1,358,717 196,262 31,174 15.88
24 5,928,814 977,312 120,049 12.28
25 7,107,877 1,028,400 153,286 14.91
26 9,895,622 1,672,433 351,702 21.03
27 5,420,550 931,542 119,437 12.82
28 2,991,207 539,006 170,629 31.66
29 6,044,171 1,020,848 203,216 19.91
30 1,913,094 162,709 30,655 18.84
31 1,868,516 334,188 49,030 14.67
32 2,790,136 483,411 99,599 20.60
33 1,472,055 205,461 19,714 9.60
34 10,552,547 1,488,882 222,992 14.98
35 2,085,287 368,816 103,790 28.14
36 19,901,043 3,066,336 666,553 21.74
37 9,848,060 1,673,310 386,419 23.09
38 723,393 113,921 12,685 11.13
39 11,570,743 1,958,998 398,688 20.35
40 3,851,487 682,548 144,867 21.22
41 3,931,430 627,584 118,023 18.81
42 12,773,801 1,999,741 342,181 17.11
44 1,065,907 159,355 31,368 19.68
45 4,790,785 787,482 194,639 24.72
46 844,877 148,002 24,675 16.67
47 6,778,703 1,091,900 260,103 23.82
48 26,452,422 5,101,161 1,198,322 23.49
49 2,900,872 642,722 85,745 13.34
50 940,840 92,223 11,990 13.00
51 8,260,405 1,352,420 190,734 14.10
53 6,971,406 1,151,175 197,126 17.12
54 1,854,304 279,484 64,539 23.09
55 5,956,920 963,445 157,356 16.33
56 582,360 99,290 11,701 11.78

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote