Question: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; public class Lab3 { public static final int SIZE = 5; public static void main(String[] args) {

import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException;

public class Lab3 { public static final int SIZE = 5;

public static void main(String[] args) { double[] data; int index; Scanner input;

data = new double[SIZE]; index = 0;

// create this file -- determine the appropriate location for it via experiment (hint: it will be somewhere within your NetBeansProjects folder) input = new Scanner(new File("input.txt"));

while(input.hasNext()) { data[index] = input.nextDouble();

System.out.println(data[index]); index++; } } }

What to submit (replace "lastname" with your actual last name): lastname_Lab3.java (up to 10 points may be deducted for submitting anything else, or not submitting these files, or naming them differently). Additional deductions (also up to 10 points) may be made for poor code style: lack of program headers, lack of methods headers, lack of in-code block-level commenting).

Set up: Download Lab3.java and import it into a blank NetBeans project (or if you're using a different IDE, import/open it in whichever manner is appropriate for that IDE). Rename the file in accordance with the instructions above (in the "What to submit" section). You will also have to change the class name in the file accordingly -- recall that in Java, the file name and class name must be identical.

The above code is what's in Lab3.java

Requirements:

1) Create the file input.txt (30 points):

  • Determine the appropriate location for the file. Hint: If using NetBeans, it will be somewhere within your NetBeansProjects folder (10 pts)
  • Name the file "input.txt" (10 pts).
  • Study the code in Lab3.java to determine what the content of this file should be. Add the appropriate content (note that this is not exact, rather it's more like "what kind of content would be appropriate?"). Run the code to make sure your input file's content is being printed out appropriately by the program (10 pts).

2) Exception Handling Addition 1: try adding some incorrect content to the file (something that would generate a crash, because the program is expecting to encounter a particular type of data). Then add exception-handling code to make sure the program doesn't crash. How exactly you choose to handle this exception is up to you, the main requirement for credit for this is that your program doesn't crash (35 points).

3) Exception Handling Addition 2: try moving the file to a different (incorrect) directory to generate a "file not found" exception/crash. Then add exception-handling code to make sure the program does not crash when the file isn't present in the expected location. As with (2) above, how exactly you choose to handle the exception is your call, the only requirement is "no crash" (35 points).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!