Simple Java Grade Program - Calculating Exam Scores and Grades

  • Context: Java 
  • Thread starter Thread starter ali11
  • Start date Start date
  • Tags Tags
    Java Program
Click For Summary

Discussion Overview

The discussion revolves around a Java program intended to calculate and display exam scores and corresponding letter grades based on user input. Participants are addressing issues in the code, including logic errors, variable declarations, and input handling.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the variable 'grade' is declared as an int but is being assigned character values, which leads to logical errors.
  • Another participant points out that the input string is not being parsed correctly to extract grades, suggesting the use of methods like hasNextInt and nextInt from the Scanner class.
  • A participant proposes a revised version of the code that attempts to correctly count letter grades based on user input, but it contains issues such as undeclared variables and missing braces in loops.
  • Concerns are raised about using a double to store the number of grades, with suggestions to use an int instead.
  • Participants discuss the redundancy of the 'letterGrade' variable and suggest simplifying the code by directly incrementing grade counters.
  • There is a question regarding the logic of incrementing grade counters within if-else blocks, with some clarification provided about the purpose of counting grades.

Areas of Agreement / Disagreement

Participants generally agree that there are multiple issues with the code, but there is no consensus on the best approach to resolve them. Various suggestions and corrections are proposed, but disagreements on specific implementations remain.

Contextual Notes

Limitations include unresolved variable declarations, potential logic errors in grade counting, and the need for proper input handling. The discussion reflects various coding practices and preferences without reaching a definitive solution.

Who May Find This Useful

Individuals interested in Java programming, particularly those learning about input handling, conditional logic, and debugging code related to grade calculations.

ali11
Messages
12
Reaction score
0
Hi i am looking for following output.so far I am only getting result for total no of grades.I am getting 0 for A B C D and F.
total no of grades
Number of A's
Number of B
Numbers of C
Number of D
number of F
import java.util.Scanner;
public class ExamScores
{
public static void main (String[]args)
{
String a;
int count= 0;

int grade=0;
int A=0;
int B=0;
int C=0;
int D=0;
int F=0;




Scanner scan = new Scanner (System.in);
System.out.println("Enter your exam grades");


a=scan.nextLine();
int len=a.length ();

for (int i=0; i< len; i++)
{
if (a.charAt (i) ==' '){
count--;
}else {
count++;

}
}




if (grade > 90 && grade <= 100) {
grade= 'A';
A=A+1;
count++;
} else if (grade > 80 && grade <= 89) {
grade= 'B';
B=B+1;
count++;

} else if (grade > 70 && grade <= 79) {
grade= 'C';
C=C+1;
count++;

} else if (grade > 60 && grade <= 69) {
grade= 'D';
D=D+1;
count++;
} else if (grade> 0 && grade <= 59) {
grade= 'F';
F=F+1;
count++;
}
System.out.println("total no of grades=" + count);
System.out.println("Number of A=" + A);
System.out.println("Number of B=" + B);
System.out.println("Number of C=" + C);
System.out.println("Number of D=" + D);
System.out.println("Number of F=" + F);
}
}
 
Technology news on Phys.org
There are several problems with your code.
1. grade is declared as an int and is initialized to 0, but in your if statements, you are attempting to store a character value in it.
2. grade should be given a value from the input string, but your code doesn't do this. None of the conditions in your if statements evaluate to true, so none of the code in the if statements actually executes.
3. Your for loop counts the nonspace characters in the input string. This is not the same as the number of grades in the string.
4. You never get the grades out of the input string. This should happen inside a loop, which should also contain your logic for determining whether a given grade is an A, B, and so on. The Scanner class has some methods that will be helpful; namely, hasNextInt and nextInt. See http://download.oracle.com/javase/7/docs/api/.
 
