Write a program that processes grades by converting number grades to letter grad
ID: 3656442 • Letter: W
Question
Write a program that processes grades by converting number grades to letter grades (we've looked at this before). Inside the an input file is a list of student names and number grades (integers) - one student and one grade per line as in name: grade. For each student and line in the input file write to the output file the student name and the letter grade. For our purposes: A+ = 90-100. A = 80-89. B = 70-79, C = 60-69. D = 50-59, F java question3 Enter input file name: goodgrades.txt Enter output file name: results.txt Processing complete. After running my solution, results . txt has the contents: Aaron Langille:A+| Steve Jobs:A+ Bill Gates:F Dennis Ritchie:A Ken Thompson:B Linus Torvalds:D Mark Zuckerberg:C Consider a file that has invalid grade date - less than 0, greater than 100. a nonnumber entry. Your grade conversion method should throw an appropriate exception of this occurs and your main method should catch it and handle it. Also, have your main method handle grades that are not numbers. Lines with these types of errors should still appear in your output file with an appropriate error message. You must use EXCEPTIONS to get these marks. Test your program using badgrades . txt.Explanation / Answer
package com.cramster.nov16; import java.io.File; import java.io.FileNotFoundException; import java.util.Formatter; import java.util.Scanner; public class question4 { public static void main(String[] args) { Scanner console = new Scanner(System.in); String inputFileName; String outputFileName; System.out.print("Enter Input file Name : "); inputFileName = console.nextLine(); System.out.print("Enter Output file Name : "); outputFileName = console.nextLine(); console.close(); try { Scanner inputFile = new Scanner(new File(inputFileName)); Formatter output = new Formatter(outputFileName); while(inputFile.hasNext()) { String line = inputFile.nextLine(); String[] tokens = line.split(":"); String name = tokens[0]; String grade = tokens[1]; try { int gradeNumber = Integer.parseInt(grade); grade = getgrade(gradeNumber); output.format("%s:%s ", name,grade); } catch(NumberFormatException e) { output.format("%s:%s ",name,"Not a valid Number grade"); } catch(IndexOutOfBoundsException e) { output.format("%s:%s ", name,"Number grade out of Range"); } } inputFile.close(); output.close(); System.out.println("Processing complete"); } catch(FileNotFoundException e) { System.err.println("Error Opening files. Terminating program"); e.printStackTrace(); System.exit(1); } } public static String getgrade(int grade) throws IndexOutOfBoundsException { if(grade < 0 || grade > 100) throw new IndexOutOfBoundsException(); else { switch(grade/10) { case 10: case 9: return "A+"; case 8: return "A"; case 7: return "B"; case 6: return "C"; case 5: return "D"; default: return "F"; } } } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.