Java Method definition convert letter grade to number

Question

We would like to write a program that prompts the user to enter a letter grade A, B, C, D, or F.

Display its corresponding numeric value 4, 3, 2, 1, or 0.

Here is a sample run:

Enter a letter grade: B 
The numeric value for grade B is 3 

Enter a letter grade: T 
T is an invalid grade 
import java.lang.reflect.Parameter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a letter grade: ");
        char ch = input.next().charAt(0);
        input.close();//from   w w w. j  a va 2 s.  co m

        if (isValidGrade(ch)) {
            System.out.println("The numeric value for grade " + ch + " " + gradeValue(ch));
        } else {
            System.out.print(ch + " is an invalid input");
        }

    }

    public static boolean isValidGrade(char ch) {
        //your code here
    }

    public static int gradeValue(char ch) {
        //your code here
    }
}



import java.lang.reflect.Parameter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter a letter grade: ");
        char ch = input.next().charAt(0);
        input.close();

        if (isValidGrade(ch)) {
            System.out.println("The numeric value for grade " + ch + " " + gradeValue(ch));
        } else {
            System.out.print(ch + " is an invalid input");
        }

    }

    public static boolean isValidGrade(char ch) {
        ch = Character.toUpperCase(ch);

        return (ch >= 'A' && ch <= 'F' && ch != 'E');
    }

    public static int gradeValue(char ch) {
        ch = Character.toUpperCase(ch);
        switch (ch) {
            case 'A': return 4;
            case 'B': return 3;
            case 'C': return 2;
            case 'D': return 1;
            case 'F': return 0;
                default: return -1;
        }
    }
}



PreviousNext

Related