-
September 13th, 2010, 06:53 PM
#1
Debugging Help(Homework)
The homework is to find the 10 errors and fix them and make a comment saying how you fixed it.
It complies now just have to find the 10 errors.
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* A class designed to help a teacher processes grades. It provides methods
* for converting a numeric grade into a letter grade, for generation an
* appropriate message to the student based on the letter grade and a report
* that includes all three items.
*/
public class GradeReporter
{
// The limit is the inclusive lower limit for each letter
// grade -- this means that 89.5 is an 'A' not a 'B'
public static final int A_LIMIT = 90;
public static final int B_LIMIT = 80;
public static final int C_LIMIT = 70;
public static final int D_LIMIT = 70;
/** Converts a numeric grade into a letter grade. Grades should be rounded to
* nearest whole number
*
* @param a numeric grade in the range of 0 to 100
* @returns a letter grade based on the numeric grade, possible grades are A, B, C, D and F.
*/
public char letterGrade(double numberGrade)
{
char letterGrade = 'A' ;
int grade = (int)numberGrade;
if (grade >= A_LIMIT)
letterGrade = 'A';
else if (grade >= B_LIMIT)
letterGrade = 'B';
else if (grade > C_LIMIT)
letterGrade = 'C';
else if (grade >= D_LIMIT)
letterGrade = 'D';
return letterGrade;
}
/** Prepares a message appropriate to a particular letter grade
* @param letterGrade a letter grade, known grades are A, B, C, D and F.
* @returns a message to the student based on the grade.
*/
public String message(char letterGrade) {
char grade= 'A';
String message = "";
switch(grade) {
case 'A':
message = "Excellent!";
break;
case 'B':
message = "Very Good.";
case 'C':
message = "Average.";
case 'D':
message = "You are in danger of failing the course.";
break;
case 'F':
message = "Please see me.";
break;
default:
message = "unknown grade";
}
if (letterGrade == 'A' && letterGrade == 'B')
message += " Keep up the good work.";
if (letterGrade == 'C' && letterGrade == 'D')
message += " Please try harder.";
message += " You have room to improve.";
return message;
}
/** Prepares a report from a numeric grade.
* @param numGrade a numeric grade in the range of 0 to 100
* @returns the numeric grade, its letter grade and a message
*/
public String report(double numGrade) {
return "Grade: " + numGrade + " " + letterGrade(numGrade)
+ " " + message(letterGrade(numGrade));
}
/** User repeatedly inputs numeric grades and the program prints out the
* grade, the letter grade and a comment until the user enters input
* beginning with the letter Q.
*
* It uses something called a "regular expression" to confirm that
* the input is numeric before attempting to convert the String input
* into a double. This is a powerful tool, and althought not covered
* in this class, it can be very useful to learn.
*/
public static void main(String[] args) {
GradeReporter reporter = new GradeReporter();
Scanner console = new Scanner(System.in);
String input = "";
String NUMERIC_ONLY = "[+-]?\\d*\\.?\\d+"; // a regular expression pattern
// you would read this as: an optional sign ([+-]?), followed by 0 or more digits (\\d*),
// followed by an optional decimal point(.?), followed by one or more digits(\\d+).
//Keep repeating until the user enters a q.
do {
System.out.print("Enter grade(Q to quit): ");
input = console.nextLine();
// if input is numeric, convert it to double and print the
// report that reporter generates
if (Pattern.matches(NUMERIC_ONLY, input)) {
double numGrade = Double.parseDouble(input);
System.out.println(reporter.report(numGrade));
}
} while (input.toLowerCase().startsWith("q") );
System.out.println("Good bye.");
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|