Create a Class that represents a grade distribution for a gi
Create a Class that represents a grade distribution for a given course. Write methods to perform each of the following tasks:
Set the number of each of the letter grades A, B, C, D and F.
Return the number of each of the letter grades A, B, C, D and F.
Return the total number of grades.
Return the percentage of each letter grade as a whole number between 0 and 100, inclusive.
Draw a bar graph of the grade distribution.
The graph will have ve bars, one per grade. Each bar can be a horizontal row of asterisks, such that the number of asterisks in a row is proportionate to the percentage of grades in each category. Let one asterisk represent 2 percent, so 50 asterisks correspond to 100 percent. Mark the horizontal axis at 10 percent increments from 0 to 100 percent, and label each line with its letter grade.
For example, if the grades are 1 A, 4 B\'s, 6 C\'s, 2 D\'s and 1 F, the total number of grades is 14, the percentage of A\'s is 7, the percentage of B\'s is 29, the percentage of C\'s is 43, the percentage of D\'s is 14, and the percentage of F\'s is 7. The A row would contain 4 asterisks (7 percent of 50 rounded o to the nearest integer), the B row 14, the C row 21, the D row 7 and the F row 4. The graph would look like this:
0 10 20 30 40 50 60 70 80 90 100%
| | | | | | | | | | |
***************************************************
**** A
************** B
********************* C
******* D
**** F
1.1 First steps Write your getters and setters rst. Work on the drawGraph() method last. You will need to write separate class with a main method to create a GradeDistribution object to run this code.
please follow the intructions step by step and using JAVA solve the problems. Make sure the code is working and with the comments shows how to the code working.
Solution
package myProject;
import java.util.*;
//Class defined
class GradeGraphDemo
{
//Array to store all grade count
double grade[] = new double[5];
//Accept each grade and calculate the frequency
void acceptGrade()
{
int c;
//Scanner class object created
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter Grade: N to stop entering grade \ \");
//Loops till grade is n / N
do
{
//Accepts grade
char ch = sc.next().charAt(0);
//If grade is \'A\' increment the position for grade A
if(ch == \'A\' || ch == \'a\')
grade[0]++;
//If grade is \'B\' increment the position for grade B
else if(ch == \'B\' || ch == \'b\')
grade[1]++;
//If grade is \'C\' increment the position for grade C
else if(ch == \'C\' || ch == \'c\')
grade[2]++;
//If grade is \'D\' increment the position for grade D
else if(ch == \'D\' || ch == \'d\')
grade[3]++;
//If grade is \'F\' increment the position for grade F
else if(ch == \'F\' || ch == \'f\')
grade[4]++;
//If grade is \'N\'/\'n\' stop accepting
else if(ch == \'N\' || ch == \'n\')
break;
//For invalid grade
else
System.out.println(\"Wrong Grade. Please reenter\");
}while(true);
}//End of method
//Returns grade A frequency
double gradeA()
{
return grade[0];
}
//Returns grade B frequency
double gradeB()
{
return grade[1];
}
//Returns grade C frequency
double gradeC()
{
return grade[2];
}
//Returns grade D frequency
double gradeD()
{
return grade[3];
}
//Returns grade F frequency
double gradeF()
{
return grade[4];
}
//Returns total grade entered
double totalGrade()
{
double tot = 0;
for(int x = 0; x < 5; x++)
tot += grade[x];
return tot;
}
//Returns percentage of Grade A
double percentA()
{
return (((gradeA()*100)/totalGrade()));
}
//Returns percentage of Grade B
double percentB()
{
return (((gradeB()*100)/totalGrade()));
}
//Returns percentage of Grade C
double percentC()
{
return (((gradeC()*100)/totalGrade()));
}
//Returns percentage of Grade D
double percentD()
{
return (((gradeD()*100)/totalGrade()));
}
//Returns percentage of Grade F
double percentF()
{
return (((gradeF()*100)/totalGrade()));
}
//Displays the graph
void drawGraph()
{
System.out.println(\"0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%\");
System.out.println(\"| | | | | | | | | | |\");
for(int x = 0; x < totalGrade()*2; x++)
System.out.print(\"*\");
System.out.println();
for(int x = 0; x < Math.ceil(percentA()*50/100); x++)
System.out.print(\'*\');
System.out.println(\"A\");
for(int x = 0; x < Math.ceil(percentB()*50/100); x++)
System.out.print(\'*\');
System.out.println(\"B\");
for(int x = 0; x < Math.ceil(percentC()*50/100); x++)
System.out.print(\'*\');
System.out.println(\"C\");
for(int x = 0; x < Math.ceil(percentD()*50/100); x++)
System.out.print(\'*\');
System.out.println(\"D\");
for(int x = 0; x < Math.ceil(percentF()*50/100); x++)
System.out.print(\'*\');
System.out.println(\"F\");
}//End of method
}//End of class
//Driver class
class GradeGraph
{
//Main method
public static void main(String ss[])
{
GradeGraphDemo gd = new GradeGraphDemo();
gd.acceptGrade();
gd.drawGraph();
}//End of main method
}//End of class
Output:
Enter Grade: N to stop entering grade
p
Wrong Grade. Please reenter
a
b
b
b
b
c
c
c
c
c
c
d
d
f
n
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
| | | | | | | | | | |
****************************
****A
***************B
**********************C
********D
****F



