SlideShare a Scribd company logo
Introduction to
         Java Programming
                Y. Daniel Liang
Edited by Hoàng Văn Hậu – VTC Academy – THSoft co.,ltd
 https://play.google.com/store/apps/developer?id=THSoft+Co.,Ltd
Introduction
 Course Objectives
 Organization of the Book




VTC Academy       THSoft Co.,Ltd   2
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
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 projects




VTC Academy                 THSoft Co.,Ltd              4
Session 02: Control statement

 Swith  Case statement
 While, do while statement
 For statement
 Continue, break, return
 Array in Java
 String in Java
 Exception and debuging


VTC Academy        THSoft Co.,Ltd   5
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 + Space



VTC Academy                  THSoft Co.,Ltd    6
switch Statement Flow Chart

                              7                                              default
                                                numOfYears



                                15                                        30

annualInterestRate=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
switch Statement Rules
The switch-expression must yield a value of char, byte, short, or
int type and must always be enclosed in parentheses.

The value1, ..., and valueN must have the same data type as the
value of the switch-expression. The resulting statements in the
case statement are executed when the value in the case
statement matches the value of the switch-expression. (The case
statements are executed in sequential order.)

The keyword break is optional, but it should be used at the end of
each case in order to terminate the remainder of the switch
statement. If the break statement is not present, the next case
statement will be executed.



    VTC Academy              THSoft Co.,Ltd                  8
switch Statement Rules, cont.

The default case, which is optional, can be used to
perform actions when none of the specified cases
is true.
The order of the cases (including the default case)
does not matter. However, it is a good
programming style to follow the logical sequence of
the cases and place the default case at the end.




 VTC Academy         THSoft Co.,Ltd             9
Actions on Eclipse
 Open         Eclipse IDE
    – Create project: Session2Ex
    – Create java class: Ex2WithSwitch.java




              Source                      Run



VTC Academy              THSoft Co.,Ltd         10
Repetitions
 while         Loops
   do-while Loops
 for         Loops
   break and continue




VTC Academy              THSoft Co.,Ltd   11
while Loop Flow Chart
while (continuation-condition) {
// loop-body;
}
                                                         false
                                         Continuation
                                          condition?


                                         true

                                          Statement(s)




Eclipse shortcut key:                        Next
w + Ctrl + Space                           Statement

    VTC Academy         THSoft Co.,Ltd                           12
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
do-while Loop
do {                                            Statement(s)


  // Loop body;
                                         true
} while (continue-condition);                    Continue
                                                 condition?


                                                          false

                                                   Next
                                                 Statement




Eclipse shortcut key:
d + Ctrl + Space

   VTC Academy          THSoft Co.,Ltd                         14
