Menu

(Solved) : Exceptions Aren T Always Errors File Countlettersjava Contains Program Reads Word User Pri Q37933664 . . .

Exceptions Aren’t Always ErrorsFileCountLetters.java contains a program that reads a word from theuser and prints the number of occurrences of each letter in theword. In reviewing the code, note that the word is converted to allupper case first, then each letter is translated to a number in therange 0.25 (by subtracting ‘A’ or decimal 65) for use as an index.See Appendix C for the Unicode character set.1.RunCountLettersandenteraphrase,thatis,morethanonewordwithspacesorotherpunctuation in between. It should throw anArrayIndexOutOfBoundsException, because a non-letter will generatean index that is not between 0 and 25. It might be desirable toallow non-letter characters, but not count them. Of course, youcould explicitly test the value of the character to see if it isbetween ‘A’ and ‘Z’. However, an alternative is to go ahead and usethe translated character as an index and catch anArrayIndexOutOfBoundsException if it occurs. Since you want don’twant to do anything when a non-letter occurs, the handler will beempty. Modify this method to do this as follows:a.Putatry/catchinsidethefirstforloop.b.Addacatchthatcatchestheexception,butdon’tdoanythingwithit.2.Nowmodifythebodyofthecatchsothatitdisplaysausefulmessagefollowedbythecharacter that is not a letter:Not aletter: $//****************************************************************************// CountLetters.java//****************************************************************************importjava.util.Scanner; public class CountLetters {publicstatic void main(String[] args) {int[]counts = new int[26];Scanner scan = new Scanner(System.in);//getword from userSystem.out.print(“n Enter a single word (letters only): “); Stringword = scan.nextLine();//convert to all upper case word = word.toUpperCase();//count frequency of each letter in stringPage 1of 4LabsCSIT210 Introduction to Programmingfor(int i=0; i < word.length(); i++)counts[word.charAt(i)-‘A’]++;//print frequencies System.out.println();for(int i=0; i < counts.length; i++)if(counts [i] != 0)System.out.println((char)(i +’A’) + “: ” + counts[i]);}}Page 2of 4
LabsCSIT210 Introduction to ProgrammingReading from Text FileReviewthe program Warning.java that reads in a file of student academiccredit data. Each line of the input file will contain the studentname (a single String with no spaces), the number of semester hoursearned (an integer), the total quality points earned (adouble).Hereis the students.dat data file:students.datSmith27 83.7Jones21 28.35Walker96 182.4Doe 60150Wood100 400Street33 57.4Taylor83 190Davis110 198Smart75 292.5Bird84 168Summers 52 83.2Theprogram should compute the GPA (grade point or quality pointaverage) for each student (the total quality points divided by thenumber of semester hours), and display the student name if the gpais less than 2.0. The file Warning.java contains a skeleton of theprogram. Do the following:1. Addthe 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. Addcode to catch the following exceptions:• AFileNotFoundException if the input file does not exist.Howwill you test for this exception?• ANumberFormatException if it can’t parse an int or double when ittries to – this indicates an error in the input file format.Howwill you test for this error?Display the record number in error.Page 3of 4
LabsCSIT210 Introduction to Programming//****************************************************************************//Warning.java//****************************************************************************importjava.util.Scanner;importjava.io.*;publicclass Warning{publicstatic void main (String[] args)throws IOException{}}intcreditHrs;doublequalityPts;doublegpa;Stringname;//number of semester hours earned//number of quality points earned//grade point (quality point) average// Setup scanner to input fileScanner inFile = new Scanner(new File(“c:students.dat”));System.out.println (“n Students on Academic Warningn”);while(inFile.hasNext()){// Getthe 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// andstatement to determine if the student name is listed.}//insert catch statements

Expert Answer


Answer to Exceptions Aren T Always Errors File Countlettersjava Contains Program Reads Word User Pri Q37933664 . . .

OR