if and else if with final variables, program not functioning properly

What I am intending to do is write a program that translates an inputted alpha form of a grade (A, B, C+, etc) into a numerical version. An A+ is considered a 4.0, and a +/- is a +3 or -3. B is a 3.0, C is a 2.0, D is a 1.0, and F is a 0. There is no F+ or F- in this program.

The problem I keep encountering at runtime is that no matter what alpha grade I input the program always seems to return a numerical grade of 0.

At runtime here is what it looks like:

Enter a letter grade: C-

The numerical value is: 0.0

But the numerical value should really be 1.7 in the above case.

Here is the constructor class:

/**

A class that assigns a numerical grade for a given letter grade.

*/

publicclass NumericalGrade

{

/**

Constructs a numerical grade for a given letter grade.

@param grade the grade inputted by the user.

*/

public NumericalGrade(int grade)

{

int n = grade;

}

/**

Gets the proper numerical grade for the letter inputted.

@return the numerical grade.

*/

publicdouble getNum()

{

double n = 0;

if (n == A1)

n = 4.0;

elseif (n == A2)

n = 4.0;

elseif (n == A3)

n = 3.7;

elseif (n == B1)

n = 3.3;

elseif (n == B2)

n = 3;

elseif (n == B3)

n = 2.7;

elseif (n == C1)

n = 2.3;

elseif (n == C2)

n = 2;

elseif (n == C3)

n = 1.7;

elseif (n == D1)

n = 1.3;

elseif (n == D2)

n = 1.0;

elseif (n == D3)

n = 0.7;

elseif (n == F)

n = 0;

return n;

}

publicstaticfinalint A1 = 1;

publicstaticfinalint A2 = 2;

publicstaticfinalint A3 = 3;

publicstaticfinalint B1 = 4;

publicstaticfinalint B2 = 5;

publicstaticfinalint B3 = 6;

publicstaticfinalint C1 = 7;

publicstaticfinalint C2 = 8;

publicstaticfinalint C3 = 9;

publicstaticfinalint D1 = 10;

publicstaticfinalint D2 = 11;

publicstaticfinalint D3 = 12;

publicstaticfinalint F = 13;

private String g;

}

And here is the tester class:

import java.util.Scanner;

/**

A class that tests the NumericalGrade class.

*/

publicclass NumericalGradeTester

{

publicstaticvoid main(String[] args)

{

Scanner in =new Scanner(System.in);

System.out.print("Enter a letter grade: ");

String input = in.next();

int n = 0;

if (input.equalsIgnoreCase("A+"))

n = NumericalGrade.A1;

elseif (input.equalsIgnoreCase("A"))

n = NumericalGrade.A2;

elseif (input.equalsIgnoreCase("A-"))

n = NumericalGrade.A3;

elseif (input.equalsIgnoreCase("B+"))

n = NumericalGrade.B1;

elseif (input.equalsIgnoreCase("B"))

n = NumericalGrade.B2;

elseif (input.equalsIgnoreCase("B-"))

n = NumericalGrade.B3;

elseif (input.equalsIgnoreCase("C+"))

n = NumericalGrade.C1;

elseif (input.equalsIgnoreCase("C"))

n = NumericalGrade.C2;

elseif (input.equalsIgnoreCase("C-"))

n = NumericalGrade.C3;

elseif (input.equalsIgnoreCase("D+"))

n = NumericalGrade.D1;

elseif (input.equalsIgnoreCase("D"))

n = NumericalGrade.D2;

elseif (input.equalsIgnoreCase("D-"))

n = NumericalGrade.D3;

elseif (input.equalsIgnoreCase("F"))

n = NumericalGrade.F;

NumericalGrade newGrade =new NumericalGrade(n);

System.out.print("The numerical value is: " + newGrade.getNum());

}

}

If anyone could please point me in the right direction I would surely appreciate it. Thank you in advance.

Message was edited by:

fairtex

[8618 byte] By [fairtexa] at [2007-11-26 23:39:53]
# 1

That's because the value passed to the constructor of NumericalGrade is a local variable.. when the constructor is finished, the value is no longer stored so when you call getNum(), it simply creates a new variable double n = 0; which is always 0.

Also, your code has lots of repetitive statements in it.. this could be improved as well.

Simeona at 2007-7-11 15:06:02 > top of Java-index,Java Essentials,New To Java...
# 2

That is because you have declared n inside the method getNum() and it's never changed. You should make grade a "class variable" so that it can be "seen" through the entire class. Here's an example:

public class NumericalGrade

{

private int grade;

public NumericalGrade(int g)

{

grade = g;

}

public double getNum()

{

// double n = 0;! removed

if (grade == A1) {

// ...

}

// ...

}

}

Good luck.

prometheuzza at 2007-7-11 15:06:02 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks guys, this worked out well, I just changed my String variable types to doubles and removed the declaration of n = 0 in the getNum() method and everything checked out great! Thanks again!
fairtexa at 2007-7-11 15:06:02 > top of Java-index,Java Essentials,New To Java...