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
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
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
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