Menu

(Solved) : Ccbc Community College Baltimore County Labs Csit 210 Introduction Programming Lab 10 Exce Q37198969 . . .

CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming LAB 10 Exceptions Exceptions ArentCCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming for (int i:0; i < word.length(); İH)CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming Reading from Text File Review the prCCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming /Warning.java import java.util.Scann

CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming LAB 10 Exceptions Exceptions Aren’t Always Errors File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. In reviewing the code, note that the word is converted to all upper case first, then each letter is translated to a number in the range 0.25 (by subtracting ‘A’ or decimal 65) for use as an index. See Appendix C for the Unicode character set 1. Run CountLetters and enter a phrase, that is, more than one word with spaces or other punctuation in between. It should throw an ArraylndexOutOfBoundsException, because a non-letter will generate an index that is not between 0 and 25. It might be desirable to allow non-letter characters, but not count them. Of course, you could explicitly test the value of the character to see if it is between ‘A’ and Z. However, an alternative is to go ahead and use the translated character as an index and catch an ArraylndexOutOfBoundsException if it occurs. Since you want don’t want to do anything when a non-letter occurs, the handler will be empty. Modify this method to do this as follows a. Put a try/catch inside the first for loop b. Add a catch that catches the exception, but don’t do anything with it. 2. Now modify the body of the catch so that it displays a useful message followed by the character that is not a letter: Not a letter: $ /l CountLetters.java import java.util.Scanner, public class CountLetters public static void main(StringD args) int counts new int 26]; Scanner scannew Scanner(System.in); I/get word from user System.out.print(“n Enter a single word (letters only): “); String word-scan.nextLine() //convert to all upper case word word.toUpperCase): //count frequency of each letter in string Page 1 of 4 CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming for (int i:0; i < word.length(); İH) counts[word.charAt(i)-‘A]++; /lprint frequencies System.out.println() for (int i-0; i < counts.length; i++) if (counts i0) System.out.printin((char)i +A)” counts) Page 2 of 4 CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming Reading from Text File Review the program Warning.java that reads in a file of student academic credit data. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double) Here is the students.dat data file students.dat Smith 27 83.7 Jones 21 28.35 Walker 96 182.4 Doe 60 150 Wood 100 400 Street 33 57.4 Taylor 83 190 Davis 110 198 Smart 75 292.5 Bird 84 168 Surmers 52 83.2 The program should compute the GPA (grade point or quality point average) for each student (the total quality points divided by the number of semester hours), and display the student name if the gpa is less than 2.0. The file Warning.java contains a skeleton of the program. Do the following 1. Add the code to calculate the gpa. 2. Display the name if the gpa is less than 2.0 3. Test to ensure the output is accurate 4. Add code to catch the following exceptions A FileNotFoundException if the input file does not exist. How will you test for this exception? A NumberFormatException if it can’t parse an int or double when it tries to – this indicates an error in the input file format. How will you test for this error? Display the record number in error Page 3 of 4 CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming /Warning.java import java.util.Scanner; import java.io.*; public class Warning public static void main (String[l args)throws IOException int creditHrs double qualityPts; double gpa; String name /number of semester hours earned // number of quality points earned // grade point (quality point) average // Set up scanner to input file Scanner inFilenew Scanner (new File(“c:students.dat”; System.out.println (“n Students on Academic Warningin”); while (inFile.hasNext )) // Get the name, credit hours and quality points and // determine if the student is on warning. If so, // display the student’s name. name- inFile.next ( creditHrs Integer.parseInt (inFile.next () qualityPts-Double.parseDouble (inFile.next )); Insert gpa calculation // and statement to determine if the student name is listed inFile.close) /insert catch statements Page 4 of 4 Show transcribed image text CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming LAB 10 Exceptions Exceptions Aren’t Always Errors File CountLetters.java contains a program that reads a word from the user and prints the number of occurrences of each letter in the word. In reviewing the code, note that the word is converted to all upper case first, then each letter is translated to a number in the range 0.25 (by subtracting ‘A’ or decimal 65) for use as an index. See Appendix C for the Unicode character set 1. Run CountLetters and enter a phrase, that is, more than one word with spaces or other punctuation in between. It should throw an ArraylndexOutOfBoundsException, because a non-letter will generate an index that is not between 0 and 25. It might be desirable to allow non-letter characters, but not count them. Of course, you could explicitly test the value of the character to see if it is between ‘A’ and Z. However, an alternative is to go ahead and use the translated character as an index and catch an ArraylndexOutOfBoundsException if it occurs. Since you want don’t want to do anything when a non-letter occurs, the handler will be empty. Modify this method to do this as follows a. Put a try/catch inside the first for loop b. Add a catch that catches the exception, but don’t do anything with it. 2. Now modify the body of the catch so that it displays a useful message followed by the character that is not a letter: Not a letter: $ /l CountLetters.java import java.util.Scanner, public class CountLetters public static void main(StringD args) int counts new int 26]; Scanner scannew Scanner(System.in); I/get word from user System.out.print(“n Enter a single word (letters only): “); String word-scan.nextLine() //convert to all upper case word word.toUpperCase): //count frequency of each letter in string Page 1 of 4
CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming for (int i:0; i

Expert Answer


Answer to CCBC The Community College of Baltimore County Labs CSIT 210 Introduction to Programming LAB 10 Exceptions Exceptions Ar…

OR