i make some changings
Code:
import java.util.Scanner;
public class ExamScores
{
	public static void main (String[]args)
	{
		String letterGrade;
		double numberOfGrades;
		double grade;
		//create scanner object
		Scanner scan = new Scanner(System.in);
		//ask user for input
		System.out.println("How many grades do you have to enter?: ");
		//reads in the number of grades user wants to enter as double from keyboard
		numberOfGrades = scan.nextDouble();
		//now since program knows how many grades are being entered it can
		//control the input
		for(int j = 1; j <= numberOfGrades; j++)
		   //based off of number of grades the user inputs it will ask
		   //for that many exam grades
		   System.out.println("Enter your exam grades: ");
		   //reads in exam scores as a double from keyboard
		   grade = scan.nextDouble();
		   //compares the datafield grade with scores 90-100
		   if (grade >= 90 && grade <= 100) {
		      //if 90 >= grade <=100 the letter grade is A
		      letterGrade = "A";
		      //if letter grade is A then A which is an int that is initialized at 0
		      //has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
		      if(letterGrade == "A"){
		         A = A + 1;
		      }

		    if (grade >= 80 && grade <= 89) {
		      //if 90 >= grade <=100 the letter grade is A
		      letterGrade = "B";}
		    //if letter grade is A then A which is an int that is initialized at 0
		    //has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
		    if(letterGrade == "B"){
		       B = B + 1;
	                 }
		    if (grade >= 70 && grade <= 79) {
		      //if 90 >= grade <=100 the letter grade is A
		      letterGrade = "C";}
		    //if letter grade is A then A which is an int that is initialized at 0
		    //has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
		    if(letterGrade == "C"){
		       C = C + 1;
		    }
		    if (grade >= 60 && grade <= 69) {
		        //if 90 >= grade <=100 the letter grade is A
		        letterGrade = "D";}
		    //if letter grade is A then A which is an int that is initialized at 0
		    //has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
		    if(letterGrade == "D"){
		        D = D + 1;
		    }
		    if (grade >= 0 && grade <= 59) {
		        //if 90 >= grade <=100 the letter grade is A
		        letterGrade = "F";}
		    //if letter grade is A then A which is an int that is initialized at 0
		    //has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
		    if(letterGrade == "F"){
		        F = F + 1;
		    }

		   System.out.println("Total number of each letter grade: " + numberOfGrades);
		   System.out.println("Number of A" + A);
		   System.out.println("Number of B" +B);
		   System.out.println("Number of C" + C);
		   System.out.println("Number of D" + D);
		   System.out.println ("Number of F" + F);

	}

   }
}
 
Last edited by a moderator:
Some comments.
1. Your code won't compile - there are variables used that aren't declared.
2. The for loop won't work as you expect, as you are missing braces ({ }) around the body of the loop. The structure of the for loop should be like this.
Code:
for(int j = 1; j <= numberOfGrades; j++)
{
   // Body of loop
}
3. When you ask the user how many grades will be entered, you shouldn't be using a double to store that value. The user will not be entering 7.36 grades. Use an int.
4. If the grades themselves are whole numbers, use ints to store them.
5. Here is one of your grade-determining branches.
Code:
if (grade >= 90 && grade <= 100) {
   //if 90 >= grade <=100 the letter grade is A
   letterGrade = "A";
   //if letter grade is A then A which is an int that is initialized at 0
   //has 1 added to it. THIS IS WHERE YOUR VALUE CHANGES.
   if(letterGrade == "A"){
        A = A + 1;
   }
All of the branches have exactly the same comments. The comments should be updated for each of the five sections.
There is no need for a letterGrade variable. You can trim your code down considerably to this:
Code:
if (grade >= 90 && grade <= 100)
{
   A_count = A_count + 1;
}
 
Why are you taking A=A+1, B=B+1, C=C+1, D=D+1, E=E+1, F=F+1 in If, else-if blocks?
Code:
if (grade > 90 && grade <= 100) {
grade= 'A';
A=A+1;
count++;
} else if (grade > 80 && grade <= 89) {
grade= 'B';
B=B+1;
count++;

} else if (grade > 70 && grade <= 79) {
grade= 'C';
C=C+1;
count++;

} else if (grade > 60 && grade <= 69) {
grade= 'D';
D=D+1;
count++;
} else if (grade> 0 && grade <= 59) {
grade= 'F';
F=F+1;
count++;
}
 
pairofstrings said:
Why are you taking A=A+1, B=B+1, C=C+1, D=D+1, E=E+1, F=F+1 in If, else-if blocks?

Presumably, to count the number of people with each grade.

If your question really meant "Why write A=A+1 rather than A++", they both do the same thing.
 
pairofstrings,
I have deleted two of your posts, as you are giving too much help. Do not try to post a complete or near-complete solution again.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
Replies
3
Views
3K