Java™ How to Program ( Deitel - Deitel) - Phần 13
Số trang: 50
Loại file: pdf
Dung lượng: 33.88 MB
Lượt xem: 15
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu java™ how to program ( deitel - deitel) - phần 13, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Java™ How to Program ( Deitel - Deitel) - Phần 1312 courseName = name; // initialize courseName13 grades = gradesArray; // store grades14 } // end two-argument GradeBook constructor1516 // method to set the course name17 public void setCourseName( String name )18 {19 courseName = name; // store the course name20 } // end method setCourseName2122 // method to retrieve the course name23 public String getCourseName()24 {25 return courseName;26 } // end method getCourseName2728 // display a welcome message to the GradeBook user29 public void displayMessage()30 {31 // getCourseName gets the name of the course32 System.out.printf( Welcome to the grade book for %s! ,33 getCourseName() );34 } // end method displayMessage3536 // perform various operations on the data37 public void processGrades()38 {39 // output grades array40 outputGrades();4142 // call methods getMinimum and getMaximum43 System.out.printf( %s %d %s %d ,44 Lowest grade in the grade book is, getMinimum(),45 Highest grade in the grade book is, getMaximum() );4647 // output grade distribution chart of all grades on all tests48 outputBarChart();49 } // end method processGrades5051 // find minimum grade52 public int getMinimum()53 {54 // assume first element of grades array is smallest55 int lowGrade = grades[ 0 ][ 0 ];5657 // loop through rows of grades array58 for ( int studentGrades[] : grades )59 {60 // loop through columns of current row61 for ( int grade : studentGrades )62 {63 // if grade less than lowGrade, assign it to lowGrade64 if ( grade < lowGrade )65 lowGrade = grade;66 } // end inner for67 } // end outer for6869 return lowGrade; // return lowest grade70 } // end method getMinimum7172 // find maximum grade73 public int getMaximum()74 {75 // assume first element of grades array is largest76 int highGrade = grades[ 0 ][ 0 ];7778 // loop through rows of grades array79 for ( int studentGrades[] : grades )80 {81 // loop through columns of current row82 for ( int grade : studentGrades )83 {84 // if grade greater than highGrade, assign it to highGrade85 if ( grade > highGrade )86 highGrade = grade;87 } // end inner for88 } // end outer for8990 return highGrade; // return highest grade91 } // end method getMaximum9293 // determine average grade for particular set of grades94 public double getAverage( int setOfGrades[] )95 {96 int total = 0; // initialize total9798 // sum grades for one student99 for ( int grade : setOfGrades )100 total += grade;101102 // return average of grades103 return (double) total / setOfGrades.length;104 } // end method getAverage105106 // output bar chart displaying overall grade distribution107 public void outputBarChart()108 {109 System.out.println( Overall grade distribution: );110111 // stores frequency of grades in each range of 10 grades112 int frequency[] = new int [ 11 ];113114 // for each grade in GradeBook, increment the appropriate frequency115 for ( int studentGrades[] : grades )116 {117 for ( int grade : studentGrades )118 ++frequency[ grade / 10 ];119 } // end outer for120121 // for each grade frequency, print bar in chart122 for ( int count = 0; count < frequency.length; count++ )123 {124 // output bar label ( 00-09: , ..., 90-99: , 100: )125 if ( count == 10 )126 System.out.printf( %5d: , 100 );127 else128 System.out.printf( %02d-%02d: ,129 count * 10, count * 10 + 9 );130131 // print bar of asterisks132 for ( int stars = 0; stars < frequency[ count ]; stars++ )133 System.out.print( * );134135 System.out.println(); // start a new line of output136 } // end outer for137 } // end method outputBarChart138139 // output the contents of the grades array140 public void outputGrades()141 {142 System.out.println( The grades are: );143 System.out.print( ); // align column heads144145 // create a column heading for each of the tests146 for ( int test = 0; test < grades[ 0 ].length; test++ )147 System.out.printf( Test %d , test + 1 );148149 System.out.println( Average ); // student average column heading150151 // create rows/columns of text representing array grades152 for ( int student = 0; student < grades.length; s ...
Nội dung trích xuất từ tài liệu:
Java™ How to Program ( Deitel - Deitel) - Phần 1312 courseName = name; // initialize courseName13 grades = gradesArray; // store grades14 } // end two-argument GradeBook constructor1516 // method to set the course name17 public void setCourseName( String name )18 {19 courseName = name; // store the course name20 } // end method setCourseName2122 // method to retrieve the course name23 public String getCourseName()24 {25 return courseName;26 } // end method getCourseName2728 // display a welcome message to the GradeBook user29 public void displayMessage()30 {31 // getCourseName gets the name of the course32 System.out.printf( Welcome to the grade book for %s! ,33 getCourseName() );34 } // end method displayMessage3536 // perform various operations on the data37 public void processGrades()38 {39 // output grades array40 outputGrades();4142 // call methods getMinimum and getMaximum43 System.out.printf( %s %d %s %d ,44 Lowest grade in the grade book is, getMinimum(),45 Highest grade in the grade book is, getMaximum() );4647 // output grade distribution chart of all grades on all tests48 outputBarChart();49 } // end method processGrades5051 // find minimum grade52 public int getMinimum()53 {54 // assume first element of grades array is smallest55 int lowGrade = grades[ 0 ][ 0 ];5657 // loop through rows of grades array58 for ( int studentGrades[] : grades )59 {60 // loop through columns of current row61 for ( int grade : studentGrades )62 {63 // if grade less than lowGrade, assign it to lowGrade64 if ( grade < lowGrade )65 lowGrade = grade;66 } // end inner for67 } // end outer for6869 return lowGrade; // return lowest grade70 } // end method getMinimum7172 // find maximum grade73 public int getMaximum()74 {75 // assume first element of grades array is largest76 int highGrade = grades[ 0 ][ 0 ];7778 // loop through rows of grades array79 for ( int studentGrades[] : grades )80 {81 // loop through columns of current row82 for ( int grade : studentGrades )83 {84 // if grade greater than highGrade, assign it to highGrade85 if ( grade > highGrade )86 highGrade = grade;87 } // end inner for88 } // end outer for8990 return highGrade; // return highest grade91 } // end method getMaximum9293 // determine average grade for particular set of grades94 public double getAverage( int setOfGrades[] )95 {96 int total = 0; // initialize total9798 // sum grades for one student99 for ( int grade : setOfGrades )100 total += grade;101102 // return average of grades103 return (double) total / setOfGrades.length;104 } // end method getAverage105106 // output bar chart displaying overall grade distribution107 public void outputBarChart()108 {109 System.out.println( Overall grade distribution: );110111 // stores frequency of grades in each range of 10 grades112 int frequency[] = new int [ 11 ];113114 // for each grade in GradeBook, increment the appropriate frequency115 for ( int studentGrades[] : grades )116 {117 for ( int grade : studentGrades )118 ++frequency[ grade / 10 ];119 } // end outer for120121 // for each grade frequency, print bar in chart122 for ( int count = 0; count < frequency.length; count++ )123 {124 // output bar label ( 00-09: , ..., 90-99: , 100: )125 if ( count == 10 )126 System.out.printf( %5d: , 100 );127 else128 System.out.printf( %02d-%02d: ,129 count * 10, count * 10 + 9 );130131 // print bar of asterisks132 for ( int stars = 0; stars < frequency[ count ]; stars++ )133 System.out.print( * );134135 System.out.println(); // start a new line of output136 } // end outer for137 } // end method outputBarChart138139 // output the contents of the grades array140 public void outputGrades()141 {142 System.out.println( The grades are: );143 System.out.print( ); // align column heads144145 // create a column heading for each of the tests146 for ( int test = 0; test < grades[ 0 ].length; test++ )147 System.out.printf( Test %d , test + 1 );148149 System.out.println( Average ); // student average column heading150151 // create rows/columns of text representing array grades152 for ( int student = 0; student < grades.length; s ...
Tìm kiếm tài liệu theo từ khóa liên quan:
java cơ bản giáo trình java thủ thuật lập trình java nhập môn java mẹo lập trình javaTài liệu liên quan:
-
Bài giảng Nhập môn Java: Bài 12 – Võ Tấn Dũng
12 trang 60 0 0 -
Đề cương môn học Lập trình Java
28 trang 50 0 0 -
Giáo trình Java (Tập 2): Phần 2
429 trang 26 0 0 -
Java™ How to Program ( Deitel - Deitel) - Phần 1
50 trang 25 0 0 -
Giáo trình java cơ bản - Chương 5
45 trang 24 0 0 -
193 trang 24 0 0
-
Bài giảng Nhập môn Java: Bài 8 – Võ Tấn Dũng
50 trang 23 0 0 -
Giáo trình Java (Tập 1): Phần 2
157 trang 22 0 0 -
Java™ How to Program ( Deitel - Deitel) - Phần 17
50 trang 22 0 0 -
LTHDT - Bài 06. Một số kỹ thuật trong kế thừa
35 trang 21 0 0 -
Bài giảng Nhập môn Java: Bài 10 – Võ Tấn Dũng
46 trang 21 0 0 -
26 trang 21 0 0
-
Giáo trình java cơ bản - Chương 9
22 trang 21 0 0 -
Bài giảng Nhập môn Java: Bài 9 – Võ Tấn Dũng
47 trang 21 0 0 -
Kỹ thuật lập trình - Chapter 3
15 trang 20 0 0 -
Java™ How to Program ( Deitel - Deitel) - Phần 6
50 trang 20 0 0 -
Bài giảng Nhập môn Java: Bài 7 – Võ Tấn Dũng
35 trang 20 0 0 -
LTHDT- Bài 08. Ngoại lệ và xử lý ngoại lệ
55 trang 20 0 0 -
Giáo trình java cơ bản - Chương 10
12 trang 20 0 0 -
LTHDT- Bài 10. Biểu đồ use case và biểu đồ hoạt động
32 trang 20 0 0