CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2010
    Posts
    2

    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.");
    }
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Debugging Help(Homework)

    What is your question? What problems are you having that you need help with?


    Please edit your code and surround it with code tags to preserve the formatting. Here's what code tags are: http://www.java-forums.org/misc.php?do=bbcode#code
    Norm

  3. #3
    Join Date
    Sep 2010
    Posts
    2

    Re: Debugging Help(Homework)

    I am having problems with the every run the following:
    The letter grade coming out to A from:
    public char letterGrade(double numberGrade)

    and the message saying Excellent from:
    public char letterGrade(double numberGrade)

    then the program does not loop to keep asking for a next grade. It just goes straight to the Good Bye message from:
    public static void main(String[] args)

    Quote Originally Posted by LordRazon View Post
    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.

    Code:
    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.");
        }
    }

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Debugging Help(Homework)

    Add some System.out.println() statements to show how the variables values change and how the execution flow goes.
    Norm

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
  •  





Click Here to Expand Forum to Full Width

Featured