bai giang java co ban - java cơ bản - bai 2

586 views
519 views

Published on

bài giảng java cơ bản, bài 2/5.

Nơi giảng: VTC academy
Người giảng: Hoàng Văn Hậu

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
586
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

bai giang java co ban - java cơ bản - bai 2

  1. 1. Introduction to Java Programming Y. Daniel LiangEdited by Hoàng Văn Hậu – VTC Academy – THSoft co.,ltd https://play.google.com/store/apps/developer?id=THSoft+Co.,Ltd
  2. 2. Introduction Course Objectives Organization of the BookVTC Academy THSoft Co.,Ltd 2
  3. 3. Course Objectives Upon completing the course, you will understand – Create, compile, and run Java programs – Primitive data types – Java control flow – Methods – Arrays (for teaching Java in two semesters, this could be the end) – Object-oriented programming – Core Java classes (Swing, exception, internationalization, multithreading, multimedia, I/O, networking, Java Collections Framework)VTC Academy THSoft Co.,Ltd 3
  4. 4. Course Objectives, cont. You will be able to – Develop programs using Eclipse IDE – Write simple programs using primitive data types, control statements, methods, and arrays. – Create and use methods – Write interesting projectsVTC Academy THSoft Co.,Ltd 4
  5. 5. Session 02: Control statement Swith Case statement While, do while statement For statement Continue, break, return Array in Java String in Java Exception and debugingVTC Academy THSoft Co.,Ltd 5
  6. 6. switch Statements switch (year) { case 7: annualInterestRate = 7.25; break; case 15: annualInterestRate = 8.50; break; case 30: annualInterestRate = 9.0; break; default: System.out.println( "Wrong number of years, enter 7, 15, or 30"); } Eclipse shortcut key: S + Ctrl + SpaceVTC Academy THSoft Co.,Ltd 6
  7. 7. switch Statement Flow Chart 7 default numOfYears 15 30annualInterestRate=7.25 annualInterestRate=8.50 annualInterestRate=9.0 System.out.println("Wrong number of " + "years, enter 7, 15, or 30"); System.exit(0); Next Statement VTC Academy THSoft Co.,Ltd 7
  8. 8. switch Statement RulesThe switch-expression must yield a value of char, byte, short, orint type and must always be enclosed in parentheses.The value1, ..., and valueN must have the same data type as thevalue of the switch-expression. The resulting statements in thecase statement are executed when the value in the casestatement matches the value of the switch-expression. (The casestatements are executed in sequential order.)The keyword break is optional, but it should be used at the end ofeach case in order to terminate the remainder of the switchstatement. If the break statement is not present, the next casestatement will be executed. VTC Academy THSoft Co.,Ltd 8
  9. 9. switch Statement Rules, cont.The default case, which is optional, can be used toperform actions when none of the specified casesis true.The order of the cases (including the default case)does not matter. However, it is a goodprogramming style to follow the logical sequence ofthe cases and place the default case at the end. VTC Academy THSoft Co.,Ltd 9
  10. 10. Actions on Eclipse Open Eclipse IDE – Create project: Session2Ex – Create java class: Ex2WithSwitch.java Source RunVTC Academy THSoft Co.,Ltd 10
  11. 11. Repetitions while Loops do-while Loops for Loops break and continueVTC Academy THSoft Co.,Ltd 11
  12. 12. while Loop Flow Chartwhile (continuation-condition) {// loop-body;} false Continuation condition? true Statement(s)Eclipse shortcut key: Nextw + Ctrl + Space Statement VTC Academy THSoft Co.,Ltd 12
  13. 13. while Loop Flow Chart, cont. i = 0;int i = 0;while (i < 100) { false (i < 100) System.out.println( "Welcome to Java!"); i++; true} System.out.println("Welcoem to Java!"); i++; Next Statement VTC Academy THSoft Co.,Ltd 13
  14. 14. do-while Loopdo { Statement(s) // Loop body; true} while (continue-condition); Continue condition? false Next StatementEclipse shortcut key:d + Ctrl + Space VTC Academy THSoft Co.,Ltd 14
  15. 15. for Loopsfor (initial-action; loop-continuation-condition; action-after-each-iteration) { //loop body;}int i = 0;while (i < 100) { System.out.println("Welcome to Java! ” + i); i++;}Example:int i;for (i = 0; i < 100; i++) { System.out.println("Welcome to Java! ” + i);} VTC Academy THSoft Co.,Ltd 15
  16. 16. for Loop Flow Chartfor (initial-action; Initial-Action loop-continuation-condition; action-after-each-iteration) { //loop body;} false Action-After- Continuation Each-Iteration condition? true Statement(s) (loop-body) Next Eclipse shortcut key: Statement f + Ctrl + Space VTC Academy THSoft Co.,Ltd 16
  17. 17. Actions on Eclipse Open Eclipse IDE – Create project: Session2Ex – Create java class: Ex2WithWhile.java – Change the rule of games. – Exception input – Example debug project Source RunVTC Academy THSoft Co.,Ltd 17
  18. 18. Arraysint[] sourceArray = {2, 3, 1, 5, 10};int[] targetArray = new int[sourceArray.length];float[] f = new float[20];Set data to array:for (int i = 0; i < sourceArrays.length; i++) targetArray[i] = sourceArray[i]; VTC Academy THSoft Co.,Ltd 18
  19. 19. Multidimensional ArraysDeclaring Variables of Multidimensional Arrays andCreating Multidimensional Arraysint[][] matrix = new int[10][10]; orint matrix[][] = new int[10][10];matrix[0][0] = 3;for (int i=0; i<matrix.length; i++) for (int j=0; j<matrix[i].length; j++) { matrix[i][j] = (int)(Math.random()*1000); }double[][] x; VTC Academy THSoft Co.,Ltd 19
  20. 20. Multidimensional Array Illustration 0 1 2 3 4 0 1 2 3 4 0 1 20 0 0 1 2 31 1 1 4 5 62 2 7 2 7 8 93 3 3 10 11 124 4 int[][] array = { {1, 2, 3},matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; VTC Academy THSoft Co.,Ltd 20
  21. 21. Declaring, Creating, and Initializing Using Shorthand NotationsYou can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example,int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};This is equivalent to the following statements:int[][] array = new int[4][3];array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;array[3][0] = 10; array[3][1] = 11; array[3][2] = 12; VTC Academy THSoft Co.,Ltd 21
  22. 22. Lengths of Multidimensional Arraysint[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};array.lengtharray[0].lengtharray[1].lengtharray[2].length Illustration on codeVTC Academy THSoft Co.,Ltd 22
  23. 23. Actions on Eclipse Open Eclipse IDE – Create project: Session2Ex – Create java class: Ex2WithArray.java – Add String type – Change rule of games Source RunVTC Academy THSoft Co.,Ltd 23
  24. 24. Action on Eclipse Example 2.1 Adding and Multiplying Two Matrices  Objective: Use two-dimensional arrays to create two matrices, and then add and multiply the two matrices.a11 a12 a13 a14 a15 b11 b12 b13 b14 b15 a11 b11 a12 b12 a13 b13 a14 b14 a15 b15a 21 a 22 a 23 a 24 a 25 b21 b22 b23 b24 b25 a 21 b21 a 22 b22 a 23 b23 a 24 b24 a 25 b25a 31 a 32 a 33 a 34 a 35 b31 b32 b33 b34 b35 a 31 b31 a 32 b32 a 33 b33 a 34 b34 a 35 b35a 41 a 42 a 43 a 44 a 45 b41 b42 b43 b44 b45 a 41 b41 a 42 b42 a 43 b43 a 44 b44 a 45 b45a 51 a 52 a 53 a 54 a 55 b51 b52 b53 b54 b55 a 51 b51 a 52 b52 a 53 b53 a 54 b54 a 55 b55 TestMatrixOperation Run VTC Academy THSoft Co.,Ltd 24
  25. 25. Example 2.2 (cont) Adding and Multiplying Two Matricesa11 a12 a13 a14 a15 b11 b12 b13 b14 b15 c11 c12 c13 c14 c15a 21 a 22 a 23 a 24 a 25 b21 b22 b23 b24 b25 c 21 c 22 c 23 c 24 c 25a 31 a 32 a 33 a 34 a 35 b31 b32 b33 b34 b35 c 31 c 32 c 33 c 34 c 35a 41 a 42 a 43 a 44 a 45 b41 b42 b43 b44 b45 c 41 c 42 c 43 c 44 c 45a 51 a 52 a 53 a 54 a 55 b51 b52 b53 b54 b55 c 51 c 52 c 53 c 54 c 55 cij = ai1 b1j+ai2 b2j+ai3 b3j+ai4 b4j+ai5 b5j VTC Academy THSoft Co.,Ltd 25
  26. 26. Action on class Teacher – hauc2@yahoo.com – 0984380003 – https://play.google.com/store/search?q=thsoft+co&c=apps Captions MembersVTC Academy THSoft Co.,Ltd 26

×