for Loops
for (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
for Loop Flow Chart
for (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
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                              Run



VTC Academy                      THSoft Co.,Ltd         17
Arrays
int[] 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
Multidimensional Arrays
Declaring Variables of Multidimensional Arrays and
Creating Multidimensional Arrays

int[][] matrix = new int[10][10];
 or
int 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
Multidimensional Array Illustration
      0 1    2    3   4        0 1     2   3   4       0    1       2
0                         0                        0   1    2        3


1                         1                        1   4    5        6


2                         2        7               2   7        8    9

3                         3                        3   10   11       12


4                         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
Declaring, Creating, and Initializing Using
              Shorthand Notations
You 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
Lengths of Multidimensional
               Arrays
int[][] array = {
  {1, 2, 3},
  {4, 5, 6},
  {7, 8, 9},
  {10, 11, 12}
};
array.length
array[0].length
array[1].length
array[2].length

                                     Illustration on code
VTC Academy         THSoft Co.,Ltd                    22
Actions on Eclipse
   Open Eclipse IDE
    –   Create project: Session2Ex
    –   Create java class: Ex2WithArray.java
    –   Add String type
    –   Change rule of games




              Source                            Run



VTC Academy                    THSoft Co.,Ltd         23
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     b15
a 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    b25
a 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    b35
a 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    b45
a 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
Example 2.2 (cont) Adding and
                     Multiplying Two Matrices
a11 a12 a13 a14 a15        b11 b12 b13 b14 b15   c11 c12 c13 c14 c15
a 21 a 22 a 23 a 24 a 25   b21 b22 b23 b24 b25   c 21 c 22 c 23 c 24 c 25
a 31 a 32 a 33 a 34 a 35   b31 b32 b33 b34 b35   c 31 c 32 c 33 c 34 c 35
a 41 a 42 a 43 a 44 a 45   b41 b42 b43 b44 b45   c 41 c 42 c 43 c 44 c 45
a 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
Action on class
 Teacher
    – hauc2@yahoo.com
    – 0984380003
    – https://play.google.com/store/search?q=thsoft+co&c=apps

 Captions
 Members




VTC Academy                  THSoft Co.,Ltd                     26

More Related Content

PPT
04 slide loops
PPT
JavaYDL4
TXT
Acciones para AmigoBot
PPT
E:\Plp 2009 2\Plp 9
PPT
Cantonese Opera As Intangible Cultural Heritage
PPTX
F# for C# devs - SDD 2015
PPT
Ch2 Liang
PDF
Chapter 1. java programming language overview
04 slide loops
JavaYDL4
Acciones para AmigoBot
E:\Plp 2009 2\Plp 9
Cantonese Opera As Intangible Cultural Heritage
F# for C# devs - SDD 2015
Ch2 Liang
Chapter 1. java programming language overview

Similar to bai giang java co ban - java cơ bản - bai 2 (20)

PDF
Chapter 00 revision
PPT
bai giang java co ban - java cơ bản - bai 1
PPTX
Core java
PPT
Java basic tutorial by sanjeevini india
PPT
Java basic tutorial by sanjeevini india
PPTX
Java introduction
PDF
Android Application Development - Level 3
PPT
PPT
Java Programming: Loops
PDF
C sharp chap4
PPT
Cso gaddis java_chapter4
PPT
Java if and else
PPTX
Seminar 2 coding_principles
PPT
M C6java6
PDF
Corejava Online 100
PPTX
Switch case and looping
PPTX
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
PPTX
Advanced Computer Programming..pptx
PPT
Iteration
PPT
Unit I Advanced Java Programming Course
Chapter 00 revision
bai giang java co ban - java cơ bản - bai 1
Core java
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Java introduction
Android Application Development - Level 3
Java Programming: Loops
C sharp chap4
Cso gaddis java_chapter4
Java if and else
Seminar 2 coding_principles
M C6java6
Corejava Online 100
Switch case and looping
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
Advanced Computer Programming..pptx
Iteration
Unit I Advanced Java Programming Course
Ad

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation theory and applications.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Chapter 3 Spatial Domain Image Processing.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Programs and apps: productivity, graphics, security and other tools
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Network Security Unit 5.pdf for BCA BBA.
Encapsulation theory and applications.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4
20250228 LYD VKU AI Blended-Learning.pptx
The AUB Centre for AI in Media Proposal.docx
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Big Data Technologies - Introduction.pptx
Ad

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

  • 1. Introduction to Java Programming Y. Daniel Liang Edited by Hoàng Văn Hậu – VTC Academy – THSoft co.,ltd https://play.google.com/store/apps/developer?id=THSoft+Co.,Ltd
  • 2. Introduction  Course Objectives  Organization of the Book VTC Academy THSoft Co.,Ltd 2
  • 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. 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 projects VTC Academy THSoft Co.,Ltd 4
  • 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 debuging VTC Academy THSoft Co.,Ltd 5
  • 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 + Space VTC Academy THSoft Co.,Ltd 6
  • 7. switch Statement Flow Chart 7 default numOfYears 15 30 annualInterestRate=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. switch Statement Rules The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. (The case statements are executed in sequential order.) The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. VTC Academy THSoft Co.,Ltd 8
  • 9. switch Statement Rules, cont. The default case, which is optional, can be used to perform actions when none of the specified cases is true. The order of the cases (including the default case) does not matter. However, it is a good programming style to follow the logical sequence of the cases and place the default case at the end. VTC Academy THSoft Co.,Ltd 9
  • 10. Actions on Eclipse  Open Eclipse IDE – Create project: Session2Ex – Create java class: Ex2WithSwitch.java Source Run VTC Academy THSoft Co.,Ltd 10
  • 11. Repetitions  while Loops  do-while Loops  for Loops  break and continue VTC Academy THSoft Co.,Ltd 11
  • 12. while Loop Flow Chart while (continuation-condition) { // loop-body; } false Continuation condition? true Statement(s) Eclipse shortcut key: Next w + Ctrl + Space Statement VTC Academy THSoft Co.,Ltd 12
  • 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. do-while Loop do { Statement(s) // Loop body; true } while (continue-condition); Continue condition? false Next Statement Eclipse shortcut key: d + Ctrl + Space VTC Academy THSoft Co.,Ltd 14
  • 15. for Loops for (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. for Loop Flow Chart for (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. 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 Run VTC Academy THSoft Co.,Ltd 17
  • 18. Arrays int[] 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. Multidimensional Arrays Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int[][] matrix = new int[10][10]; or int 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. Multidimensional Array Illustration 0 1 2 3 4 0 1 2 3 4 0 1 2 0 0 0 1 2 3 1 1 1 4 5 6 2 2 7 2 7 8 9 3 3 3 10 11 12 4 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. Declaring, Creating, and Initializing Using Shorthand Notations You 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. Lengths of Multidimensional Arrays int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; array.length array[0].length array[1].length array[2].length Illustration on code VTC Academy THSoft Co.,Ltd 22
  • 23. Actions on Eclipse  Open Eclipse IDE – Create project: Session2Ex – Create java class: Ex2WithArray.java – Add String type – Change rule of games Source Run VTC Academy THSoft Co.,Ltd 23
  • 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 b15 a 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 b25 a 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 b35 a 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 b45 a 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. Example 2.2 (cont) Adding and Multiplying Two Matrices a11 a12 a13 a14 a15 b11 b12 b13 b14 b15 c11 c12 c13 c14 c15 a 21 a 22 a 23 a 24 a 25 b21 b22 b23 b24 b25 c 21 c 22 c 23 c 24 c 25 a 31 a 32 a 33 a 34 a 35 b31 b32 b33 b34 b35 c 31 c 32 c 33 c 34 c 35 a 41 a 42 a 43 a 44 a 45 b41 b42 b43 b44 b45 c 41 c 42 c 43 c 44 c 45 a 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. Action on class  Teacher – hauc2@yahoo.com – 0984380003 – https://play.google.com/store/search?q=thsoft+co&c=apps  Captions  Members VTC Academy THSoft Co.,Ltd 26