We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,599 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

JAVA Grade Class Average Program

I am creating a java program that will take 10 user input grades from 1-100 and display them as a letter grade, then calculate the percent of how many letter grades there are in total.

The last step is to figure out how to get each letter grade, lets say 5 F's, 2 B's, 2 C's, and 1 A, and get what total percentage of each letter grade there is out of ten so like 50% F's, 20% B's, etc. What do I do next? I assume I have to assign some doubles for the percent to display but my brain is fried right now.

Here is what I have so far:

import java.util.Scanner;
public class GradePercent {
public static void main(String[] args)
{

int g1, g2, g3, g4, g5, g6, g7, g8, g9, g10;
char grade;

System.out.println("Type 10 grades in whole numbers from 0-100");
System.out.println();

Scanner keyboard = new Scanner(System.in);
g1 = keyboard.nextInt( );
g2 = keyboard.nextInt( );
g3 = keyboard.nextInt( );
g4 = keyboard.nextInt( );
g5 = keyboard.nextInt( );
g6 = keyboard.nextInt( );
g7 = keyboard.nextInt( );
g8 = keyboard.nextInt( );
g9 = keyboard.nextInt( );
g10 = keyboard.nextInt( );

if (g1 >= 90) {
grade = 'A';
} else if (g1 >= 80) {
grade = 'B';
} else if (g1 >= 70) {
grade = 'C';
} else if (g1 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);

if (g2 >= 90) {
grade = 'A';
} else if (g2 >= 80) {
grade = 'B';
} else if (g2 >= 70) {
grade = 'C';
} else if (g2 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g3 >= 90) {
grade = 'A';
} else if (g3 >= 80) {
grade = 'B';
} else if (g3 >= 70) {
grade = 'C';
} else if (g3 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g4 >= 90) {
grade = 'A';
} else if (g4 >= 80) {
grade = 'B';
} else if (g4 >= 70) {
grade = 'C';
} else if (g4 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g5 >= 90) {
grade = 'A';
} else if (g5 >= 80) {
grade = 'B';
} else if (g5 >= 70) {
grade = 'C';
} else if (g5 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g6 >= 90) {
grade = 'A';
} else if (g6 >= 80) {
grade = 'B';
} else if (g6 >= 70) {
grade = 'C';
} else if (g6 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g7 >= 90) {
grade = 'A';
} else if (g7 >= 80) {
grade = 'B';
} else if (g7 >= 70) {
grade = 'C';
} else if (g7 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g8 >= 90) {
grade = 'A';
} else if (g8 >= 80) {
grade = 'B';
} else if (g8 >= 70) {
grade = 'C';
} else if (g8 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g9 >= 90) {
grade = 'A';
} else if (g9 >= 80) {
grade = 'B';
} else if (g9 >= 70) {
grade = 'C';
} else if (g9 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
if (g10 >= 90) {
grade = 'A';
} else if (g10 >= 80) {
grade = 'B';
} else if (g10 >= 70) {
grade = 'C';
} else if (g10 >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
3
Contributors
7
Replies
2 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
crazybeaner
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

put counters in your if-else statements so you can monitor how many A,B,C,D,E,and F you have.

to compute for the percentage, divide the number of times you got each letter by 10.
then multiply by 100%.

for example, you have 5 A's.
5/10 = 0.5 * 100% = 50%;

if you want a copy of the code that I have made, just reply to this post, but i used arrays, so my code will be shorter.

mitchiexlolz
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Well what I am having troubles with is getting the count of how many of each grade there is into a variable in order to divide and then multiply. The way I did it takes the user input grade number and divides and multiplies which is incorrect. I assume that the counter stores the count of each letter grade in order to divide by 10, but I can't find anything on assigning variables to a counter in my text book. As far as I know I'm not allowed to use arrays neither unless there is no other possible way of completing this task.

What I was thinking was doing something like

double gr1 = (g1/10)*100

which returns as lets say a score of 50 will turn into 500%. I need lets say 5 A's to count at 50%. Care to hint me on that?

crazybeaner
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

first, you should initialize 5 variables to hold your counters and initialize each value to zero.,
say

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

then, in your if-else statements, put these counters.

if (g1 >= 90)
{
grade = 'A';
A=A+1;
}
else if (g1 >= 80)
{
grade = 'B';
B=B+1;
}
else if (g1 >= 70)
{
grade = 'C';
C=C+1;
} else if (g1 >= 60) {
grade = 'D';
D=D+1;
}
else
{
grade = 'F';
F=F+1;
}
System.out.println("Grade = " + grade);

put these counters in all of your codes.

mitchiexlolz
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hey thanks a lot man that's exactly what I was trying to learn about. That pretty much completes my program. Thanks for helping me, as you can tell I'm still a beginner at programming haha.

crazybeaner
Newbie Poster
4 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

you are welcome!:)

mitchiexlolz
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Congrats on completing your assignment.
If you are interested, there's a better way to approach this because any time you see 10 almost-identical variables and 10 almost-identical blocks of code you should be thinking about using an array and a loop.
So instead of g1, g2, g3 etc you would have int g[10] and you main code would look like:

for (int i = 0; i<g.length; i++) {
  if (g[i] >= 90) {
     grade = 'A';
  } else if (g[i]>= 80) { ...

Just think what would happen if your next assignment is to do the exactly the same thing but for 200 students...

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

i agree. that is exactly what I told him to do, but as you can see, they are not yet allowed to use arrays for their assignment.

mitchiexlolz
Newbie Poster
15 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0783 seconds using 2.72MB