import java.text.*; // format output
import java.util.*; // Scanner class

public class gpa
{
    public static void main(String[] args)
    {
        DecimalFormat num=new DecimalFormat("#######.00"); // Create format for name num (courseName)
        DecimalFormat gpaNum=new DecimalFormat("##.00"); // Create format for name gpaNum (gpa)


        // variable names 
        gpaNum gpa=0; // grade point average
        double gp=0; // grade point 
        double totalgp=0; // total grade point
        int n=0; //number of courses
        String str; // to hold answer input to the prompt
        String firstName; // student first name
        String lastName; // student last name
        String inGrade; //input grade
        num courseName; //course name & section (ex. CISC115.59)
        String str; //to hold input answer for repeat prompt
        char repeat; //to hold yes 'Y' or no 'N' answer 
        char letGrade; //input letter grade
        letGrade= charAt(0);
        int idNumber=0; //student ID number
        char repeat; // to hold yes or no, 'Y' or 'N'   

        //1st row in Heading
        System.out.println("\tBucks County Community College "+"Grade Report For ");

        // Initialize Scanner to read from DOS window.
        Scanner input = new Scanner(System.in);

        // Get as many different couses as the student wants.

        do
        {
            // Read numbers from the DOS window.
            System.out.print("Enter the student First name: ");
            firstName = input.next();

            System.out.print("Enter the student Last name:  ");
            lastName = input.next();

            System.out.print("Enter the student ID Number:  ");
            idNumber = input.nextInt();

            //Output Title Heading
            System.out.println(firstName+lastName); //2nd row in heading
            System.out.println(idNumber); //3rd row in Heading


            //Start Count Loop to read all courses and their letter grades
            for(i=1; i<=n; i++) 
            {
                //input number of courses
                System.out.print("Enter the number of courses:  ");
                n = input.nextInt();

                //input course name
                System.out.print("Enter the course name:    ");
                courseName = input.next();

                //input letter grade
                System.out.print("Enter the letter grade of this course:    ");
                letGrade = charAt(0);

                switch(letGrade)
                {
                    case 'A': gp=4; break;
                    case 'B': gp=3; break;
                    case 'C': gp=2; break;
                    case 'D': gp=1; break;
                    default: gp=0; break;                   
                } //end Switch loop

                //Output layout with Column & Column Heading on the DOS window
                System.out.println("Course"+ "\t\tGrade"+ "\t\tGradepoints");//column heading
                System.out.println(courseName+letGrade+gp);//column body


                // Compute the total grade points.
                totalgp+= gp;

            }

            // Compute the GPA
            gpa= totalgp / n;

            //Output layout with summary GPA
            System.out.println("--------------------------------------");//column divider
            System.out.println("GPA: "+ gpa);//Output GPG as the column footer


            System.out.println(); //Prints a blank line.

            // Calculate the GPA for as many students as there are input
            System.out.println("Would you like to prepare GPA for another student?"); 


            System.out.print("Enter Y for Yes or N for No: ");

            str=input.next(); // Read next char
            repeat=str.charAt(0); // Get the first char.

        } while (repeat=='Y' || repeat=='y');

        System.out.println("Good-bye!");
    }
}

Recommended Answers

All 10 Replies

Was there a question in there somewhere?

Was there a question in there somewhere?

