Student Grade Report Java Program
Java Code:
import java.util.Scanner;
public class grade {
public static void average(int[] array){
double sum=0;
int count=0;
double average=0;
for(int i=0; i<array.length; i++){
sum+=array[i];
count++;
}
average=sum/count;
System.out.print(average);
}
public static void main(String[] args)
{
Scanner
input = new Scanner(System.in);
System.out.print("Enter the
number of students: ");
int NoS =
input.nextInt();
String[]
names = new String[NoS];
int[] array = new int[NoS];
int i=0;
int Score;
while(i<NoS){
System.out.print("Enter the
test Score: ");
Score
= input.nextInt();
if(Score<0 || Score
>100){
System.out.print("incorrect
score, Re-enter the score-(must be positive and between 0 to 100):");
Score
= input.nextInt();
}
array[i]=Score;
System.out.print("Enter the
Student Name: ");
String
name = input.next();
names[i]=name;
i++;
}
int h=array[0];
int high=0;
int j=0;
while(j<array.length){
if(h>=array[j]){
high=
h;
j++;
}
else{
h=array[j];
}
}
int l=array[0];
int low=0;
int k=0;
while(k<array.length){
if(l<=array[k]){
low=l;
k++;
}
else{
l=array[k];
}
}
int ind=0;
int n=0;
int m=0;
while(ind<array.length){
if(array[ind]==high){
n=ind;
}
if(array[ind]==low){
m=ind;
}
ind++;
}
System.out.print("\n");
System.out.println("Test Analysis
Program");
System.out.println("High score:
"+
names[n] +":
"+
high);
System.out.println("Low score:
"+
names[m] +":
"+
low);
System.out.print("Class average
is"
+ "
");
average(array);
int grade=0;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
while(grade<array.length){
if(array[grade]>=90){
a+=1;
}
if(array[grade]>=80
&& array[grade]<90){
b+=1;
}
if(array[grade]>=70
&& array[grade]<80){
c+=1;
}
if(array[grade]>=60
&& array[grade]<70){
d+=1;
}
if(array[grade]>=0
&& array[grade]<60){
e+=1;
}
grade++;
}
System.out.print("\n");
System.out.println("Grade
Distribution");
System.out.println("90-100:
" +
a);
System.out.println("80-89: " + b);
System.out.println("70-79: " + c);
System.out.println("60-69: " + d);
System.out.println("Below
60:"
+ e);
}
}
public class Distribution {
ReplyDeleteprivate int totalA;
private int totalB;
private int totalC;
private int totalD;
private int totalF;
public Distribution() {
totalA = 0;
totalB = 0;
totalC = 0;
totalD = 0;
totalF = 0;
}
void addGrade(int grade) {
if (0 <= grade && grade < 65) totalF++;
else if (65 <= grade && grade < 70) totalD++;
else if (70 <= grade && grade < 80) totalC++;
else if (80 <= grade && grade < 90) totalB++;
else totalA++;
}
int getGradeCount(int num) {
int gradeCount;
if (num == 1) { gradeCount = totalA; }
else if (num == 2) { gradeCount = totalB; }
else if (num == 3) { gradeCount = totalC; }
else if (num == 4) { gradeCount = totalD; }
else { gradeCount = totalF; }
return gradeCount;
}
char getGradeLetter(int num) {
char gradeLetter;
if (num == 1) { gradeLetter = 'A'; }
else if (num == 2) { gradeLetter = 'B'; }
else if (num == 3) { gradeLetter = 'C'; }
else if (num == 4) { gradeLetter = 'D'; }
else { gradeLetter = 'F'; }
return gradeLetter;
}
int getTotalGrades() {
int total = totalA + totalB + totalC + totalD + totalF;
return total;
}
String makeHeader() {
String s = "";
for (int label = 0; label <= 10; label++) {
s += 10 * label;
if (label != 10) {
if (label == 0) s += " ";
s += " ";
}
}
s += "%\n";
for (int line = 0; line <= 10; line++) {
s += "| ";
}
s += "\n";
s += " ";
for (int i = 0; i < 50; i++) {
s += '*';
}
return s;
}
String makeBar(int barNum) {
int gradeCount = getGradeCount(barNum);
double percent;
double barCount;
int wholePercent;
int wholeBarCount;
String bar = " ";
percent = (double) gradeCount / (double) getTotalGrades();
barCount = 50 * percent;
wholePercent = (int)(100 * percent + 0.5);
wholeBarCount = (int)(barCount + 0.5);
for (int s = 0; s < wholeBarCount; s++) {
bar += "*";
}
bar += " " + getGradeLetter(barNum) + ' ' + wholePercent + "%";
return bar;
}
public String toString() {
String graph = "";
graph += makeHeader() + "\n";
for (int gradeNum = 1; gradeNum <= 5; gradeNum++) {
graph += makeBar(gradeNum) + "\n";
}
graph += "Total number of grades: " + getTotalGrades();
return graph;
}
}