PLEASE DO NOT COPY OTHER EXPERTS' ANSWERS. THEY DO NOT WORK. ABuggyProgram.java import java.io.*; import java.util.*; pu

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

PLEASE DO NOT COPY OTHER EXPERTS' ANSWERS. THEY DO NOT WORK. ABuggyProgram.java import java.io.*; import java.util.*; pu

Post by answerhappygod »

PLEASE DO NOT COPY OTHER EXPERTS' ANSWERS. THEY DO NOT WORK.
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 1
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 1 (138.78 KiB) Viewed 42 times
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 2
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 2 (118.08 KiB) Viewed 42 times
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 3
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 3 (104.59 KiB) Viewed 42 times
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 4
Please Do Not Copy Other Experts Answers They Do Not Work Abuggyprogram Java Import Java Io Import Java Util Pu 4 (112.29 KiB) Viewed 42 times
ABuggyProgram.java
import java.io.*;
import java.util.*;
public class ABuggyProgram{

public static void openFile()throws
FileNotFoundException{
// attempt to open a file that doesn't
exist

String fileName =
"importantFile.docx.java.cwu";
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
System.out.println("The first line of
myFileFile is: "+
inputFile.nextLine());


}

public static void parseNumber(){
// parsing an integer "String"
String str = "Hello World";
int intFormOfStr =
Integer.parseInt(str);
}

public static void readPoorlyFormattedFile()throws
FileNotFoundException{
// accessing a file with doubles
String fileName =
"myDataFile.txt";
File file = new File(fileName);
Scanner inputFile = new
Scanner(file);
double sumOfDoubleVals = 0.0;
while (inputFile.hasNext()) {
sumOfDoubleVals +=
inputFile.nextDouble();
}
}

public static void createCarArray(){
// invoking methods on null
objects
Car[] cars = new Car[100];
for(int i=0; i<cars.length;
i++){
System.out.println("car
"+i+" make :"+cars.getMake());
}
}

// main method
public static void main(String[] args) throws
IOException {

int userInput = 0;

do{
// ask what feature
should be tested
Scanner keyboard = new
Scanner(System.in);

System.out.println("\nWhich 'error' would you like to test:
");
System.out.println(" Press
0 to exit the program.");
System.out.println(" Press
1 to test FileNotFoundException");
System.out.println(" Press
2 to test NumberFormatException");
System.out.println(" Press
3 to test InputMismatchExcpetion");
System.out.println(" Press
4 to test NullPointerExcpetion\n");

userInput=
keyboard.nextInt();

switch (userInput) {
case
0:

break;
case
1:

openFile();

break;
case
2:

parseNumber();

break;
case
3:

readPoorlyFormattedFile();

break;
case
4:

createCarArray();

break;

default:

System.out.println("Bad selection. Try again!");
}

}while(userInput != 0);
}

}
.
Car.java
public class Car{

//fields
private String make;
private String model;
private String color;
private String licenseNumber;
private int minutesParked;

public Car(String carMake, String carModel, String
carColor, String
carLicenseNum, int carMinutesParked){
make = carMake;
model = carModel;
color = carColor;
licenseNumber = carLicenseNum;
minutesParked = carMinutesParked;
}


public Car(Car car2){//copy constructor
make = car2.make;
model = car2.model;
color = car2.color;
licenseNumber = car2.licenseNumber;
minutesParked = car2.minutesParked;
}


public void setAmountOfMinutesParked(int
minutesPark){
minutesParked= minutesPark;
}


//getter methods
public int getMinutesParked(){
return minutesParked;
}


public String getMake(){
return make;
}


public String getModel(){
return model;
}


public String getColor(){
return color;
}


public String getLicenseNumber(){
return licenseNumber;
}


public String toString(){//toString method
String aString = "\n"+"Make:
"+make+"\n"+"Model: "+model+"\n"+"Color:
"+color+"\n"

+"License Number:
"+licenseNumber+"\n"+"Minutes Parked:
"+minutesParked+"\n";
return aString;

}

}
myDataFile.txt
846.9
- 0.98
1566.41
16.02
136.98
44.02
846.9
- 0.98
- 55.41
0B.02
1546.77
1326.98
This lab is meant to further your understanding of exceptions, and give you hands-on experience with try-catch blocks. I. Introduction We've discussed in lecture, how to catch exceptions, so that your program exits gracefully'. Catching exceptions also allows you to write programs that generate an appropriate response, based on the error that is thrown. In this lab you are given a buggy code, to which you will add try-catch blocks, so that the program does not crash when it encounters an error. To get started download the following files from Canvas: ABuggyProgram.java • Car.java • myData File.txt Open the java file and explore it, have a look at the switch and the methods being called inside of the cases. Compile and run the program. Right now the code does not handle any exceptions, when you call any of the methods, the program will crash and print out the stack. The java program ABuggy Program has four intentional errors. Your goal is to catch those errors, so that the program does not crash. II. Handling Errors of type FileNotFoundException You will add a try-catch clause to the method named openFile(). First run the code and select the option number 1, the program will crash. Explore the stack printed out, notice the line where the program encounter a problem. The error message will tell you which exception has been generated, you can check the Java API to get a better understanding about the specific exception. Do the following to handle this exception. 1. Wrap all of the code inside of the method named openFile() with a try clause. Have a look at code snipped below, all of the code in the method should be part of the "try code block”. 2. Add a single catch statement. The exception type being caught must match that one being throw inside of this method. The exception should be FileNotFoundException, and the reference variable name can be just about anything you want. Inside of the "catch code block” add a System.out.println() with the message *****File can't be open, it does not exist. ****". You can also print out the message generated by the exception, by calling the method getMessage() on the reference variable of the exception. Add another print statement and call the method inside of it. 3. Remove the "throws FileNotFoundException" from the method header, compile and run the program. The program should not crash anymore when option number 1 is selected and the method named openFile() is called. try{ //try code block }catch(Exception Type referenceVariable) { //catch code block }

III. Handling Errors of type NumberFormatException Run the code and select the option number 2, the program will crash. Explore the stack printed out, notice the line where the program encounter a problem. The error message will tell you which exception has been generated, you can check the Java API to get a better understanding about the specific exception. Do the following to handle this exception. 1. Similarly to the last part of the lab, put all of the code inside of the method named parseNumber(), into a try clause. 2. Add a single catch statement. The exception being caught should be Number FormatException, and the reference variable name can be just about anything you want. Inside of the "catch code block” add a System.out.println() with the message ***** A conversion error happened****”. 3. Compile and run the program. The program should not crash anymore when option number 2 is selected and the method named parseNumber() is called. IV. Handling Errors of type InputMismatchException Run the code and select the option number 3, the program will crash. Explore the stack printed out, notice the line where the program encounter a problem. 1. Similarly to the last part of the lab, put all of the code inside of the method named readPoorlyFormatted File(), into a try clause. 2. Inside of this method, there will be two separate exception which you will need to catch Have a look at the code snippet below. First add a catch statement which can catch an exception of type FileNotFoundException. Inside of the "catch code block” add a System.out.println() with the message ***** File can't be open, it does not exist. *****". Exactly like you did in the previous part of this lab. 3. Secondly, add a catch statement which can catch an exception of type InputMismatchException. Inside of the “catch code block” for this catch clause add a System.out.println() statement. In the print statement put the message "****Input was incorrect****”. 4. Compile and run the program. The program should not crash anymore when option number 3 is selected and the method named readPoorlyFormatted File () is called.

try{ //try code block }catch(Exception Type el){ //catch 1 code block System.out.println(e1.getMessage()); }catch(Exception Type e2){ //catch 2 code block } CSI Lab 7: Exceptions Page 2 of 4 V. Handling Errors of type NullPointerException Run the code and select the option number 4, the program will crash. Explore the stack printed out, notice the line where the program encounter a problem. This time you will not handle this exception inside of the method where the exception is being throw. Instead you will handle this exception inside of the calling method (main method). 1. Add a try-catch inside of the switch case in the main method. In case number 4, modify the code in such way that the method named createCarArray() which is being called, is part of the "try code block”. 2. Add a single catch statement. The exception being caught should be NullPointerException. Inside of the "catch code block” add a System.out.println() with the message "*****The array has NOT been populated with car objects ****”. 3. The entire try-catch needs to be inside of the case 4 of the switch. The break statement should be after the closing curly bracket of the catch clause. Compile and run the program. The program should not crash anymore when option number 4 is selected and the method named createCarArray () is called. VI. Main method Input MismatchException After you have taken care of all of the exceptions run the program and enter a character or a word instead of a number. An exception should be generated. Have a look at the stack trace and find out which line you have to wrap in a try-catch clause. In the catch statement assign a random number to the userInput, any number that's not 0-4.

VII. Sample Output Sample output of the program can be seen below on Figure 1. ---GRASP exec: java ABuggyProgramsolution Which 'error' would you like to test: Press to exit the program. Press 1 to test FileNotFoundException Press 2 to test Number FormatException Press 3 to test InputMismatchExcpetion Press 4 to test NullPointerExcpetion 3 ****Input was incorrect **** Which 'error' would you like to test: Press o to exit the program. Press 1 to test FileNotFoundException Press 2 to test Number FormatException Press 3 to test InputMismatchExcpetion Press 4 to test NullPointerExcpetion 1 4 ****File can't be open, it does not ist. **** importantFile.docx.java (The system cannot find the file specified) Which 'error' would you like to test: Press to exit the program. Press 1 to test FileNotFoundException Press 2 to test Number FormatException Press 3 to test InputMismatchExcpetion Press 4 to test NullPointerExcpetion ****The array has NOT been populated with car objects. **** Which 'error' would you like to test: Press to exit the program. Press 1 to test FileNotFoundException Press 2 to test Number FormatException Press 3 to test InputMismatchExcpetion Press 4 to test NullPointerExcpetion 2 @ ****A conversion error happened**** ---GRASP: operation complete. Figure 1 : Sample output VIII. Upload your work to Canvas For this lab, make sure that you upload the following file to the Lab 7 assignment in your Canvas account: ABuggyProgram.java Rubric Points 20 20 20 File / Lab Exception FileNotFoundException is handled inside of the open File() method Exception Number FormatException is handled inside of the parseNumber() method Two exceptions are handled inside of the method readPoorlyFormattedFile() Exception NullPointerException is handled inside of the switch in main() method Exception InputMismatchException is handled inside of the main method Code compiles and runs as expected Code is indented and commented 20 10 5 5
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply