Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Method Revision
Problem
we want to use a java program to analysis the students grades for
csc111 exam, The maximum capacity of the section is 40. the user
should enter number of students first (not larger than 40 ) , then the
grades. Then the program should print the equivalent grade out of
15, also the program should print maximum grade, minimum grade,
average grade. finally the program should print grade distribution
for mid term as bar chart
Problem
Sample Run
input :
Enter the number of student less than 40 :
25
Enter 25 grades
67
89
76
…
…
67
output :
The equivalent grade are :
10 13 11 ……………. 11
lowest grade is 4 and the Highest grade is 14
the Average grade is 8.75
Overall grade distribution:
00-04 : ****
05-09 : *************
10-14: *****
15
:*
analysis
input :
number of students (num) (should be validated )
list of grades (grades)
processing
calculate equivalent grades
compute average
find maximum grade
find minimum grade
output :
equivalent grades
lowest grade and the Highest grade
the Average grade
grade Distribution
using method
Using methods has several advantages:
While working on one method, you can focus on just that part of the
program and construct it, debug it, and perfect it.
Different people can work on different methods simultaneously.
If a method is needed in more than one place in a program, or in
different programs, you can write it once and use it many times.
Using methods greatly enhances the program’s readability because it
reduces the complexity of the method main.
readGrades
printGrades
calculateEquGrade
findMaxGrade
findMinGrade
calculateAvg
printBarChart
flow chart
Required Methods
Design
start program
average computeAverage(eqGrades)
initialize maxCapacity with 40
print eqGrades
initialize grades [] with size 40
print max
initialize eqGrades [] with size 40
do {read num }while (num>40)
read grades(grades)
print min
print Average
printBarChart (eqGrades)
for each grade(i)
end program
eqGrades[i]=calculateEquGrades (grades[i])
maxFindmaxGrade(eqGrades)
min FindMinGrade(eqGrades)
Program Structure
import java.util.Scanner;
public class gradeBook {
static Scanner console=new Scanner (System.in);
static final int maxCapcity = 40;
static int num;
public static void main (String args []){
//declaration
//input
//processing
//output
}
//methods declaration
}//end of class
writing method
The name of the method
The number of parameters, if any
The data type of each parameter
The data type of the value computed (that is, the value
returned) by the method.
what should the method do. (method body)
read grades
Read grades in an Array
public static void readGrades(int [] gradeList) {
for (int count = 0; count < num ; count++)
gradeList[count] = console.nextInt();
}
Print grades from an Array
public static void printGrades(int [] gradeList) {
for (int count = 0; count < num ; count++)
System.out.print(gradeList[i]);;
System.out.println();
}
find equivalent
The name of the method (findEquGrade)
The number of parameters (grade)
The data type of each parameter (grade int )
The data type of the value computed by the method. (eqGrade
int)
eqGrade = grad/100.0 * 15 should be rounded to nearest int
eqGrade =Math .round(grad/100.0 * 15.0)
find equivalent
public static int findEquGrade(int grade) {
int eqGrade =(int)Math .round(grad/100.0 * 15.0);
return eqGrade ;
}
find maximum grade
name : findMaxGrade
parameter : grades
parameters data type : int []
return data type : max int
find maximum grade
public static int findMaxGrade (int [] grades )
{
int max = grades [0];
for (int i= 1; i< num; i++)
max=Math.max(max , grades[i]);
return max ;
}
find minimum grade
public static int findMinGrade (int [] grades )
{
int min =grades [0];
for (int I = 1; i< num; i++)
min=Math.min(min , grades[i]);
return min ;
}
Calculate average
The name of the method (calculateAvg)
The number of parameters ( grades )
The data type of each parameter (grades int [])
The data type of the value computed by the method. (Average double)
method body :
initialize sum with 0
for each grade(i) in grades
sum+=grades[i];
Average =sum/grades.length
Calculate average
public static double calculateAvg (int [] grades )
{
int sum=0;
for (int i=0;i<num;i++)
sum+=grades[i];
return sum*1.0/num;
}
grade distribution
Suppose the equivalent grades are
11, 10, 7, 9, 15, 13, 14, 6, 4, 3
one grades of 15
four grades 10-14
three grades 9-5
two grades 4-0
we need to stores this grade distribution in an array of four elements:
each element corresponding to a category of grades
e.g. frequentArray[0] indicates the number of grade in the range 0-4
calculate grade distribution
if (grades[i] ==15)
0
0
0-4
1
0
5-9
2
0
10-14
3
0
15
++frequentArray[3];
else if (grades[i] <=14 && grades[i]>=10 )
++frequentArray[2];
else if (grades[i] <=9 && grades[i]>=5 )
++frequentArray[1];
else if (grades[i] <=4 && grades[i]>=0)
++frequentArray[0];
calculate grade distribution
to calculate grade distribution we need to use frequency array
find the relation between index (0,1,2,3) and grade distribution
index 0->{0,1,2,3,4}
index 1->{5,6,7,8,9}
For grades: 11, 10, 7, 9, 15, 13, 14, 6, 4, 3
index 2->{10,11,12,13,14}
0-4
0
2
index 3->{15}
++frequentArray[ grade[i] /5];
1
3
5-9
2
4
10-14
3
1
15
print barChart
public static void printBarChart (int grades[])
{
// find grades distribution
Int freq[]=new int [4];
for (int i=0;i<num; i++)
++freq[grades[i]/5];
//for each freq element, output a bar of chart
for (int i=0; i<freq.length; i++)
{
// output label (“00-04 ”,” ”)
if (i==3) System.out.printf(“%5d: ”, 15);
else System.out.printf(“%02d-%02d: ”,i*5, i*5+4);
//print bar of asterisks
for (int s=0; s<freq[i]; s++)
{System.out.print(“*”);}
System.out.println();//start new line of output
}//end outer for
}// end method
print barChart for specific range
public static void printBarChart
(int grades[], int start, int end )
{// find grades distribution
int freq=0;
//for each freq element, output
a bar of chart
for (int i=0;i<num; i++)
{
if (grades[i] <=end&&
grades[i]>=start)
++freq;
}
// output label (“00-04 ”,” ”)
if(start==end )
System.out.printf(“%5d: ”,end);
else System.out.printf(“%02d%02d: ”,start, end);
//print bar of asterisks
for (int
s=0;s<freq;s++){System.out.print(“*”
);
}
System.out.println();//start new line
of output }//end outer for
}// end method
variable and data type
input :
num int
grades int []
processing
eqGrades int []
average double
max int
min int
//processing
// find equivalent array
for (int i=0;i<grades.length; i++)
import java.util.Scanner;
eqGrades[i]= findEquGrades(grades[i]);
public class gradeBook {
static Scanner console=new Scanner (System.in); //find Maximum grade
static final int maxCapcity =40;
max =findMaxGrade (eqGrades);
static int num;
//find Minimum grade
public static void main (String args []){
min =findMinGrade (eqGrades);
//declaration
// calculate average
int max ,min ;
average=calculateAvg(eqGrades);
int [] grades, eqGrades;
//output
grades =new int [maxCapcity];
//print equivalent grades
eqGrades=new int [maxCapcity];
System.out.println(“The equivalent grades are”
double average;
printGrades(eqGrades);
//input
System.out.printf(“lowest grade is %d and the
do {
Highest grade is %d %n
System.out.println(“Enter number of students”);
the Average grade is %.2f ”, min, max, average);
num= console.nextInt();
System.out.println(“The Overall grade
while (num>40);
distribution :”);
//read grades from user
//print bar chart
System.out.println(“Enter the grades ”);
printBarChart (eqGrades);
readGrades(grades);
} //end main
// method declaration
public static void readGrades(int [] gradeList) {
for (int count = 0; count < num; count++)
gradeList[count] = console.nextInt();}
public static int findEquGrades(int grade) {
int eqGrade =(int)Math .round(grade/100.0 *
15.0);
return eqGrade ;}
public static int findMaxGrade (int [] grades )
{int max =grades [0];
for (int i=1;i<num;i++)
max=Math.max(max,grades[i]);
return max ;}
public static int findMinGrade (int [] grades )
{int min =grades [0];
for (int i=1;i<num;i++)
min=Math.min(min,grades[i]);
return min ;}
public static void printGrades(int [] grades )
{for (int i=0;i<num;i++)
System.out.print(grades[i]+” ”);
System.out.println();}
public static void printBarChart (int grades[])
{// find grades distribution
int freq[]=new int [4];
for (int i=0;i<num; i++)
++freq[grades[i]/5];
//for each freq element, output a bar of chart
for (int i=0;i<freq.length; i++)
{// output label (“00-04 ”,” ”)
if(i==3) System.out.printf(“%5d: ”,15);
else System.out.printf(“%02d-%02d: ”,i*5, i*5+4
//print bar of asterisks
for (int s=0;s<freq[i];s++)
{System.out.print(“*”);}
System.out.println();//start new line of output
}//end outer for }// end method
public static double calculateAvg (int [] grades )
{int sum=0;
for (int i=0;i<num;i++)
sum+=grades[i];
return sum*1.0/grades.length;
//return (double) sum/grades.length;}