The objectives of this lab are
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).
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.
Construct a new BlueJ project. Make one class called Main with the following code (cut and paste it):
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();
}
}
Look at this program. Describe on your worksheet what this program does. Take a guess.
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. The problem will have to do with Exceptions, the BlueJ compiler will tell you what line will throw what type of exception, and you must include the try/catch so the program will compile. Remember any variables declared inside a try or catch will only be local to that block of code. You need to decide to make your try/catch blocks bigger, or declare your variables outside the block of code.
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 your program again. This time type FRED for the input file name. On your worksheet describe what happens
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.
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 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.
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