Method Revision Problem we want to use a
Method Revision
Problem we want to use a java program to analysis the students grades for csc 111 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 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.
read. Grades print. Grades calculate. Equ. Grade find. Max. Grade find. Min. Grade calculate. Avg print. Bar. Chart flow chart Required Methods
Design start program average compute. Average(eq. Grades) initialize max. Capacity with 40 print eq. Grades initialize grades [] with size 40 print max initialize eq. Grades [] with size 40 do {read num }while (num>40) read grades(grades) for each grade(i) eq. Grades[i]=calculate. Equ. Grades (grades[i]) max Findmax. Grade(eq. Grades) min Find. Min. Grade(eq. Grades) print min print Average print. Bar. Chart (eq. Grades) end program
Program Structure import java. util. Scanner; public class grade. Book { static Scanner console=new Scanner (System. in); static final int max. Capcity = 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 read. Grades(int [] grade. List) { for (int count = 0; count < num ; count++) grade. List[count] = console. next. Int(); } Print grades from an Array public static void print. Grades(int [] grade. List) { for (int count = 0; count < num ; count++) System. out. print(grade. List[i]); ; System. out. println(); }
find equivalent The name of the method (find. Equ. Grade) The number of parameters (grade) The data type of each parameter (grade int ) The data type of the value computed by the method. (eq. Grade int) eq. Grade = grad/100. 0 * 15 should be rounded to nearest int eq. Grade =Math. round(grad/100. 0 * 15. 0)
find equivalent public static int find. Equ. Grade(int grade) { int eq. Grade =(int)Math. round(grad/100. 0 * 15. 0); return eq. Grade ; }
find maximum grade name : find. Max. Grade parameter : grades parameters data type : int [] return data type : max int
find maximum grade public static int find. Max. Grade (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 find. Min. Grade (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 (calculate. Avg) 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 calculate. Avg (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. frequent. Array[0] indicates the number of grade in the range 0 -4
calculate grade distribution if (grades[i] ==15) ++frequent. Array[3]; else if (grades[i] <=14 && grades[i]>=10 ) ++frequent. Array[2]; else if (grades[i] <=9 && grades[i]>=5 ) ++frequent. Array[1]; else if (grades[i] <=4 && grades[i]>=0) ++frequent. Array[0]; 0 0 0 -4 1 0 5 -9 2 0 10 -14 3 0 15
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} ++frequent. Array[ grade[i] /5]; 1 3 5 -9 2 4 10 -14 3 1 15
print bar. Chart public static void print. Bar. Chart (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(“%5 d: ”, 15); else System. out. printf(“%02 d-%02 d: ”, 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 bar. Chart for specific range public static void print. Bar. Chart (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(“%5 d: ”, end); else System. out. printf(“%02 d: ”, 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 eq. Grades int [] average double max int min int
//processing // find equivalent array for (int i=0; i<grades. length; i++) import java. util. Scanner; eq. Grades[i]= find. Equ. Grades(grades[i]); public class grade. Book { static Scanner console=new Scanner (System. in); //find Maximum grade static final int max. Capcity =40; max =find. Max. Grade (eq. Grades); static int num; //find Minimum grade public static void main (String args []){ min =find. Min. Grade (eq. Grades); //declaration // calculate average int max , min ; average=calculate. Avg(eq. Grades); int [] grades, eq. Grades; //output grades =new int [max. Capcity]; //print equivalent grades eq. Grades=new int [max. Capcity]; System. out. println(“The equivalent grades are” double average; print. Grades(eq. Grades); //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 %. 2 f ”, min, max, average) num= console. next. Int(); System. out. println(“The Overall grade while (num>40); distribution : ”); //read grades from user //print bar chart System. out. println(“Enter the grades ”); print. Bar. Chart (eq. Grades); read. Grades(grades); } //end main
// method declaration public static void read. Grades(int [] grade. List) { for (int count = 0; count < num; count++) grade. List[count] = console. next. Int(); } public static int find. Equ. Grades(int grade) { int eq. Grade =(int)Math. round(grade/100. 0 * 15. 0); return eq. Grade ; } public static int find. Max. Grade (int [] grades ) {int max =grades [0]; for (int i=1; i<num; i++) max=Math. max(max, grades[i]); return max ; } public static int find. Min. Grade (int [] grades ) {int min =grades [0]; for (int i=1; i<num; i++) min=Math. min(min, grades[i]); return min ; } public static void print. Grades(int [] grades ) {for (int i=0; i<num; i++) System. out. print(grades[i]+” ”); System. out. println(); } public static void print. Bar. Chart (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(“%5 d: ”, 15); else System. out. printf(“%02 d-%02 d: ”, i*5+ //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 calculate. Avg (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; }
- Slides: 26