Opps...There you go:

  • Prepare a grade report for more than 1 student using DOS window input and output to a file with print outs letter grades # of A's, B's etc.of all students.

  • Input: College Name, Student Name, Student ID Number and Courses-Section Name (ex. CISC115.59), Letter Grades.

  • Output: Format layout with Heading and Column: College Name, Student Name, Student ID Number (as Title); Courses, Letter Grades, Grade Points (Headings and column's body)and GPA (as Footer)

  • Description: Using Writing own method, Switch Selection Control, String, Accumulating, Counting method (and other loops if needed), Array (base on last reviews from previous labs).

I know there will be more loops and array as it required more than one student with grades sort out. For easy understanding I started out as a simple program then adding up as I go. Please be patient. Below is the code which I just did some minor fixed. Thnx

import java.text.*; // format output
import java.util.*; // Scanner class


public class gpa
{
    public static void main(String[] args)
    {
        DecimalFormat num=new DecimalFormat("#######.00"); // Create format for name num (courseName)
        DecimalFormat gpaNum=new DecimalFormat("##.00"); // Create format for name gpaNum (gpa)


        // variable names 
        gpaNum gpa=0; // grade point average
        double gp=0; // grade point 
        double totalgp=0; // total grade point
        int n=0; //number of courses
        String str; // to hold answer input to the prompt
        String firstName; // student first name
        String lastName; // student last name
        String inGrade; //input grade
        num courseName; //course name & section (ex. CISC115.59)
        String str; //to hold input answer for repeat prompt
        char repeat; //to hold yes 'Y' or no 'N' answer 
        char letGrade; //input letter grade
        letGrade= charAt(0);
        int idNumber=0; //student ID number
        char repeat; // to hold yes or no, 'Y' or 'N'   

        //1st row in Heading
        System.out.println("\tBucks County Community College "+"Grade Report For ");

        // Initialize Scanner to read from DOS window.
        Scanner input = new Scanner(System.in);

        // Get as many different couses as the student wants.

        do
        {
            // Read numbers from the DOS window.
            System.out.print("Enter the student First name: ");
            firstName = input.next();

            System.out.print("Enter the student Last name:  ");
            lastName = input.next();

            System.out.print("Enter the student ID Number:  ");
            idNumber = input.nextInt();

            //input number of courses
            System.out.print("Enter the number of courses:  ");
            n = input.nextInt();


            //Output Title Heading
            System.out.println(firstName+lastName); //2nd row in heading
            System.out.println(idNumber); //3rd row in Heading


            //Start Count Loop to read all courses and their letter grades
            for(i=1; i<=n; i++) 
            {                               
                //input course name
                System.out.print("Enter the course name:    ");
                courseName = input.next();

                //input letter grade
                System.out.print("Enter the letter grade of this course:    ");
                letGrade = charAt(0);

                switch(letGrade)
                {
                    case 'A': gp=4; break;
                    case 'B': gp=3; break;
                    case 'C': gp=2; break;
                    case 'D': gp=1; break;
                    default: gp=0; break;                   
                } //end Switch loop

                //Output layout with Column & Column Heading on the DOS window
                System.out.println("Course"+ "\t\tGrade"+ "\t\tGradepoints");//column heading
                System.out.println(courseName+"\t\t"+letGrade+"\t\t"+gp);//column body


                // Compute to find the total grade points.
                totalgp+= gp;                   
            }

            // Compute the GPA
            gpa= totalgp / n;

            //Output layout with summary GPA
            System.out.println("--------------------------------------");//column divider
            System.out.println("\tGPA: "+ gpa);//Output GPG as the column footer
            System.out.println(); //Prints a blank line.





            // Calculate the GPA for as many students as there are input
            System.out.println("Would you like to prepare GPA for another student?"); 


            System.out.print("Enter Y for Yes or N for No: ");

            str=input.next(); // Read next char
            repeat=str.charAt(0); // Get the first char.

        } while (repeat=='Y' || repeat=='y');

        System.out.println("Good-bye!");
    }
}

Looks like code alright (but it would be easier to read if it were in code tags - select your program text and use the button marked (Code). But what's your question? Not your assignment, your question.

Looks like code alright (but it would be easier to read if it were in code tags - select your program text and use the button marked (Code). But what's your question? Not your assignment, your question.

I have lots of errors where i declared my variables and format. Such as, num for format courseName, charAt(0)...please look at the copied & pasted below. How do I fix them?

 ----jGRASP exec: javac -g gpa.java
gpa.java:36: cannot find symbol
symbol  : class num
location: class gpa
        num courseName; //course name & section (ex. CISC115.59)
        ^
gpa.java:39: cannot find symbol
symbol  : method charAt(int)
location: class gpa
        letGrade= charAt(0);
                  ^
gpa.java:41: repeat is already defined in main(java.lang.String[])
        char repeat; // to hold yes or no, 'Y' or 'N'   
             ^
gpa.java:74: cannot find symbol
symbol  : variable i
location: class gpa
            for(i=1; i<=n; i++) 
                ^
gpa.java:74: cannot find symbol
symbol  : variable i
location: class gpa
            for(i=1; i<=n; i++) 
                     ^
gpa.java:74: cannot find symbol
symbol  : variable i
location: class gpa
            for(i=1; i<=n; i++) 
                           ^
gpa.java:82: cannot find symbol
symbol  : method charAt(int)
location: class gpa
                letGrade = charAt(0);
                           ^
gpa.java:103: cannot find symbol
symbol  : variable gpa
location: class gpa
            gpa= totalgp / n;
            ^
gpa.java:107: cannot find symbol
symbol  : variable gpa
location: class gpa
            System.out.println("\tGPA: "+ gpa);//Output GPG as the column footer
                                          ^
9 errors

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

Okay, "Cannot find symbol" means that you're referring to something that doesn't exist in the context where you're trying to use it.
This can be a class:

gpa.java:36: cannot find symbol
symbol : class num
location: class gpa
num courseName; //course name & section (ex. CISC115.59)

(you're trying to declare a variable of type "num" which doesn't exist in Java)

It can be a method of a class:

gpa.java:39: cannot find symbol
symbol : method charAt(int)
location: class gpa
letGrade= charAt(0);

(Here you're trying to call a method "charAt" which is not defined for chars, as indeed no methods are defined for char, it's a primitive type.)

Or it can be a variable:

gpa.java:74: cannot find symbol
symbol : variable i
location: class gpa
for(i=1; i<=n; i++)

(Here you're trying to refer to a variable, i, which you haven't declared.)


This message is pretty clear:

gpa.java:41: repeat is already defined in main(java.lang.String[])
char repeat; // to hold yes or no, 'Y' or 'N'

(You've already defined "repeat" as a char, so it can't allocate space for a new variable with the same name.)


So now that you know how to read the error messages, you can probably figure out how to fix them.

Okay, "Cannot find symbol" means that you're referring to something that doesn't exist in the context where you're trying to use it.
This can be a class:

gpa.java:36: cannot find symbol
symbol : class num
location: class gpa
num courseName; //course name & section (ex. CISC115.59)

(you're trying to declare a variable of type "num" which doesn't exist in Java)

It can be a method of a class:

gpa.java:39: cannot find symbol
symbol : method charAt(int)
location: class gpa
letGrade= charAt(0);

(Here you're trying to call a method "charAt" which is not defined for chars, as indeed no methods are defined for char, it's a primitive type.)

Or it can be a variable:

gpa.java:74: cannot find symbol
symbol : variable i
location: class gpa
for(i=1; i<=n; i++)

(Here you're trying to refer to a variable, i, which you haven't declared.)


This message is pretty clear:

gpa.java:41: repeat is already defined in main(java.lang.String[])
char repeat; // to hold yes or no, 'Y' or 'N'

(You've already defined "repeat" as a char, so it can't allocate space for a new variable with the same name.)


So now that you know how to read the error messages, you can probably figure out how to fix them.

I seemed to get the hang of them. Below is the result of what I just ran. It doesn't look like it calculated the gpa. I'll be back in about 2 hrs or so for an update and add-on if you're still available. Thanks


----jGRASP exec: java gpa
Bucks County Community College Grade Report For
Enter the student First name: Tracy
Enter the student Last name: Do
Enter the student ID Number: 008657
Enter the number of courses: 2
TracyDo
8657
Enter the course name: cyhh354-59
Enter the letter grade of this course: a
Course Grade Gradepoints
cyhh354-59 a 0.0
Enter the course name: jklh567-89
Enter the letter grade of this course: b
Course Grade Gradepoints
jklh567-89 b 0.0
--------------------------------------
GPA: 0.0

Would you like to prepare GPA for another student?
Enter Y for Yes or N for No: n
Good-bye!

----jGRASP: operation complete.

I seemed to get the hang of them. Below is the result of what I just ran. It doesn't look like it calculated the gpa. I'll be back in about 2 hrs or so for an update and add-on if you're still available. Thanks

 ----jGRASP exec: java gpa
    Bucks County Community College Grade Report For 
Enter the student First name:   Tracy
Enter the student Last name:    Do
Enter the student ID Number:    008657
Enter the number of courses:    2
TracyDo
8657
Enter the course name:  cyhh354-59
Enter the letter grade of this course:  a
Course      Grade       Gradepoints
cyhh354-59      a       0.0
Enter the course name:  jklh567-89
Enter the letter grade of this course:  b
Course      Grade       Gradepoints
jklh567-89      b       0.0
--------------------------------------
    GPA: 0.0

Would you like to prepare GPA for another student?
Enter Y for Yes or N for No: n
Good-bye!

 ----jGRASP: operation complete.

end quote.

Forgot to post what I have fixed so you can have an idea. There it is:

import java.text.*; // format output
import java.util.*; // Scanner class


public class gpa
{
    public static void main(String[] args)
    {
        DecimalFormat num=new DecimalFormat("#######.00"); // Create format for name num (courseName)


        // variable names 

        double gp=0.0; // grade point 
        double totalgp=0.0; // total grade point
        int n=0; //number of courses
        double gpa=0.0; //greade point average
        String str; // to hold answer input to the prompt
        String firstName; // student first name
        String lastName; // student last name
        String inGrade; //input grade
        String courseName; //course name
        char repeat; //to hold yes 'Y' or no 'N' answer 
        char letGrade; //input letter grade
        int idNumber=0; //student ID number
        int i=0; //number of courses input  

        //1st row in Heading
        System.out.println("\tBucks County Community College "+"Grade Report For ");

        // Initialize Scanner to read from DOS window.
        Scanner input = new Scanner(System.in);

        // Get as many different couses as the student wants.

        do
        {
            // Read numbers from the DOS window.
            System.out.print("Enter the student First name: ");
            firstName = input.next();

            System.out.print("Enter the student Last name:  ");
            lastName = input.next();

            System.out.print("Enter the student ID Number:  ");
            idNumber = input.nextInt();

            //input number of courses
            System.out.print("Enter the number of courses:  ");
            n = input.nextInt();


            //Output Title Heading
            System.out.println(firstName+lastName); //2nd row in heading
            System.out.println(idNumber); //3rd row in Heading


            //Start Count Loop to read all courses and their letter grades
            for(i=1; i<=n; i++) 
            {                               
                //input course name
                System.out.print("Enter the course name:    ");
                courseName = input.next();

                //input letter grade
                System.out.print("Enter the letter grade of this course:    ");
                str=input.next(); // Read next char
                letGrade = str.charAt(0); //get the letter grade

                switch(letGrade)
                {
                    case 'A': gp=4; break;
                    case 'B': gp=3; break;
                    case 'C': gp=2; break;
                    case 'D': gp=1; break;
                    default: gp=0; break;                   
                } //end Switch loop

                //Output layout with Column & Column Heading on the DOS window
                System.out.println("Course"+ "\t\tGrade"+ "\t\tGradepoints");//column heading
                System.out.println(courseName+"\t"+letGrade+"\t"+gp);//column body


                // Compute to find the total grade points.
                totalgp+= gp;                   
            }

            // Compute the GPA
            gpa= totalgp / n;

            //Output layout with summary GPA
            System.out.println("--------------------------------------");//column divider
            System.out.println("\tGPA: "+ gpa);//Output GPG as the column footer
            System.out.println(); //Prints a blank line.


            // Calculate the GPA for as many students as there are input
            System.out.println("Would you like to prepare GPA for another student?"); 

            System.out.print("Enter Y for Yes or N for No: ");

            str=input.next(); // Read next char
            repeat=str.charAt(0); // Get the first char.

        } while (repeat=='Y' || repeat=='y');

        System.out.println("Good-bye!");
    }

The way to solve this sort of problem (as I just said in another thread) is to find the place where you have a wrong value and track back to figure out how it got that way. In this case, your wrong value is gpa==0. What is the last time you touch GPA before you print it out? What do you assign to it? Are you sure that's correct? (actually, you're pretty sure it's incorrect, since the next thing that happens is it has the wrong value). If you're not sure what's happening, put in print statements to see the values of the variables involved. If they don't look right, how did they get that way? Work back this way until facepalm occurs.

You've avoided the most obvious mistake: since totalgp is a double, you're not getting an integer division problem. That tells me it's likely a simple logic error - I'll let you hunt it down, it'll be good practice for you.

The way to solve this sort of problem (as I just said in another thread) is to find the place where you have a wrong value and track back to figure out how it got that way. In this case, your wrong value is gpa==0. What is the last time you touch GPA before you print it out? What do you assign to it? Are you sure that's correct? (actually, you're pretty sure it's incorrect, since the next thing that happens is it has the wrong value). If you're not sure what's happening, put in print statements to see the values of the variables involved. If they don't look right, how did they get that way? Work back this way until facepalm occurs.

You've avoided the most obvious mistake: since totalgp is a double, you're not getting an integer division problem. That tells me it's likely a simple logic error - I'll let you hunt it down, it'll be good practice for you.

I'm not quite understand why you're mention about what is the last time I touch gpa before I print it out? and "You've avoided the most obvious mistake: since totalgp is a double, you're not getting an integer division problem." I fixed the "n" which I declared as an interger before to double for the compute area "gpa= totalgp / n;" but it still giving me the same result. Did I do what you need me to do?

I notice that if I input capital A (like I have in Switch loop) instead of lower case a, it results with gpa number (just even number: 3.0 instead of 3.5).

I'm now trying to correct my output layout using just one column & header instead of every time user inputs different courses. But one thing at the time. May you walk me through the gpa calculating part and how do I also have lower case a for letter grade in case if use want to use it? I have tried using || (or) but it doesn't work here. Thanks

You know that the value of gpa is 0 when you print it out, right? How did it get set to 0? Well, the last time you assigned something to gpa, it ended up as 0. So find that assignment and see what happened there.

As for your capital letters issue, there are two ways to deal with it. The more usual method is to upcase or downcase the input:

String s = "a";
s = s.toUpperCase();
System.out.println(s);   // prints "A"

However, you could also use a fall-through in your switch statement:

switch(letGrade)
{
case 'a':
case 'A': gp=4; break;
case 'b':
case 'B': gp=3; break;
case 'c':
case 'C': gp=2; break;
case 'd':
case 'D': gp=1; break;
default: gp=0; break;
} //end Switch loop

Later on, you should probably read up on how a switch works - I imagine your teacher told you it's like a bunch of if statements. That's not quite correct, and if you want to understand why this works, or why you can't use a logical or in a case statement, or why you can't use a String in a case, you'll need to do a bit more advanced study.
One thing you should already know: a switch ain't a loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.