Question

write a Java console application that Create a text file called Data.txt. Within the file, use...

write a Java console application that Create a text file called Data.txt. Within the file, use the first row for the name and data title, and use the second row for column headers. Within the columns, insure that the first column is a label column and other columns contain numeric data. In your application, read file Data.txt into parallel arrays, one for each column in the file. Create method printArrays to print the header row(s) and the (unsorted) data in formatted columns. Within method printArrays, also calculate and print the max, min, and average of each numeric column. Sort the data by the first numeric column using any sort method. Use method printArrays to print the (sorted) data again. Format all real numbers to one decimal place.

Population and Housing Characteristics (Population, Age, Sex, Race, Households and Housing)
Column Number percent
0 to 5 years 2531333 6.8
5 to 9 years 2505839 6.7
10 to 14 years 2590930 7.0
15 to 19 years 2823940 7.6
20 to 24 years 2765949 7.4
25 to 29 years 2744409 7.4
30 to 34 years 2573468 6.9
35 to 39 years 2573579 6.9
40 to 44 years 2609131 7.0
45 to 49 years 2689819 7.2
50 to 54 years 2562552 6.9
55 to 59 years 2204296 5.9
60 to 64 years 1832197 4.9
65 to 69 years 1303558 3.5
70 to 74 years 971778 2.6
75 to 79 years 766971 2.1
80 to 84 years 603239 1.6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File ProcessMyDataFile.java

package data.file.process;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

public class ProcessMyDataFile {

private String header1;

private String header2;

private String[] column1;

private double[] number;

private double[] percent;

// Constructor to initialize the private variables

public ProcessMyDataFile()

{

header1 = "Population and Housing Characteristics (Population, Age, Sex, Race, Households and Housing)";

header2 = "Column Number percent";

column1 = new String[]{ "0 to 5 years", "5 to 9 years", "10 to 14 years", "15 to 19 years", "20 to 24 years",

"25 to 29 years", "30 to 34 years", "35 to 39 years", "40 to 44 years", "45 to 49 years",

"50 to 54 years", "55 to 59 years", "60 to 64 years", "65 to 69 years",

"70 to 74 years", "75 to 79 years", "80 to 84 years" };

number = new double[]{ 2531333, 2505839, 2590930, 2823940, 2765949, 2744409, 2573468,

2573579, 2609131, 2689819, 2562552, 2204296, 1832197,

1303558, 971778, 766971, 603239 };

percent = new double[]{6.8, 6.7, 7.0, 7.6, 7.4, 7.4, 6.9, 6.9, 7.0, 7.2, 6.9, 5.9, 4.9, 3.5, 2.6,

2.1, 1.6 };

}

/**

* Method to create the text file named Data.txt containing above data

*/

public void CreateTextFile()

{

PrintWriter output = null;

try {

output = new PrintWriter("Data.txt");

} catch (FileNotFoundException e) {

System.err.println("Unable to create the Data.txt file");

System.exit(0);

}

output.println(header1);

output.println(header2);

for(int i = 0; i<column1.length; i++)

{

output.print(column1[i]+" "+number[i]+" "+percent[i]+"\n"); // writing to file

}

output.close();

System.out.println(">> File Data.txt created successfully. Please check the program's project folder <<");

}

/**

* Reading the created file and saving its contents in arrays

* @throws IOException if not able to open and read

*/

public void readFile() throws IOException

{

String header1, header2;

String[] column = new String[20];

double[] number = new double[20];

double[] percent = new double[20];

BufferedReader buff = null;

try {

buff = new BufferedReader(new FileReader("Data.txt"));

} catch (FileNotFoundException e) {

System.err.println("Data.txt file not found");

System.exit(0);

}

header1 = buff.readLine();

header2 = buff.readLine();

int i = 0;

String st;

while((st = buff.readLine() ) != null)

{

String col = "";

String[] str = st.split(" ");

int j = 0;

while(j < str.length - 2 )

{

col += str[j]+" ";

j++;

}

column[i] = col;

number[i] = Double.parseDouble(str[str.length - 2]);

percent[i] = Double.parseDouble(str[str.length - 1]);

i++;

}

buff.close();

System.out.println("\n Printing unsorted data \n");

printArrays(header1, header2,column, number, percent, i);

sortArrays(column, number, percent, i);

System.out.println("\n Printing sorted data based on the number column\n");

printArrays(header1, header2,column, number, percent, i);

}

/**

* Method to sort arrays using bubble sort

* @param column

* @param number

* @param percent

* @param size

*/

public void sortArrays(String[] column, double[] number, double[] percent, int size)

{

for (int i = 0; i < size; i++) {

for (int j = 1; j < size-i; j++) {

if (number[j-1] > number[j])

{ // swap elements j and j+1

double temp = number[j-1];

number[j-1] = number[j];

number[j] = temp;

  

String tempS = column[j-1];

column[j-1] = column[j];

column[j] = tempS;

  

double temp2 = percent[j-1];

percent[j-1] = percent[j];

percent[j] = temp2;

}

}

}

}

/**

* Method to calculate max, min, average of each numeric column and print them as well as the arrays

* @param header1

* @param header2

* @param column

* @param number

* @param percent

* @param size

*/

public void printArrays(String header1, String header2, String[] column, double[] number, double[] percent, int size)

{

System.out.println(header1);

System.out.println(header2);

double max1 = number[0];

double min1 = number[0];

double max2 = percent[0];

double min2 = percent[0];

double sum1 = 0, sum2 = 0;

// loop to print arrays and find max, min and sum

for(int i = 0; i<size; i++)

{

System.out.println(column[i]+" "+number[i]+" "+percent[i]);

if(number[i] > max1)

max1 = number[i];

if(number[i] < min1)

min1 = number[i];

if(percent[i] > max2)

max2 = percent[i];

if(percent[i] < min2)

min2 = percent[i];

sum1 += number[i];

sum2 += percent[i];

}

double avg1 = sum1 / size;

double avg2 = sum2 / size;

System.out.println("\nGreatest number in 'number' column is: " + max1);

System.out.println("Smallest number in 'number' column is: " + min1);

System.out.printf("%s %.1f\n\n", "Average of 'number' column: ", avg1);

System.out.println("Greatest number in 'percent' column is: " + max2);

System.out.println("Smallest number in 'percent' column is: " + min2);

System.out.printf("%s %.1f\n", "Average of 'percent' column: ", avg2);

System.out.println();

}

// main method

public static void main(String[] args) throws IOException

{

ProcessMyDataFile process = new ProcessMyDataFile();

process.CreateTextFile();

process.readFile();

}

}

