0

I'm supposed to design a method that takes in a double value and translates the value into the closest letter grade. 4,3,2,1,0 denote A, B,C,D,F, respectively. + increases a grade's numerical value by 0.3 and - decreases a grade's numerical value by 0.3. There is no F+ or F-. A+ is 4.0

Here is the problem: How do I change my method so that when I enter 2.8, the grade returned will be a B- and when I enter 2.85, the grade returned will be a B? Here's whaT I got:

public class Grade
{
	public Grade(double numGrade)
	{
		num = numGrade;
	}
	public String getLetterGrade()
	{
		String grade = null;
		double numGrade = (double)Math.round(num);

		System.out.println(numGrade);

		if (numGrade > 3.7)
			grade = "A+";
		else if (numGrade <= 3.7 && numGrade > 3.3)
			grade = "A-";
		else if (numGrade <= 3.3 && numGrade > 3.0)
			grade = "B+";
		else if (numGrade <= 3.0 && numGrade > 2.7)
			grade = "B";
		else if (numGrade <= 2.7 && numGrade > 2.3)
			grade = "B-";
		else if (numGrade <= 2.3 && numGrade > 2.0)
			grade = "C+";
		else if (numGrade <= 2.0 && numGrade > 1.7)
			grade = "C";
		else if (numGrade <= 1.7 && numGrade > 1.3)
			grade = "C-";
		else if (numGrade <= 1.3 && numGrade > 1.0)
			grade = "D+";
		else if (numGrade <= 1.0 && numGrade > 0.7)
			grade = "D-";
		else if (numGrade < 0.7)
			grade = "F";

		return grade;
	}
	private double num;
}
import java. util.Scanner;

/**
   This class prints the letter grade corresponding to a numeric value given
   by the user.
*/
public class GradePrinter
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a numeric value:");
      double numGrade = in.nextDouble();

      Grade g = new Grade(numGrade);

      System.out.println("Letter grade: " + g.getLetterGrade()) ;
   }
}
3
Contributors
2
Replies
3
Views
10 Years
Discussion Span
Last Post by Mouche
0

Don't tell me that you are not even capable of changing the numbers in that block of if statements. If that is the case then I want to know who you copied the code from, because you definately did not write it, in that case.

0

Yeah, just change the numbers in the if statements.

Fyi, you don't need the first boolean comparison in the if statements.

Example:

if (numGrade > 3.7)
grade = "A+";
else if (numGrade <= 3.7 && numGrade > 3.3)

For the program to even get to the else if, numGrade has to be less than or equal to 3.7.

So, you can change all of the else ifs to:

else if (numGrade > [# here] )
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.