Method Revision.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Computer Programming Lab(5).
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Chapter 5 Loops.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4: Control Structures II
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter VII: Arrays.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
CSC111 Quick Revision.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 6 More Conditionals and Loops
Repetition-Sentinel,Flag Loop/Do_While
TK1114 Computer Programming
SELECTION STATEMENTS (1)
CSC 142 Computer Science II
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Chapter 6 More Conditionals and Loops
CSS161: Fundamentals of Computing
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Building Java Programs
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 6: User-Defined Functions I
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
Building Java Programs
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Array Review Selection Sort
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

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 …

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

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.

flow chart Required Methods readGrades printGrades calculateEquGrade findMaxGrade findMinGrade calculateAvg printBarChart

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

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) { 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) ++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

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

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 : processing num int grades int [] eqGrades int [] average  double max  int min  int

//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);

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