Lab 14


Objectives

The objectives of this lab are

Worksheet

Take out a clean sheet of paper.  In the upper right hand corner print your name, your esus e-mail address, your lab TA's name, and your lab section time (e.g., 2:00-4:00).

Levels

All students must do everything listed below.  The one exception is that advanced students who zip through the first part should in addition work on the advanced part given in the Advanced section below.

Terminology

Give a description for the following terms (chapter 14 will help):

Java Exceptions

Construct a new BlueJ project.  Make one class called Main with the following code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main
{
  public static void main (String[] args) 
  {
      String fileName = "";
      BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
      FileReader inputFile;

      System.out.print("Please enter the name of the data file: ");
      fileName = console.readLine();
      inputFile = new FileReader(fileName);
      BufferedReader input = new BufferedReader(new FileReader(fileName));
     
      int numberOfElements = Integer.parseInt(input.readLine());
      int sum = 0;
      
      for (int i = 1; i <= numberOfElements; i++)
      {
          sum = sum + Integer.parseInt(input.readLine());
      }
      System.out.println();
      System.out.println("The value in sum is " + sum);
      System.out.println("The average is      " + sum/numberOfElements);
      input.close();
      System.out.println(); System.out.println();
  }
}

What Does this Program Do?

Look at this program.  Describe on your worksheet what this program does.

Compile this Program

The program will not compile as is.  Discover what the problem is and make the simplest possible fix to the program so that it does compile.  On your worksheet write down what your fix was.

 

Run 1

Using Notepad, construct a data file that looks like

5
1
2
3
4
5

Save this file in the project directory where the above Main class is with the name data1.txt.

Run the program on data1.txt.  Write on your worksheet what is printed.

Run 2

Run your program again.  This time type FRED for the input file name.  On your worksheet describe what happens

Run 3

Using Notepad, construct a data file that looks like

0

That is, the file consists of just one line that contains a zero.  Name the file data2.txt and save it in this project.

Run your program on on data2.txt.  Describe on your worksheet what happens.

Modify the Program

Copy this program into a new class called Main2 in this same project.  Be sure to rename the class Main2.  Modify the program using exceptions to do the following:

1.  Create a loop that continuously prompts the user to type in a file name until an existing filename is typed (thus getting rid of the problem of Run 2 above).

Run 4

Run your program again and type FRED.  Does the program work as expected?  Describe on your worksheet.

2.  Make sure that the problem of Run3 on file data2.txt does not crash your program.  Instead, your program should print out

Average not computed because a divide by zero was attempted.

Run 5

Run your program again on file data2.txt.  Does it work as required?  Describe on your worksheet.

Advanced

Modify the program of this lab to eliminate all negative numbers from the computation of sum.  Rather than using an if statement (which would be the best way to do this) use an exception named NegativeNumber that you declare of your own that is thrown any time a negative number is read in.  Try your program on negative numbers.  Describe what is printed on a file that has input

6
4
-5
4
10
-3
5

To Turn In