I'm taking AP comp sci and I'm new to JAVA. Can someone help me write this program?

Assignment:

1.

Write a program that accepts the letter grades for a student, calculates the student's GPA, and prints it out along with one of the following five messages:

Eligible
Ineligible, taking less than 4 classes.
Ineligible, gpa below 2.0
Ineligible, gpa above 2.0 but has F grade (note: gpa >= 2.0)
Ineligible, gpa below 2.0 and has F grade

2.

Your program must use an appropriate sequence of nested if-else statements to print out the appropriate message.
3.

The message "Ineligible, taking less than 4 classes" has priority over the other 3 ineligible cases.
4.

The program is not to ask the user for how many grades are in a student's report card. The program will continue to read grades until a non-grade character is input. At this point, some type of loop will cease and the program prints the GPA value and the eligibility message.
5.

Example of run output:

GPA = 3.75 Eligible

6.

You do not have to print out any of the individual grades.
7.

Your program should allow input of grades in either upper or lower case.

Thank you!!

Dani AI

Generated

A clear structure makes this assignment straightforward: read letter tokens until a non-grade token appears, convert each valid letter to its numeric points, keep a running sum and count, set a flag if any F is seen, compute GPA as sum/count, then use a nested if-else (with the "less than 4 classes" check first) to pick the exact output string. The sketch by is a useful starting point, but it increments the count even for the sentinel input and compares strings case-sensitively; both issues can cause incorrect GPA or runtime errors. The mapping of grades to points was clarified earlier by .

An implementation approach that meets the assignment rules:

  • Read tokens (no prior prompt for how many grades).
  • Treat the first character of each token as the grade letter (accept upper or lower case; ignore trailing +/− if present).
  • Stop when that first character is not A, B, C, D, or F.
  • Only increment count and add to sum for valid grades.
  • After the loop compute GPA and use nested if-else to print one of the five required messages (check count < 4 first).

Example Java outline (keeps counting correct, accepts upper/lower case, and uses nested if-else for the final decision):

import java.util.Scanner;

public class Grades {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double sum = 0.0;
        int count = 0;
        boolean hasF = false;

        while (sc.hasNext()) {
            String tok = sc.next();
            if (tok.length() == 0) break;
            char g = Character.toUpperCase(tok.charAt(0));
            if (g == 'A' || g == 'B' || g == 'C' || g == 'D' || g == 'F') {
                double pts;
                switch (g) {
                    case 'A': pts = 4.0; break;
                    case 'B': pts = 3.0; break;
                    case 'C': pts = 2.0; break;
                    case 'D': pts = 1.0; break;
                    default:  pts = 0.0; hasF = true; break;
                }
                sum += pts;
                count++;
            } else {
                break;
            }
        }

        double gpa = count > 0 ? sum / count : 0.0;

        if (count < 4) {
            System.out.printf("GPA = %.2f\tIneligible, taking less than 4 classes.%n", gpa);
        } else {
            if (gpa < 2.0) {
                if (hasF) {
                    System.out.printf("GPA = %.2f\tIneligible, gpa below 2.0 and has F grade%n", gpa);
                } else {
                    System.out.printf("GPA = %.2f\tIneligible, gpa below 2.0%n", gpa);
                }
            } else {
                if (hasF) {
                    System.out.printf("GPA = %.2f\tIneligible, gpa above 2.0 but has F grade (note: gpa >= 2.0)%n", gpa);
                } else {
                    System.out.printf("GPA = %.2f\tEligible%n", gpa);
                }
            }
        }
    }
}

Notes: ensure count is only incremented for valid grades (avoids the off-by-one seen in 's sketch), handle EOF safely, and format the GPA (above uses two decimals).

Recommended Answers

All 6 Replies

You didnt write the comparison of grades vs the gpa ... how would I know the gpa if my grade is A?? A means 4? B means 3 or above ? or something else????

We dont do your hw for you here. what dont you understand about it? be more specific of your problem.

can you please let us know what exactly you are having problems on thanks

We dont do your hw for you here. what dont you understand about it? be more specific of your problem.

Yes. Please ask specific questions that you're having with YOUR code. We're not here to help you essentially cheat.

I dont understand the basic structure of the program. Where does everything go? Things like that.

A=4
B=3
C=2
D=1
F=0

I'm not trying to cheat, I just need some help. Thanks. :o

This isnt exactly what your looking for but it should give you an idea of how it kind of works. I hope this helps a little...

import java.io.*;

class Grade 
{
	public static void main(String[] args) throws IOException 
	{
	  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	  String inData;
	  double grade = 0;
	  double count = 0;
	  
	  do
	  {
	    
	    System.out.println("Enter the letter grade");
	    inData = br.readLine();
	     if (inData.equals("a"))
	     {
	       grade = grade + 4.0;
	     } 
	     else if (inData.equals("b"))
	     {
		grade = grade + 3.0;
	     }
	     else if (inData.equals("c"))
	     {
	   	grade = grade + 2;
	     }
	    else if (inData.equals("d"))
	    {
		grade = grade + 1;
	    }
	   else if (inData.equals("f"))
	   {
		grade = grade + 0;
	   }
	   count++;
	  
	}
	

	while  (!inData.equals(""));

	System.out.println("GPA = " + (grade / (count - 1)));
	
        }
}
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.