Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: why does this code print 0.0 for the value and "F" as the letter grade?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Exclamation why does this code print 0.0 for the value and "F" as the letter grade?

    why is it that when i run this it outputs "0.0" and "F" every time?
    import java.util.Scanner;
     
    public class GradeTest
    {
    public static void main (String[] args)
    {
    Scanner in = new Scanner(System.in);
    String reply = "yes";
    while (reply.equalsIgnoreCase("yes") || reply.equalsIgnoreCase("y"))
    {
    if (reply.equalsIgnoreCase("no") || reply.equalsIgnoreCase("n"))
    System.exit(0);
    System.out.println("Enter a number grade 0-4: ");
    double grade = in.nextDouble();
    Grade g = new Grade(grade);
    System.out.println(g.getNumberGrade() + g.getLetterGrade());
    System.out.println("Do you want to run again? ");
    reply = in.next();
    }
    }
    }
     
    /*
    * this class will give you your GPA
    */
    public class Grade
    {
    private double numberGrade;
    /*
    * constructor that receives a value
    * @param value the variable of the user input
    */
    public Grade(double value)
    {
    value = numberGrade;
    }
     
    //method to return the number grade
    public double getNumberGrade()
    {
    return numberGrade;
    }
     
    /*
    * this method, getLetterGrade() returns the
    * actual description in a string
    */
    public String getLetterGrade()
    {
    String letterGrade = ""; //variable to hold the grade notation
    String plusOrMinus = ""; //the + or - sign variable
     
    //the order matters in the if statement that decides the letter
    if (numberGrade > 4.0)
    letterGrade = "Off the charts";
    else if (numberGrade >= 3.5)
    letterGrade = "A";
    else if (numberGrade >= 2.5)
    letterGrade = "B";
    else if (numberGrade >= 1.5)
    letterGrade = "C";
    else if (numberGrade >= 0.5)
    letterGrade = "D";
    else if (numberGrade < 0.5)
    letterGrade = "F";
     
    //this arithmetic statement gives a double that is 0 <= remainder <= 4
    double remainder = numberGrade - (int) numberGrade;
    if ((0.5 <= remainder && 0.85 <= remainder))
    plusOrMinus = "-";
    else if ((0.15 <= remainder && 0.5 <= remainder))
    plusOrMinus = "+";
     
    return letterGrade + plusOrMinus;
    }
    }
    Last edited by helloworld922; January 29th, 2010 at 06:18 PM. Reason: Please use [code] tags!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why does this code print 0.0 for the value and "F" as the letter grade?

    The constructor in grade does not set the variable numberGrade, which is then always going to be 0.0.

    private double numberGrade;
    /*
    * constructor that receives a value
    * @param value the variable of the user input
    */
    public Grade(double value)
    {
    //value = numberGrade;//NO - not setting numberGrade
    numberGrade = value;//sets the variable numberGrade
    }

    PS Just a suggestion, but the code tags really help make your code more readable

  3. The Following 2 Users Say Thank You to copeg For This Useful Post:

    JavaPF (January 30th, 2010), Json (January 31st, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: why does this code print 0.0 for the value and "F" as the letter grade?

    thank you so much!!!

  5. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why does this code print 0.0 for the value and "F" as the letter grade?

    One problem you might run into, is if the user enters something completely unexpected, like "rgfqerg", it'll quit, whereas it should tell the user it's bad input and ask for a yes or no answer again.

Similar Threads

  1. help with the logic on this letter grade program.
    By etidd in forum Loops & Control Statements
    Replies: 2
    Last Post: January 28th, 2010, 09:14 PM
  2. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  3. Replies: 2
    Last Post: October 29th, 2009, 06:13 PM
  4. HELP "code needed"
    By Dave in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: August 26th, 2009, 11:06 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM