CSE 201 - Lab 7 - File I/O


Note: This lab assumes that you have completed lab 1, and that you know how to login to your account, and how to open, edit, save your files, as well as compile and run your programs. If you have forgotten any of these techniques, refer back to the lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs do not have the appropriate comments included.


Table of Contents


Objective

The objective for this lab is to write a Java program to practice the use of Java file I/O. There is a lot going on in this lab. To cope with the complexity of the task, it is essential that you use appropriate class methods to break up your solution in smaller, more manageable, parts. Monolithic solutions that do not exhibit a good design using appropriate class methods will not receive full credit.


Overview

You will be writing a Java program from scratch. The program will read several series of heart rates from a file, compute minimum, maximum, and fitness quotient for each series, and output the input data and computed info in an HTML document. See Description below for the details.


Materials Provided

For this lab you will need the following three files:


Set up

Start Eclipse, create a new project, Lab7, and create a new Lab7 class (the corresponding file name will be Lab7.java) in the project. Import into your Lab7 project the following files from K:\CSE201\Lab7 (see Lab 1 for instructions on how to import files in Eclipse):


Description

You have been hired by a medical research group, led by the famous cardiologist Dr. Angie O'Plasty, that has developed something they call the fitness quotient. The idea is to have a person walk on a treadmill for several minutes and to take his/her heart rate measurement a couple times a minute. This leads to a series of heart rates, e.g., <60,66,72,75,72,74>. The person's fitness quotient is twice the smallest heart rate in this series, divided by the sum of the smallest and largest heart rates. (Notice that neither the duration of the test nor the frequency of heart rate measurements is important, and in general these may vary from test to test and from individual to individual without affecting the fitness quotient.) For example, the individual whose heart rates are those in the above example would have a fitness quotient of 0.889 = (2*60)/(60+75).

The medical research group is interested in a program that will read a file containing heart rate measurements for several anonymous individuals, and will create from it an HTML file that looks like this when viewed in a browser:
 
Fitness Quotient Minimum Heart Rate Maximum Heart Rate Heart Rate Series
0.919 68 80 68, 69, 76, 78, 77, 77, 78, 80
0.887 67 84 67, 69, 78, 83, 83, 84, 79, 82, 79, 84, 83, 82, 83, 84, 79, 83, 80, 78, 84, 84
0.894 63 78 63, 71, 73, 78, 76

The program will perform the following actions:

  1. Ask the user to enter the name of the input file containing the heart rate measurements, and input the file name;
  2. 
    
  3. Ask the user to enter the name of the output file where the output HTML document will be saved, and input the file name;
  4. 
    
  5. Open the input file by using a Scanner object (see Input file format for a description of the format of the input file);
  6. 
    
  7. Open (create) the output file by using a PrintWriter object (see Output file format for a description of the format of the output file);
  8. 
    
  9. Output to the file the appropriate opening HTML tags;
  10. 
    
  11. Output to the file the header row of the table;
  12. 
    
  13. For each series of heart rates in the input file, do the following:
      
      
    1. Input the series of numbers from the input file and store them in an array;
    2. 
      
    3. Compute the minimum, maximum, and fitness quotient for the series;
    4. 
      
    5. Output one row of the HTML table to the output file, with the fitness quotient, minimum and maximum heart rates, and the list of heart rates;
  14. 
    
  15. Output to the file the appropriate closing HTML tags;
  16. 
    
  17. Close the input and output files.

Make sure you handle all possible IOException exceptions generated by the file I/O calls.

Note: If you have any problems or questions make sure you ask your instructor as soon as possible.

Input File Format

The input file will contain heart rate measurements for several anonymous individuals in the following format:
    <number of heart rate series>
    <number of heart rate measurements for first individual>
    <heart rate 1>
    <heart rate 2>
    <heart rate 3>
    ...
    <number of heart rate measurements for second individual>
    <heart rate 1>
    <heart rate 2>
    <heart rate 3>
    ...
    ...
where the first line <number of heart rate series> is the number of heart rate series in the input file; after that, <number of heart rate measurements for first individual> is the length of the first heart rate series, which is followed by the corresponding number of heart rate measurements, one per line of the input file. Each following heart rate series follows the same format. See sample-input.txt and many-heart-rates.txt for sample input files in this format. Note that your solution must work with an input file following this format but with an arbitrary number of heart rate series.

Output File Format

The output file format will be HTML (hyper-text markup language) which is the simple markup language that Netscape and other web browsers interpret to render web pages.

HTML is composed of tags. HTML tags are always enclosed in angle-brackets ( < > ). Tags typically occur in begin-end (or open-close) pairs. These pairs are in the form

   <tag>  ...  </tag>
where the <tag> indicates the beginning (opening) of a tag-pair, and the </tag> indicates the end (close). The three dots indicate an arbitrary amount of content between the tags.

Specifically, an HTML document starts with the <html> tag and ends with the </html> tag. Inside these tags, you usually find a <body> ... </body> pair of tags that define the body of the HTML document.

So the general structure of your program's output will be the following:

<html>
<body>
...
</body>
</html>

The body of the HTML output file will contain a table. A table is enclosed in a <table> ... </table> pair of tags. Between these tags you need to list the rows of the table. Each row is enclosed in a <tr> ... </tr> pair of tags. Between these tags you need to list the elements in the row of the table. Each element is enclosed in a <th> ... </th> pair of tags, for header elements, or in a <td> ... </td> pair of tags, for normal data elements.

That's all you need to know to generate the output in this lab assignment. Of course, HTML has a lot more tags, and you should explore the language if you are interested. Here is a skeleton of an HTML file in the format expected to be produced by your program, where the ... represent other elements in a row or other rows:

<html>
<body>
<table border="1">

<tr>
<th>
header 1
</th>
<th>
header 2
</th>
...
</tr>

<tr>
<td>
data 1
</td>
<td>
data 2
</td>
...
</tr>

...

</table>
</body>
</html>

See sample-output.html for a sample output corresponding to the sample-input.txt input. You may want to open this HTML file in a text editor (e.g., Eclipse) to see the source HTML instead of the rendered version shown by a browser. This sample shows how you can force the table to be displayed with a border by using the border attribute of the <table> tag, i.e., <table border="1">, or how you can force all the elements or selected elements of a row to be centered.


Lab Submission

Make sure your program compiles and runs correctly before submitting. To submit, create a single zip file (lab7.zip) containing the Lab7.java file from the Lab7 project (the location of the project in the file system will be z:\eclipse\workspace\Lab7) and then upload that zip file to the Carmen dropbox for Lab 7. If you don't remember how to create a zip file see Lab 1 for instructions.