-2

I need help with my assignment.I need to write class program that tranlates grade into grade point. If the grade have + like A+ it will increase the grade point by 0.3 and - will decrease by 0.3.

private static final double GradePoint = 0;
private static Scanner input;

public static void main(String [] args)
{
    String grade ;      
    double GradePoint = 0;

    System.out.print("Please enter your grade:  ");
    input = new Scanner(System.in);
    grade = input.nextLine();

    switch(grade)
    {
        case "A": 
        case "a": GradePoint = 4; break;
        case "B":
        case "b": GradePoint = 3; break;
        case "C":
        case "c": GradePoint = 2; break;
        case "D":
        case "d": GradePoint = 1; break;
        case "F":
        case "f": GradePoint = 0; break;
    }    

    System.out.print("Your grade is: "+GradePoint);
}

public double getGradePoint(String grade)
{
    return GradePoint;

}

What i dont understand is about how to use the method to calculate.I'm still beginner.

Beginner
  • 3
  • 3
  • @Mohd Khairul: Just check the second charecter. You have methods to convert a string to array. and check the second char to match + or - isnt that what you want?? – kidd0 Nov 09 '12 at 06:51
  • Homework tag? Also, to search for the "+" you can use this method called indexOf. Example: if(str1.indexOf("#") > -1) { // # was found in str1 }. – Vineet Kosaraju Nov 09 '12 at 07:16
  • You have a pal who has the same assignment. Look here: http://stackoverflow.com/questions/13286631/java-variable-not-initialized – maba Nov 09 '12 at 07:35

2 Answers2

1
I have to use CLASS and method*public double getGradePoint(String grade)* to
return the grade point of grade entered.

You need to shift your entire code from main() to getGradePoint(String grade);

also your switch case switch(grade) will not work for values like "A+" as there are no such case that matches the string "A+"

Bhavik Shah
  • 5,125
  • 3
  • 23
  • 40
0

I was bored and had nothing better to do so here :)

public class GradeCalculator
{

    public static void main(String[] args)
    {
        System.out.print("Please enter your grade:  ");
        Scanner input = new Scanner(System.in);
        String grade = input.nextLine().trim();

        GradeCalculator calculator = new GradeCalculator();

        double gradePoint = calculator.getGradePoint(grade);
        System.out.print("Your grade is: " + gradePoint);
    }

    private double getGradePoint(String grade)
    {
        int score = getGradeScore(grade.charAt(0));

        double modifier = 0;

        if (grade.length() > 1)
        {
            modifier = getModifierValue(grade.charAt(1));
        }

        return score + modifier;
    }

    private int getGradeScore(char grade)
    {
        int score = 0;

        switch (grade)
        {
            case 'A':
            case 'a':
                score = 4;
                break;
            case 'B':
            case 'b':
                score = 3;
                break;
            case 'C':
            case 'c':
                score = 2;
                break;
            case 'D':
            case 'd':
                score = 1;
                break;
            case 'F':
            case 'f':
                score = 0;
                break;
        }

        return score;
    }

    private double getModifierValue(char modifier)
    {
        double value = 0;

        switch (modifier)
        {
            case '+':
                value = 0.3;
                break;
            case '-':
                value = -0.3;
                break;
        }

        return value;
    }
}
The Cat
  • 2,375
  • 6
  • 25
  • 37
  • This is most helping me. Maybe i need to modified a little bit the code. Like A= must be result 4.0 not 4.3. Thanks my friends. – Beginner Nov 09 '12 at 12:54