Chapter 2_lecture Note
Chapter 2_lecture Note
___________________________________
MOBILE APPLICATION
PROGRAMMING AND DESIGN
___________________________________
Chapter 2: Java and Android programing
MS. DAO TO HIEU
Office: A4 704 Mobile: 0389959524
Email: [email protected]
___________________________________
18/08/2023 1
___________________________________
___________________________________
___________________________________
Android Project
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
Slide 4 WHY JAVA? ___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 4
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 5
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
✓ Everything outside the 8 primitives is an Object (defined by a
class)
✓ Comment a line with // or use /* and */ to create a block
comment.
✓ non-control (program flow) statements end with a “;”
___________________________________
18/08/2023 6
___________________________________
___________________________________
___________________________________
Slide 7 VARIABLES ___________________________________
• Piece of memory
✓ Primitive: int, double, boolean,…
___________________________________
✓ Class: CreatbySenSor, GetTime, …
• Memory and a name for the variable
✓ Classes: usable instance does not exist
yet (variable is null)
✓ Primitives: variable has a default value
___________________________________
(0 or false)
Example:
ElapsedTime runtime;
ElapsedTime runTime = new ElapsedTime(); ___________________________________
18/08/2023 7
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
Category
byte
Types
+127 to -128
Values Example
byte b = 65;
___________________________________
char c = 'A';
char All Unicode characters
char c = 65;
short +32,767 to -32,768 short s = 65;
Integer
+2,147,483,647 to -
int int i = 65;
long
2,147,483,648
+9,223,372,036,854,775,807 to
-9,223,372,036,854,775,808
long l = 65L; ___________________________________
float 3.402,823,5 E+38 to 1.4 E-45 float f = 65f;
Floating-
1.797,693,134,862,315,7 E+308
point double double d = 65.55;
to 4.9 E-324
boolean false, true boolean b = true;
Other
void -- --
___________________________________
18/08/2023 9
___________________________________
___________________________________
___________________________________
Slide 10 MODIFIERS ___________________________________
Access
Modifiers
Non-Access
Modifiers
Other Modifiers ___________________________________
• Public • Static (class- • Transient
• Private level) not • native
•
•
Protected
default
instance-level.
• Finalabstract
• strictfp ___________________________________
• Synchronized
• Volatile
___________________________________
___________________________________
___________________________________
❑ Local Scope
public class LocalScopeExample { public static void main(String[] args) {
int localVar = 10; // only main System.out.println(localVar); //} }
___________________________________
❑ Instance Scope
public class InstanceScopeExample {
int instanceVar = 20; // variable instance, anywhere in this class
public void printInstanceVar() {
System.out.println(instanceVar); }
public static void main(String[] args) {
InstanceScopeExample obj = new InstanceScopeExample();
obj.printInstanceVar(); // call method print instance } }
___________________________________
❑ Class Scope
public class ClassScopeExample {
static int classVar = 30; // variable class, anywhere in program
public static void main(String[] args) {
System.out.println(classVar); // In ra giá trị của biến class } }
___________________________________
18/08/2023 11
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
Slide 13 CLASS IN JAVA ___________________________________
Definition Attributes
Methods ___________________________________
Object Constructor
Creation
Access ___________________________________
Modifiers
Polymorphism
Inheritance Encapsulation
___________________________________
18/08/2023 13
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
Slide 16 INHERITANCE/SUBCLASSING ___________________________________
Example of Inheritance in Java:
class Vehicle {
void start() { ___________________________________
System.out.println("Vehicle is starting..."); } }
___________________________________
___________________________________
___________________________________
Input/Output
Processing
___________________________________
Data
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 18
___________________________________
___________________________________
___________________________________
Slide 19 CONTROL STRUCTURES JAVA ___________________________________
switch(expression) {
case constant1:
___________________________________
code_block1();
break; //exit
case constant-expression:
code_block2();
___________________________________
break; //exit
default : //Optional
}
default_code;
___________________________________
18/08/2023 19
___________________________________
___________________________________
___________________________________
switch(expression) {
case constant1:
___________________________________
code_block1();
break; //exit
case constant-expression:
code_block2();
___________________________________
break; //exit
default : //Optional
}
default_code;
___________________________________
18/08/2023 20
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023
Create new project with Empty Activity 21
___________________________________
___________________________________
___________________________________
Slide 22 PROJECT STARTLOGO ___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023
Name and location 22
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 23
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 24
___________________________________
___________________________________
___________________________________
Slide 25 PROJECT STARTLOGO ___________________________________
Create an image (or vector) icon named "icon.png" (or "icon.xml") and save it in the
drawable directory. Path: ".../ScreenWaitingStart/app/src/main/res/drawable/".
___________________________________
___________________________________
___________________________________
18/08/2023 25
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 26
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 27
___________________________________
___________________________________
___________________________________
Slide 28 PROJECT STARTLOGO ___________________________________
Create a styles.xml file in the values directory. Navigate to
".../ScreenWaitingStart/app/src/main/res/values/".
___________________________________
___________________________________
___________________________________
18/08/2023 28
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 29
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 30
___________________________________
___________________________________
___________________________________
Slide 31 PROJECT STARTLOGO ___________________________________
Add the line android:theme="@style/AppTheme" after the "StartActivity" entry.
___________________________________
___________________________________
___________________________________
18/08/2023 31
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 32
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
18/08/2023 33
___________________________________
___________________________________
___________________________________