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 5 of 5

Thread: Hello, I have a class assignment and I need help.

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hello, I have a class assignment and I need help.

    This project is based on Value Returning Methods. So here is my code:
    [highlight = Java]
    import java.util.Scanner;

    public class HW8_1
    {
    public static void main (String[] args)
    {
    Scanner keyboard = new Scanner (System.in);
    double a;
    double b;
    double c;
    double d;
    double e;
    double total;
    String average = "";
    double calcAverage = 0.0;

    System.out.println("Please enter five test scores:");
    System.out.println("Test score #1:");
    a = keyboard.nextDouble();

    System.out.println("Test score #2:");
    b = keyboard.nextDouble();

    System.out.println("Test score #3:");
    c = keyboard.nextDouble();

    System.out.println("Test score #4:");
    d = keyboard.nextDouble();

    System.out.println("Test Score #5:");
    e = keyboard.nextDouble();

    total = calcAverage(a,b,c,d,e);
    average = determineGrade(calcAverage);

    System.out.println("Your letter grade is " + average);
    }

    public static double calcAverage (double num1, double num2, double num3, double num4, double num5)
    {
    return (num1 + num2 + num3 + num4 + num5) / 5;

    }
    public static String determineGrade (double total)
    {
    if (total >= 90)
    {
    return "A";
    }
    else if (total >= 80 && total <= 89)
    {
    return "B";
    }
    else if (total >= 70 && total <= 79)
    {
    return "C";
    }
    else if (total >= 60 && total <= 69)
    {
    return "D";
    }
    else if (total >= 0 && total <=59)
    {
    return "F";
    }
    return total;
    }
    }
    [/highlight]

    The assignment asks for the user to enter 5 different grades and spit out returns on two things.. the average of the 5 scores and a letter grade based on the average.
    When I compile, the letter grade doesn't show up. I've been changing the code left and right but I'm all out of ideas.
    What am I doing wrong?


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Hello, I have a class assignment and I need help.

    This would be your culprit
    average = determineGrade(calcAverage);
    You never set the value of calcAverage, so it is still 0.0. You most likely want to use total by the looks of it. The names of your variables are a bit confusing since they aren't what they say they are

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hello, I have a class assignment and I need help.

    Quote Originally Posted by Parranoia View Post
    This would be your culprit
    average = determineGrade(calcAverage);
    You never set the value of calcAverage, so it is still 0.0. You most likely want to use total by the looks of it. The names of your variables are a bit confusing since they aren't what they say they are
    Thank you!! Gaww!!!... I've been pulling my hair out on this silly little thing. I made adjustments to the code. For those who are curious over what changes I made... (which is 1 person)

    import java.util.Scanner;

    public class HW8_1
    {
    public static void main (String[] args)
    {
    Scanner keyboard = new Scanner (System.in);
    double a;
    double b;
    double c;
    double d;
    double e;
    double total;
    String grade = "";

    System.out.println("Please enter five test scores:");
    System.out.println("Test score #1:");
    a = keyboard.nextDouble();

    System.out.println("Test score #2:");
    b = keyboard.nextDouble();

    System.out.println("Test score #3:");
    c = keyboard.nextDouble();

    System.out.println("Test score #4:");
    d = keyboard.nextDouble();

    System.out.println("Test Score #5:");
    e = keyboard.nextDouble();

    total = calcAverage(a,b,c,d,e);
    grade = determineGrade(total);

    System.out.println("Your letter grade is " + grade);
    }

    public static double calcAverage (double num1, double num2, double num3, double num4, double num5)
    {
    return (num1 + num2 + num3 + num4 + num5) / 5;

    }
    public static String determineGrade (double total)
    {
    String grade="";
    if (total >= 90)
    {
    return grade = "A";
    }
    else if (total >= 80 && total <= 89)
    {
    return grade = "B";
    }
    else if (total >= 70 && total <= 79)
    {
    return grade = "C";
    }
    else if (total >= 60 && total <= 69)
    {
    return grade = "D";
    }
    else if (total >= 0 && total <=59)
    {
    return grade = "F";
    }
    return grade;
    }
    }

    Thanks again

  4. #4
    Junior Member
    Join Date
    Nov 2012
    Location
    Kolkata, India
    Posts
    8
    My Mood
    Cool
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Hello, I have a class assignment and I need help.

    You could also do this change to your original code. I think this was what you wanted

    grade = determineGrade(calcAverage(a,b,c,d,e));

    moreover,

    Your syntax in the method public static String determineGrade (double total){} is wrong

    You are having individual return statements within every conditional block. If that is so, then you do not need to return the variable "grade" at the end again. What you could do is keep the last return statement at the end of the function and remove all the previous ones. Just keep the value assigning part and remove the return keyword.

    One more thing is that the method has an extra closing second brace at the end, just after the last return statement

    So, this should be your edited code within that method


    --Code removed
    Last edited by jps; May 17th, 2013 at 11:48 PM. Reason: spoonfeeding

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Hello, I have a class assignment and I need help.

    @wings please see the announcements page for the use of code tags.
    @KaustavBanerjee please see The problem with spoonfeeding

Similar Threads

  1. My Class assignment help
    By ben_S21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2012, 01:35 AM
  2. class assignment
    By shanem in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 2nd, 2012, 01:23 PM
  3. class assignment
    By shanem in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 2nd, 2012, 01:23 PM
  4. Help with class assignment !
    By Ryoshiro in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2012, 09:35 PM
  5. class assignment
    By shanem in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 04:09 PM