Presentation is loading. Please wait.

Presentation is loading. Please wait.

Method Revision.

Similar presentations


Presentation on theme: "Method Revision."— Presentation transcript:

1 Method Revision

2 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

3 Problem Sample Run input : Enter the number of student less than 40 :
25 Enter 25 grades 67 89 76

4 output : The equivalent grade are : 10 13 11 ……………. 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: ***** : *

5 analysis input : processing output :
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

6 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.

7 flow chart Required Methods readGrades printGrades calculateEquGrade
findMaxGrade findMinGrade calculateAvg printBarChart

8 Design average computeAverage(eqGrades) print eqGrades print max
start program initialize maxCapacity with 40 initialize grades [] with size 40 initialize eqGrades [] with size 40 do {read num }while (num>40) read grades(grades) for each grade(i) eqGrades[i]=calculateEquGrades (grades[i]) maxFindmaxGrade(eqGrades) min FindMinGrade(eqGrades) average computeAverage(eqGrades) print eqGrades print max print min print Average printBarChart (eqGrades) end program

9 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

10 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)

11 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) { System.out.print(gradeList[i]);; System.out.println();

12 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)

13 find equivalent public static int findEquGrade(int grade) {
int eqGrade =(int)Math .round(grad/100.0 * 15.0); return eqGrade ; }

14 find maximum grade name : findMaxGrade parameter : grades
parameters data type : int [] return data type : max  int

15 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 ; }

16 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 ; }

17 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

18 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; }

19 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

20 calculate grade distribution
if (grades[i] ==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]; 0-4 1 5-9 2 10-14 3 15

21 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} index 3->{15} ++frequentArray[ grade[i] /5]; 2 0-4 1 3 5-9 4 10-14 15

22 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

23 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

24 variable and data type input : processing num int grades int []
eqGrades int [] average  double max  int min  int

25 //processing // find equivalent array for (int i=0;i<grades.length; i++) eqGrades[i]= findEquGrades(grades[i]); //find Maximum grade max =findMaxGrade (eqGrades); //find Minimum grade min =findMinGrade (eqGrades); // calculate average average=calculateAvg(eqGrades); //output //print equivalent grades System.out.println(“The equivalent grades are”); printGrades(eqGrades); System.out.printf(“lowest grade is %d and the Highest grade is %d %n the Average grade is %.2f ”, min, max, average); System.out.println(“The Overall grade distribution :”); //print bar chart printBarChart (eqGrades); } //end main 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 int max ,min ; int [] grades, eqGrades; grades =new int [maxCapcity]; eqGrades=new int [maxCapcity]; double average; //input do { System.out.println(“Enter number of students”); num= console.nextInt(); while (num>40); //read grades from user System.out.println(“Enter the grades ”); readGrades(grades);

26 // 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]; 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;}


Download ppt "Method Revision."

Similar presentations


Ads by Google