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

Thread: troubleshooting GPA Calculator

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default troubleshooting GPA Calculator

    I am writing a GPA calculator for a java programming class. I have written all the code and tried to run the program but I keep getting the most insane GPA's. 0.00 13.00 etc. While I would love to have a 13.00 GPA, I think there is something perhaps wrong with my code. I have posted the code below. I have also included some system.out.prints of different variables throughout the process trying to diagnose what is wrong. I will delete these in the final code but thought they would perhaps be useful for diagnostic purposes. Below is the code and the terminal screen. Also I apologize if my variables are confusing. I need to work on defining them better. For your info, Ctotal is the total number of class hours and Gtotal is the number of GPA points for each class added together. hours is the number of hours for an individual class. grade is the letter grade while Ngrade is that converted into number form. Ex A(grade) = 4.0(Ngrade). Please ask if anything does not make sense. I appreciate the help with troubleshooting. I have tried several things but can not figure out what is wrong. I guess it is at least putting out a GPA now even if it is obviously incorrect

    Code

    import java.text.*;
    import java.util.*;
     
    public class proj3 {
    public static void main (String[] args){
    DecimalFormat df = new DecimalFormat("#0.00");
    Scanner s = new Scanner(System.in);
    System.out.print("How many courses did you take?");
    int course = Integer.parseInt(s.nextLine());
    int count = 0;
    int Ngrade = 0;
    int Ctotal = 0;
    int Gtotal = 0;
     
    while (count < course){
    count ++;
    System.out.print("How many hours?");
    int hours = Integer.parseInt(s.nextLine());
    System.out.print("Letter Grade?");
    String grade = s.nextLine();
     
    if (grade.equals( "A" ))
    { 
    Ngrade = 4;
    }
     
    if (grade.equals( "B" ))
    {
    Ngrade = 3;
    }
     
    if (grade.equals("C" ))
    {
    Ngrade = 2;
    }
     
    if (grade.equals("D" ))
    {
    Ngrade = 1;
    }
     
    if (grade.equals("F" ))
    {
    Ngrade = 0;
    }
     
    if (grade.equals( "a"))
    { 
    Ngrade = 4;
    }
     
    if (grade.equals("b"))
    {
    Ngrade = 3;
    }
     
    if (grade.equals( "c"))
    {
    Ngrade = 2;
    }
     
    if (grade.equals("d"))
    {
    Ngrade = 1;
    }
     
    if (grade.equals( "f"))
    {
    Ngrade = 0;
    }
     
    // Gtotal is A = 4.0 b=3.0 etc
    //Ctotal is number of class hours
     
    System.out.println(Gtotal + "initial GPA value");
    System.out.println(Ctotal + "initial class hours");
     
     
    Gtotal= Gtotal * Ctotal;
     
    System.out.println(Gtotal + "GPA when multiplied by class hours");
    //Gtotal equals gpa times class hours
     
    Ctotal= Ctotal + hours;
    Gtotal = Gtotal + Ngrade;
     
    System.out.println(Ctotal + "total number of class hours");
    System.out.println(Gtotal + "total of GPA's for all classes");
     
     
     
    }
     
    double GPA = Gtotal/Ctotal;
     
    System.out.println(df.format(GPA));
     
     
     
    }
    }

    Terminal window

    How many courses did you take?4
    How many hours?1
    Letter Grade?A
    0initial GPA value
    0initial class hours
    0GPA when multiplied by class hours
    1total number of class hours
    4total of GPA's for all classes
    How many hours?2
    Letter Grade?B
    4initial GPA value
    1initial class hours
    4GPA when multiplied by class hours
    3total number of class hours
    7total of GPA's for all classes
    How many hours?3
    Letter Grade?C
    7initial GPA value
    3initial class hours
    21GPA when multiplied by class hours
    6total number of class hours
    23total of GPA's for all classes
    How many hours?4
    Letter Grade?D
    23initial GPA value
    6initial class hours
    138GPA when multiplied by class hours
    10total number of class hours
    139total of GPA's for all classes
    13.00 (This is Semester GPA)


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: troubleshooting GPA Calculator

    Your code is without any indentations making it all left justified and almost impossible to debug. Please re-format your posted code by giving it proper indentations, usually 3 spaces per block, and making sure that all code on the same block is on the same indentation level.

    Your cooperation in this would be greatly appreciated and will likely improve your chances of getting a decent answer quicker.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: troubleshooting GPA Calculator

    I am sorry curmedgeon. I don't usually indent my programs. My TA does and I realize this is a habit I need to get into. Here is the code formatted into indentations. Thank you for helping. Please let me know if that is not formatted correctly and I will try again.

    Kat

    /*
     *
     * (GPA Calculator)
     *
     *
    @Katherine Deckert
     *
    @Proj3
     */
     
    import java.text.*;
    import java.util.*;
     
    public class proj3 {
        public static void main (String[] args){
            DecimalFormat df = new DecimalFormat("#0.00");
            Scanner s = new Scanner(System.in);
            System.out.print("How many courses did you take?");
            int course = Integer.parseInt(s.nextLine());
            int count = 0;
            int Ngrade = 0;
            int Ctotal = 0;
            int Gtotal = 0;
     
            while (count < course){
                count ++;
                System.out.print("How many hours?");
                int hours = Integer.parseInt(s.nextLine());
                System.out.print("Letter Grade?");
                String grade = s.nextLine();
     
                if (grade.equals( "A" ))
                { 
                    Ngrade = 4;
                }
     
                if (grade.equals( "B" ))
                {
                    Ngrade = 3;
                }
     
                if (grade.equals("C" ))
                {
                    Ngrade = 2;
                }
     
                if (grade.equals("D" ))
                {
                    Ngrade = 1;
                }
     
                if (grade.equals("F" ))
                {
                    Ngrade = 0;
                }
     
                if (grade.equals( "a"))
                { 
                    Ngrade = 4;
                }
     
                if (grade.equals("b"))
                {
                    Ngrade = 3;
                }
     
                if (grade.equals( "c"))
                {
                    Ngrade = 2;
                }
     
                if (grade.equals("d"))
                {
                    Ngrade = 1;
                }
     
                if (grade.equals( "f"))
                {
                    Ngrade = 0;
                }
     
                // Gtotal is A = 4.0 b=3.0 etc
                //Ctotal is number of class hours
     
                System.out.println(Gtotal + "initial GPA value");
                System.out.println(Ctotal + "initial class hours");
     
                Gtotal= Gtotal * Ctotal;
                System.out.println(Gtotal + "GPA when multiplied by class hours");
                //Gtotal equals gpa times class hours
     
                Ctotal= Ctotal + hours;
                Gtotal = Gtotal + Ngrade;
     
                System.out.println(Ctotal + "total number of class hours");
                System.out.println(Gtotal + "total of GPA's for all classes");
     
            }
            double GPA = Gtotal/Ctotal;
            System.out.println(df.format(GPA));
     
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: troubleshooting GPA Calculator

    The formatting looks much better now.

    For easier testing for everyone and to make sure everyone is testing with the same data, can create a String in the Scanner class's constructor that has all the answers that a user would type in to answer program questions?
    For example:
            Scanner s = new Scanner("2\n12\nB\n16\nC\n"); //System.in);
    When the code is executed with a String vs System.in, it will automatically answer all questions from the String.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: troubleshooting GPA Calculator

    Norm- that is pretty neat. I wish they would have taught us to do that in class. I have put your suggestions for the values into the scanner, as I assumed you had already run the program with those numbers. The terminal window looks slightly disorganized now yet I don't know why adding that one line would make it that way. It appears to be working with the scanner string though just not pulling in data correctly or giving the right answer which it wasn't doing before anyway.lol.
    Here is the code still formatted with the string in the scanner. I have kept the normal scanner in the code still but have made it inactive. Thanks for your help.

    /*
     *
     * (GPA Calculator)
     *
     *
    @Katherine Deckert
     *
    @Proj3
     */
     
    import java.text.*;
    import java.util.*;
     
    public class proj3 {
        public static void main (String[] args){
            DecimalFormat df = new DecimalFormat("#0.00");
            //Scanner s = new Scanner(System.in);
     
            Scanner s = new Scanner("2\n12\nB\n16\nC\n"); //System.in);
            System.out.print("How many courses did you take?");
            int course = Integer.parseInt(s.nextLine());
            int count = 0;
            int Ngrade = 0;
            int Ctotal = 0;
            int Gtotal = 0;
     
            while (count < course){
                count ++;
                System.out.print("How many hours?");
                int hours = Integer.parseInt(s.nextLine());
                System.out.print("Letter Grade?");
                String grade = s.nextLine();
     
                if (grade.equals( "A" ))
                { 
                    Ngrade = 4;
                }
     
                if (grade.equals( "B" ))
                {
                    Ngrade = 3;
                }
     
                if (grade.equals("C" ))
                {
                    Ngrade = 2;
                }
     
                if (grade.equals("D" ))
                {
                    Ngrade = 1;
                }
     
                if (grade.equals("F" ))
                {
                    Ngrade = 0;
                }
     
                if (grade.equals( "a"))
                { 
                    Ngrade = 4;
                }
     
                if (grade.equals("b"))
                {
                    Ngrade = 3;
                }
     
                if (grade.equals( "c"))
                {
                    Ngrade = 2;
                }
     
                if (grade.equals("d"))
                {
                    Ngrade = 1;
                }
     
                if (grade.equals( "f"))
                {
                    Ngrade = 0;
                }
     
                // Gtotal is A = 4.0 b=3.0 etc
                //Ctotal is number of class hours
     
                System.out.println(Gtotal + "initial GPA value");
                System.out.println(Ctotal + "initial class hours");
     
                Gtotal= Gtotal * Ctotal;
                System.out.println(Gtotal + "GPA when multiplied by class hours");
                //Gtotal equals gpa times class hours
     
                Ctotal= Ctotal + hours;
                Gtotal = Gtotal + Ngrade;
     
                System.out.println(Ctotal + "total number of class hours");
                System.out.println(Gtotal + "total of GPA's for all classes");
     
            }
            double GPA = Gtotal/Ctotal;
            System.out.println(df.format(GPA));
     
        }
    }
    Last edited by uswhovian; March 18th, 2013 at 12:41 PM. Reason: code not pasted correctly

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: troubleshooting GPA Calculator

    Now you need to look at the program's output and see if it is what you want. If not, copy and paste the output here and add some comments to it describing what is wrong with it and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: troubleshooting GPA Calculator

    Norm below is the output of the terminal window using the values of 2 classe, 12 hours of b and 16 hours of C. The formatting became weird with the string inside the scanner but getting the program to work is my first concern.

    How many courses did you take?How many hours?Letter Grade?0initial GPA value
    0initial class hours
    0GPA when multiplied by class hours
    12total number of class hours
    3total of GPA's for all classes
    How many hours?Letter Grade?3initial GPA value
    12initial class hours
    36GPA when multiplied by class hours
    28total number of class hours
    38total of GPA's for all classes
    1.00


    Given these values ideally the program output window would look like this. side notes in parenthesis

    How many courses did you take?2
    How many hours?12
    Letter Grade?B
    3.0initial GPA value ( for a b grade)
    12initial class hours
    36GPA when multiplied by class hours (3*12))
    12total number of class hours (this is correct (12+0))
    36total of GPA's for all classes (36+0))
    How many hours?16
    Letter Grade?C
    2.0initial GPA value (for a C)
    16initial class hours
    32GPA when multiplied by class hours(2.0 gpa * 16 credit hours)
    28total number of class hours (this is correct 12 hours +16 hours)
    68total of GPA's for all classes ( (36+32)
    2.43 (final GPA)
    formula used for calculating gpa is Gtotal( total of gpa points multiplied by class hours)/(total number of class hours)



    The terminal window when completely done should look like this. I will be getting rid of all of the variable prints as those are merely trying to figure out where the program is going wrong.

    How many courses did you take? 2

    Course (1) : How many hours? 12
    Course (1) : Letter Grade? B
    Course (2) : How many hours? 16
    Course (2) : Letter Grade? C

    GPA: 2.43


    Hope this helps. Random question as you seem rather well versed in programming. How long does it typically take before it starts to come naturally and one does not make lots of errors? I'm rather new to this but enjoying it despite how tedious it seems. I was just sort of curious.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: troubleshooting GPA Calculator

    Look at the code to see what order the statements are in. The print statements are BEFORE the code that assigns values to the variables being printed.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: troubleshooting GPA Calculator

    Thanks for bringing that up norm. I looked back through the code and I had gotten some variables mixed around leading to not assigning values

    I have fixed this and the new code is posted below. Based upon the terminal window, it appears the variables are defined correctly now as I am getting all the correct values for class hour related info. Yet anything relating to GPA is 0. I suspect that there is an error in the code where the program asks the user for a letter value and then converts it to a number (ex a =4 b=3 etc. ) I don't know if it is not capturing the letter value or if it is not converting it over to a numerical value. They did not teach us how to convert from a string to an int. So I tried to teach myself although perhaps I have coded it wrongly. I suspect that once this part of the program is fixed. It should run correctly. Do you know what I did wrong with the converting of the letter grade to a point value?

    /*
     *
     * (GPA Calculator)
     *
     *
    @Katherine Deckert
     *
    @Proj3
     */
     
    import java.text.*;
    import java.util.*;
     
    public class proj3 {
        public static void main (String[] args){
            DecimalFormat df = new DecimalFormat("#0.00");
            //Scanner s = new Scanner(System.in);
     
            Scanner s = new Scanner("2\n12\nB\n16\nC\n"); //System.in);
            System.out.print("How many courses did you take?");
            int course = Integer.parseInt(s.nextLine());
            int count = 0;
            int Ngrade ;
            int Ctotal = 0 ;
            int Gtotal = 0;
            int GPtotal ;
     
            while (count < course){
                count ++;
                System.out.print("How many hours?");
                int hours = Integer.parseInt(s.nextLine());
               System.out.print("Letter Grade?");
                String grade = s.nextLine();
     
                if (grade.equals( "A" ))
                { 
                    Ngrade = 4;
                }
     
                if (grade.equals( "B" ))
                {
                    Ngrade = 3;
                }
     
                if (grade.equals("C" ))
                {
                    Ngrade = 2;
                }
     
                if (grade.equals("D" ))
                {
                    Ngrade = 1;
                }
     
                if (grade.equals("F" ))
                {
                    Ngrade = 0;
                }
     
                if (grade.equals( "a"))
                { 
                    Ngrade = 4;
                }
     
                if (grade.equals("b"))
                {
                    Ngrade = 3;
                }
     
                if (grade.equals( "c"))
                {
                    Ngrade = 2;
                }
     
                if (grade.equals("d"))
                {
                    Ngrade = 1;
                }
     
                if (grade.equals( "f"))
                {
                    Ngrade = 0;
                }
     
                else 
                {
                    Ngrade=0;
                }
     
                // Ngrade is A = 4.0 b=3.0 etc
                //Ctotal is number of class hours
     
                System.out.println(Ngrade + "initial GPA value");
                System.out.println(hours + "initial class hours");
     
                GPtotal= Ngrade * hours;
                System.out.println(GPtotal + "GPA when multiplied by class hours");
                //GPtotal equals gpa times class hours
     
                Ctotal= Ctotal + hours;
                Gtotal = Gtotal + GPtotal;
     
                System.out.println(Ctotal + "total number of class hours");
                System.out.println(Gtotal + "total of GPA's for all classes");
     
            }
            double GPA = Gtotal/Ctotal;
            System.out.println(df.format(GPA));
     
        }
    }



    terminal window now based off same values as before.
    How many courses did you take?
    How many hours?
    Letter Grade?
    0initial GPA value
    12initial class hours
    0GPA when multiplied by class hours
    12total number of class hours
    0total of GPA's for all classes
    How many hours?Letter Grade?0initial GPA value
    16initial class hours
    0GPA when multiplied by class hours
    28total number of class hours
    0total of GPA's for all classes
    0.00

    As you can see everything relating to class value is correct but anything relating to GPA is zero. This obviously is caused by the program not having an initial point value to work with. Thus 0 times anything = 0.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: troubleshooting GPA Calculator

    The equations used in the program are very simple. You need to play computer with the program and manually go through each statement and do on paper what the statement does to see where the code is not doing what you want.

    Print out the contents of the variable grade after it is given a value so you can see what it is.
    Also add a println statement in the final else statement to print a message giving the value of grade and saying it is not a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to calculate GPA
    By leonne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2012, 02:37 PM
  2. [SOLVED] Need advice in troubleshooting
    By javaneedhelp in forum Exceptions
    Replies: 3
    Last Post: November 15th, 2011, 08:57 AM
  3. GPA Calculation Program using an input file
    By ddk1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2011, 06:28 AM
  4. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  5. GPA calc code... HELP
    By ber1023 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2009, 11:20 PM