SlideShare une entreprise Scribd logo
1  sur  26
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

Contenu connexe

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

Similaire à bai giang java co ban - java cơ bản - bai 2 (20)

Java™ (OOP) - Chapter 4: "Loops"
Java™ (OOP) - Chapter 4: "Loops"Java™ (OOP) - Chapter 4: "Loops"
Java™ (OOP) - Chapter 4: "Loops"
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
04slidemicroemulsions microemulsions microemulsions microemulsions microemuls...
 
Chapter 00 revision
Chapter 00 revisionChapter 00 revision
Chapter 00 revision
 
bai giang java co ban - java cơ bản - bai 1
bai giang java co ban - java cơ bản - bai 1bai giang java co ban - java cơ bản - bai 1
bai giang java co ban - java cơ bản - bai 1
 
Lecture05(control structure part ii)
Lecture05(control structure part ii)Lecture05(control structure part ii)
Lecture05(control structure part ii)
 
L8
L8L8
L8
 
Loops
LoopsLoops
Loops
 
Lo43
Lo43Lo43
Lo43
 
09
0909
09
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Java if and else
Java if and elseJava if and else
Java if and else
 
control statements
control statementscontrol statements
control statements
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Priming Java for Speed at Market Open
Priming Java for Speed at Market OpenPriming Java for Speed at Market Open
Priming Java for Speed at Market Open
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020Week2 ch4 part1edited 2020
Week2 ch4 part1edited 2020
 
Core java
Core javaCore java
Core java
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
PVS-Studio Meets Octave
PVS-Studio Meets Octave PVS-Studio Meets Octave
PVS-Studio Meets Octave
 

Dernier

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Dernier (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

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