Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java I Code Project and Class name: CommaSeparatedValues Follow naming rules (first line should be WJoe Student CommaScparatedValues, and window title bar should have that

Java I Code

image text in transcribed

Project and Class name: CommaSeparatedValues Follow naming rules (first line should be WJoe Student CommaScparatedValues, and window title bar should have that same text). You will be using Window Builder to write an application that opens Comma Separated Value files (.csv files). This is a fairly standard file format. It starts out simple: each line in the text files contains values separated by commas, like: name, height,weight,score George, 66.8, 188.5, 105 Fred, 72.125, 200, 95 We are going to read such files, with some simplifications: 1. For the first line, the Values" will be strings [obviously, these strings can't include commas - why?) 2. For the subsequent lines in the file, the "values" will all be all be parseable as doubles, EXCEPT the first value: it will be a String (Think about it, that string can't include a comma, because it would be taken as the comma to separate one value from another). 3. Every line will have exactly the same number of "values, or as they are usually called, "fields. That is, each line will have exactly the same number of commas on it. When your program starts up. you may find it convenient to have button to "Start" or "Find File. When the user presses the button, use a JFileChooser to allow the user to open a .csv fle. Bonus 5 points: figure out how to limit the dialog to show only csv files. When the user has identified the file, read it into a string. Find a method in the String class to split the string into substrings that were separated by commas. The length of the array returned by this method will tell you how many fields (the number of commas is one less than the number of fields, no?) in this, and by rule 3 above, all subsequent lines. When you have that first line, you will then be able to read the rest of the lines. You will want to read all the data into your program. Make sure you have a second class in your program. It can be in a separate file of its own, or as a "private inner class" in your main .java class. Either way, your class should contain two fields: a String for the name, and an array of doubles. You may find it convenient to use Eclipse to build getter and setter methods for that field, and you may also find it convenient to copy some of the code from the setter into a constructor that takes an array of doubles as an argument. Read each subsequent line of the file. Use your splitting technique to split each line into an array of strings. Each string can then be parsed into a double. Be sure to use the trim) methods to remove any spaces at the beginning or end of each of the substrings. You will want to do the trimming and parsing in a loop - your code can "remember" how many fields to expect for each line, from the splitting and parsing of the first line. When each line has been read and converted to an array of doubles, store those doubles into an object in the second class described above. Then store that object into an ArrayList of those objects. The ArrayList is very nice for this, since it stores objects, and doesn't have to "know in advance" how many to allocate. You just use the "add(...." method to put the objects into the ArrayList. When you get to the end of the file (You will know because the 'readLineO method will return anur when you try to read past the end of the file, and you can test this and use it to "break" out of the read loop), you will then produce your output. First put your output into a multi-line string. That is simply a string like: String s "first line nsecond line nthird line" where the lines are ended with n" newline characters. It is useful when building a multi-line string like this to repeatedly add to a string like this: s+"another linen" When you have built your report (see below), use the .setTextis) method of a JTextArea control on your window to display the report. The report should look very much like this: Read and processed 2 lines in file Cusers dave documents testdoc.csv. There were 3 fields in this file average height was: 69.4625, max was Fred average weight was: 194.25, max was Fred average score was: 100, max was George Bonus 5 points, put the JTextArea into a scrolling container, so if there are more lines in the report than can fit in your area, the user will be able to scroll through an see all the lines. While it would be possible to get this report without storing all of the data in an array or ArrayList, other operations like calculating a standard deviation would require scanning through the data multiple times, so I require you to store the entire contents of the file before calculating the averages Project and Class name: CommaSeparatedValues Follow naming rules (first line should be WJoe Student CommaScparatedValues, and window title bar should have that same text). You will be using Window Builder to write an application that opens Comma Separated Value files (.csv files). This is a fairly standard file format. It starts out simple: each line in the text files contains values separated by commas, like: name, height,weight,score George, 66.8, 188.5, 105 Fred, 72.125, 200, 95 We are going to read such files, with some simplifications: 1. For the first line, the Values" will be strings [obviously, these strings can't include commas - why?) 2. For the subsequent lines in the file, the "values" will all be all be parseable as doubles, EXCEPT the first value: it will be a String (Think about it, that string can't include a comma, because it would be taken as the comma to separate one value from another). 3. Every line will have exactly the same number of "values, or as they are usually called, "fields. That is, each line will have exactly the same number of commas on it. When your program starts up. you may find it convenient to have button to "Start" or "Find File. When the user presses the button, use a JFileChooser to allow the user to open a .csv fle. Bonus 5 points: figure out how to limit the dialog to show only csv files. When the user has identified the file, read it into a string. Find a method in the String class to split the string into substrings that were separated by commas. The length of the array returned by this method will tell you how many fields (the number of commas is one less than the number of fields, no?) in this, and by rule 3 above, all subsequent lines. When you have that first line, you will then be able to read the rest of the lines. You will want to read all the data into your program. Make sure you have a second class in your program. It can be in a separate file of its own, or as a "private inner class" in your main .java class. Either way, your class should contain two fields: a String for the name, and an array of doubles. You may find it convenient to use Eclipse to build getter and setter methods for that field, and you may also find it convenient to copy some of the code from the setter into a constructor that takes an array of doubles as an argument. Read each subsequent line of the file. Use your splitting technique to split each line into an array of strings. Each string can then be parsed into a double. Be sure to use the trim) methods to remove any spaces at the beginning or end of each of the substrings. You will want to do the trimming and parsing in a loop - your code can "remember" how many fields to expect for each line, from the splitting and parsing of the first line. When each line has been read and converted to an array of doubles, store those doubles into an object in the second class described above. Then store that object into an ArrayList of those objects. The ArrayList is very nice for this, since it stores objects, and doesn't have to "know in advance" how many to allocate. You just use the "add(...." method to put the objects into the ArrayList. When you get to the end of the file (You will know because the 'readLineO method will return anur when you try to read past the end of the file, and you can test this and use it to "break" out of the read loop), you will then produce your output. First put your output into a multi-line string. That is simply a string like: String s "first line nsecond line nthird line" where the lines are ended with n" newline characters. It is useful when building a multi-line string like this to repeatedly add to a string like this: s+"another linen" When you have built your report (see below), use the .setTextis) method of a JTextArea control on your window to display the report. The report should look very much like this: Read and processed 2 lines in file Cusers dave documents testdoc.csv. There were 3 fields in this file average height was: 69.4625, max was Fred average weight was: 194.25, max was Fred average score was: 100, max was George Bonus 5 points, put the JTextArea into a scrolling container, so if there are more lines in the report than can fit in your area, the user will be able to scroll through an see all the lines. While it would be possible to get this report without storing all of the data in an array or ArrayList, other operations like calculating a standard deviation would require scanning through the data multiple times, so I require you to store the entire contents of the file before calculating the averages

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions