Lab 11 (Year 3 Sem 1)

Lab 11 is the twelfth lab in semester 1 of year 3, and the twelfth lab in the total of year 3. It is a lab for file i/o, and is considered to be moderately hard.

Description
No description was provided for this lab.

Unlocked after completion

 * OCD (Score at least 60% for Lab 11: File I/O)
 * Ins & Outs (Score at least 80% for Lab 11: File I/O)

Question
The file random.txt contains a long list of random numbers. Add in relevant code in method processFile(String filename) to achieve the following:  Open the file with filename given as an input parameter. Read all the numbers from the file, and calculate the following: how many numbers are in the file the total sum of all numbers in the file the average of all numbers in the file (formatted in 2 dp)     <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">Return a String in the following format: &lt;count&gt; &lt;sum&gt; &lt;average&gt; e.g. "200 105527 527.64"</li> <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">You are required to use try-catch to handle the IOException. If an Exception is met, return the String "Error in file reading."</li> </ul> <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Note that your program should work even if another text file is passed in. <b style="background-color: rgb(255, 255, 0);">Do not forget to close your file.</b>

Solution
todo

Question
<p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">The file Lincoln.txt contains some sentences. <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Add in relevant code in method processFile(String filename) to achieve the following:  <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">Open the file with filename given as an input parameter.</li> <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">Read all the sentences from the file, and calculate the following: <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">the number of sentences in the file (you may assume a valid sentence will only end with a full-stop (.) )</li> <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">the number of words in the file (you may assume a word will always be separated by spaces, comma, or a full-stop)</li> <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">the number of characters in the file (not counting the spaces, commas and full-stops)</li> </ul> </li> </ul>  <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">Return a String in the following format: &lt;numSentences&gt; &lt;numWords&gt; &lt;numCharacters&gt; e.g. "10 270 1148"</li> <li style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;">You are required to use try-catch to handle the IOException. If an Exception is met, return the String "Error in file reading."</li> </ul> <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Note that your program should work even if another text file is passed in. <b style="background-color: rgb(255, 255, 0);">Do not forget to close your file.</b> <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">  <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Hint: You should use .split method in String class to tokenize the data. <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">For example, the following code can be used to tokenize a String named line, with data delimited by | or ; or space. <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">e.g. String[] tokens = line.split( <span style="font-family: Consolas; color: rgb(0, 128, 0); ">"[|; ]+" );

Solution
todo

Question
<p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Write a program to read in a file: <font face="Courier New">person.csv. The file <font face="Courier New">person.csv contains three pieces of information per line: <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; "><font face="Courier New">lastname (i.e. surname), firstname, gender <div style="font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; "> In Computing, a comma-separated values (CSV) file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. Comma is use as delimiter by default. However, it is possible to format a record with other delimiter such as #, space, etc. Using the Person class (Person.java) provided, read line by line the content in <font face="Courier New">person.csv and create two ArrayList of Person objects, one to store male person instances, and one to store female instances from the data loaded from person.csv. Each Person object should hold one line of record. You are to sort the ArrayList of Person objects according to the firstname attribute. Relevant code to allow this sorting has already been coded in Person.java. You may sort the ArrayList of Person directly by calling Collections.sort method as described in the notes. You are to save the Person objects in the given static ArrayLists for autograding purposes. <b style="background-color: rgb(255, 255, 0);">Do not forget to close your file.</b> Sample Output: [M1) Aaamir Khan, M2) Ahmad Mahmud , M3) Akira Kurosawa , M4) Albert Remy ..., M177) William Wyler , M181) Yimou Zhang , M168) Yin Tse , M179) Yoshio Inaba ] [F1) Agnes Moorehead , F2) Alisa Frejndlikh , F3) Anna Paquin , F4) Antonella Attili , F5) Audrey Tautou ... , F73) Virginia Cherrill , F74) Winai Kraibutr , F76) Xiang Yun , F77) Ziyi Zhang ]

Solution
todo

Question
<p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Write a program that allows user to type in the name of 2 input files (e.g. <font face="Courier New">data1.txt and <font face="Courier New">data2.txt ). <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">You may assume the data in these files are numbers between 1 – 999. <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Your program should sort the numbers in the two files in ascending order and write into: <font face="Courier New">data1sorted.txt and <font face="Courier New">data2sorted.txt, respectively. Finally, combine the data from both <font face="Courier New">data1.txt and <font face="Courier New">data2.txt to produce a sorted merge file named <font face="Courier New">merge.txt. <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; "><b style="background-color: rgb(255, 255, 0);">Do not forget to close your file.</b> <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; ">Sample Output: <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; "><font face="Courier New">Content in 'data1sorted.txt': <font face="Courier New">30 38 39. . 980 991 997 <font face="Courier New">Content in data2sorted.txt: <font face="Courier New">14 32 40. . 983 991 997 <font face="Courier New">Content in merge.txt: <font face="Courier New">14 30 32. . 991 997 997 <p style="margin-bottom: 10px; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; "> ﻿Hint: You may want to read up more about the MergeSort algorithm for this question.

Solution
todo