Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Java cơ bản java co ban
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 03: Method and Class
Methods
Object-Oriented Programming
Classes
Packages
Subclassingand Inheritance
Polymorphism
VTC Academy THSoft Co.,Ltd 5
6. Introducing Methods
Method Structure
A method is a
collection of
statements that are
grouped together
to perform an
operation.
VTC Academy THSoft Co.,Ltd 6
7. Introducing Methods, cont.
•parameter profile refers to the type, order, and
number of the parameters of a method.
•method signature is the combination of the
method name and the parameter profiles.
•The parameters defined in the method header
are known as formal parameters.
•When a method is invoked, its formal
parameters are replaced by variables or data,
which are referred to as actual parameters.
VTC Academy THSoft Co.,Ltd 7
8. Declaring Methods
public static int max(int num1,
int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
VTC Academy THSoft Co.,Ltd 8
9. Calling Methods, cont.
pass i
pass j
public static void main(String[] args) { public static int max(int num1, int num2) {
int i = 5; int result;
int j = 2;
int k = max(i, j); if (num1 > num2)
result = num1;
System.out.println( else
"The maximum between " + i + result = num2;
" and " + j + " is " + k);
} return result;
}
VTC Academy THSoft Co.,Ltd 9
10. Calling Methods, cont.
The main method pass 5 The max method
i: 5 num1: 5
pass 2
parameters
j: 2 num2: 2
k: 5 result: 5
VTC Academy THSoft Co.,Ltd 10
11. CAUTION
A return statement is required for a nonvoid
method. The following method is logically
correct, but it has a compilation error, because the
Java compiler thinks it possible that this method
does not return any value.
public static int xMethod(int n) {
if (n > 0) return 1;
else if (n == 0) return 0;
else if (n < 0) return –1;
}
To fix this problem, delete if (n<0) in the code.
VTC Academy THSoft Co.,Ltd 11
12. Actions on Eclipse
Open Eclipse IDE
– Create project: Session3Ex
– Create package: com.vtcacademy.exopp
– Create java class: Ex3WithOpp.java (with main method)
– Create java class: Circle.java
Source Run
VTC Academy THSoft Co.,Ltd 12
13. The Math Class
Class constants:
– PI
–E
Class methods:
– Trigonometric Methods
– Exponent Methods
– Rounding Methods
– min, max, abs, and random Methods
VTC Academy THSoft Co.,Ltd 13
14. Trigonometric Methods
sin(double a)
cos(double a)
tan(double a)
acos(double a)
asin(double a)
atan(double a)
VTC Academy THSoft Co.,Ltd 14
15. Exponent Methods
exp(double a)
Returns e raised to the power of a.
log(double a)
Returns the natural logarithm of a.
pow(double a, double b)
Returns a raised to the power of b.
sqrt(double a)
Returns the square root of a.
VTC Academy THSoft Co.,Ltd 15
16. Rounding Methods
double ceil(double x)
x rounded up to its nearest integer. This integer is returned as a
double value.
double floor(double x)
x is rounded down to its nearest integer. This integer is
returned as a double value.
double rint(double x)
x is rounded to its nearest integer. If x is equally close to two
integers, the even one is returned as a double.
int round(float x)
Return (int)Math.floor(x+0.5).
long round(double x)
Return (long)Math.floor(x+0.5).
VTC Academy THSoft Co.,Ltd 16
17. min, max, abs, and random
max(a, b)and min(a, b)
Returns the maximum or minimum of two
parameters.
abs(a)
Returns the absolute value of the parameter.
random()
Returns a random double value
in the range [0.0, 1.0).
VTC Academy THSoft Co.,Ltd 17
18. Example 3.1 Computing Mean
and Standard Deviation
Generate n random numbers and compute the mean
and standard deviation
n n
xi n ( xi )2
i 1 xi2 i 1
mean i 1 n
n deviation
n 1
VTC Academy THSoft Co.,Ltd 18
19. Example 3.2 Obtaining Random
Characters
Write the methods for generating random
characters. The program uses these methods to
generate 175 random characters between „!' and „~'
and displays 25 characters per line. To find out the
characters between „!' and „~', see Appendix B,
“The ASCII Character Set.”
VTC Academy THSoft Co.,Ltd 19
20. OO Programming Concepts
An object A Circle object
Data Field
data field 1
radius = 5
... State
Method
data field n findArea
method 1
... Behavior
method n
VTC Academy THSoft Co.,Ltd 20
21. Class and Objects
Circle UML Graphical notation for classes
radius: double UML Graphical notation for fields
UML Graphical notation for methods
findArea(): double
new Circle() new Circle()
circle1: Circle circlen: Circle UML Graphical notation
for objects
radius = 2 ... radius = 5
Illustration on code
VTC Academy THSoft Co.,Ltd 21
22. Class Declaration
class Circle {
double radius = 1.0;
double findArea(){
return radius * radius * 3.14159;
}
}
VTC Academy THSoft Co.,Ltd 22
23. Declaring Object Reference Variables
ClassName objectReference;
Example:
Circle myCircle;
VTC Academy THSoft Co.,Ltd 23
24. Creating Objects
objectReference = new ClassName();
Example:
myCircle = new Circle();
The object reference is assigned to the object
reference variable.
VTC Academy THSoft Co.,Ltd 24
25. Declaring/Creating Objects
in a Single Step
ClassName objectReference = new ClassName();
Example:
Circle myCircle = new Circle();
Circle myCircle2;
VTC Academy THSoft Co.,Ltd 25
26. Constructors
Circle(double r) {
radius = r;
} Constructors are a
special kind of
Circle() { methods that are
radius = 1.0; invoked to construct
} objects.
myCircle = new Circle(5.0);
VTC Academy THSoft Co.,Ltd 26
27. Constructors
Circle(double r) {
radius = r;
} Constructors are a
special kind of
Circle() { methods that are
radius = 1.0; invoked to construct
} objects.
myCircle = new Circle(5.0);
VTC Academy THSoft Co.,Ltd 27
28. Constructors, cont.
A constructor with no parameters is referred to
as a default constructor.
Constructors must have the same name
as the class itself.
Constructors do not have a return type—
not even void.
Constructors are invoked using the new
operator when an object is created.
Constructors play the role of initializing
objects.
VTC Academy THSoft Co.,Ltd 28
29. Example 3.3 Using Classes from
the Java Library
Objective: Demonstrate using classes from the
Java library. Use the JFrame class in the
javax.swing package to create two frames;
use the methods in the JFrame class to set
the title, size and location of the frames and
to display the frames.
Illustration on code
VTC Academy THSoft Co.,Ltd 29
30. Subclassing and Inheritance
private
None
protected
Public
Illustration on code
VTC Academy THSoft Co.,Ltd 30
31. Example 3.4
Shape
TwoDimensionalShape ThreeDimensionalShape
Circle Square Triangle Sphere Cube Tetrahedron
VTC Academy THSoft Co.,Ltd 31
33. Abstract Classes and
Methods
Abstract classes
– Are superclasses (called abstract superclasses)
– Cannot be instantiated
– Incomplete
subclasses fill in "missing pieces"
Concrete classes
– Can be instantiated
– Implement every method they declare
– Provide specifics
VTC Academy THSoft Co.,Ltd 33
34. Abstract Classes and Methods
(Cont.)
Abstract classes not required, but reduce
client code dependencies
To make a class abstract
– Declare with keyword abstract
– Contain one or more abstract methods
public abstract void draw();
– Abstract methods
No implementation, must be overridden
VTC Academy THSoft Co.,Ltd 34
35. Abstract Classes and Methods
(Cont.)
Application example
– Abstract class Shape
Declares draw as abstract method
– Circle, Triangle, Rectangle extends Shape
Each must implement draw
– Each object can draw itself
Iterators
– Array, ArrayList (Chapter 22)
– Walk through list elements
– Used in polymorphic programming to traverse a collection
Illustration on code
VTC Academy THSoft Co.,Ltd 35
36. Case Study: Creating and Using
Interfaces
Use interface Shape
– Replace abstract class Shape
Interface
– Declaration begins with interface keyword
– Classes implement an interface (and its methods)
– Contains public abstract methods
Classes (that implement the interface) must implement these
methods
VTC Academy THSoft Co.,Ltd 36
37. 1 // Fig. 10.18: Shape.java
2 // Shape interface declaration.
3
4 public interface Shape {
5 public double getArea(); // calculate area
6 public double getVolume(); // calculate volume
7 public String getName(); // return shape name
8
9 } // end interface Shape
Lines 5-7
Classes that implement Shape
must implement these methods
VTC Academy THSoft Co.,Ltd 37
38. 1 // Fig. 10.19: Point.java
2 // Point class declaration implements interface Shape.
3
4 public class Point extends Object implements Shape {
5 private int x; // x part of coordinate pair
6 private int y; // y part of coordinate pair
7
8 // no-argument constructor; x and y default to 0
9 public Point()
10 {
11 // implicit call to Object constructor occurs here
12 }
13
14 // constructor
15 public Point( int xValue, int yValue )
16 { Error compile
17 // implicit call to Object constructor occurs here
18 x = xValue; // no need for validation
19 y = yValue; // no need for validation
20 }
21
22 // set x in coordinate pair
23 public void setX( int xValue )
24 {
25 x = xValue; // no need for validation
26 }
27
VTC Academy THSoft Co.,Ltd 38
39. 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 39