OUTPUT

Printing sorted data based on the number columrn Population and Housing Characteristics (Population, Age, Sex, Race, Househol

DIRECTIONS: Open your java ide create a new java project with package name data.file.process and within this package create a new class named ProcessMyDataFile after then copy-paste the above code into the ProcessMyDataFile.java file and run.

Please check the output file Data.txt in the project folder (data.file.process) in your workspace.

Add a comment
Know the answer?
Add Answer to:
write a Java console application that Create a text file called Data.txt. Within the file, use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA HELP! Question: Write a Java console application that investigates how well Java handles large integers....

    JAVA HELP! Question: Write a Java console application that investigates how well Java handles large integers. Write two loops that iterate from 0 through 35. Before each loop, set an integer variable (IV) to 1. Within each loop, print the loop count and the value of IV formatted in two columns. Within the first loop, multiply IV by 2. Within the second loop, multiply IV by the appropriate StrictMath method. The second loop will not complete since there will eventually...

  • please answer it in a java console application form (java GUI). Least Square Lines Equation- Text File /O Suppose we ha...

    please answer it in a java console application form (java GUI). Least Square Lines Equation- Text File /O Suppose we have a text file (which I supplied named data.txt) that has the following table: Temperature (celsius) Resistance (ohms) 20.0 761 31.5 817 50.0 874 71.8 917 91.3 1018 Write a Java console applicatlon, that does the following: 1. Prompt the user for the name of the text file 2. Opens the text file and reads in the ordered pair data...

  • answer in c# console application. File l/O Create a Person class (firstName, lastName, phoneNumber, gender (Use...

    answer in c# console application. File l/O Create a Person class (firstName, lastName, phoneNumber, gender (Use radioButtons for gender in the GUI) ) Create a List<Person> Create a GUI that will collect the Person information from the user and have 3 buttons: Add, Display, Read. Add button Add the just created person to List<Person>, and append to a text file Display button > print out all persons currently in the List<Person> in a label in a visually attractive way Read...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines...

    create a new Java application called "WeightedAvgWithExceptions" (without the quotation marks), according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files. Input File The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions...

  • 7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food...

    7.2 Write a Java program called to create an Excel spreadsheet Create a class called Food to represent a Lunch food item. Fields include name, calories, carbs In a main method (of a different class), ask the user "How many things are you going to eat for lunch?". Loop through each food item and prompt the user to enter the name, calories, and grams of carbs for that food item. Create a Food object with this data, and store each...

  • Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions....

    Create a file called Sort.py (note the capitalization). Within that file, write two different Python functions. Each function will take an array of integers as a parameter and sort those integers in increasing order. One will use insertion sort, and the other will use selection sort (described below). Simple functions will suffice here; do not create any classes. Your insertion sort function should be called insertion_sort(arr). Your selection sort function should be called selection_sort(arr). Selection sort must use a while...

  • EA6-A2 Complete a Depreciation Schedule for Furniture Resellers In this exercise, you will create a depreciation...

    EA6-A2 Complete a Depreciation Schedule for Furniture Resellers In this exercise, you will create a depreciation schedule for Furniture Resellers as of 12/31/2016 using an Excel table. You will then sort, filter, and analyze the data in the table. These fixed assets, with associated data as of 12/31/2015, were acquired prior to the current year. Fixed Asset Date of Cost Salvage Useful Life Accumulated Acquisition Value (years) Depreciation Machinery 1/1/2007 $8,200 $700 10 $6,750 1/1/2009 $8,400 Garage Equipment $11,000 $200...

  • create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in...

    create a new Java application called "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain...

  • The input file should be called 'data.txt' and should be created according to the highlighted instructions...

    The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know the name of the input file, you should not hard-code this name into your program. Instead, prompt the user for the name of the input file. The input file should contain (in order): the weight (a number greater than zero and less than or equal to 1), the number, n, of lowest numbers to drop, and the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT