By GokiSoft.com|
15:28 14/06/2023|
Java Basic
[Share Code] Tìm hiểu về Loop & Mảng index & collection & OOP trong Java - C2209I
Nội dung kiến thức:
- Vòng lặp (for, while, do ... while)
- Mảng
- Index -> Giống C/C++
- Collection -> Mảng động
- ArrayList
- Vector
- OOP:
T/c bao đóng
=================================================================
OOP:
Quản lý khách sạn:
- Xác định thực thể (models):
- Thông tin về ks -> Đối tượng -> Chuyển đổi vào trong ngôn ngữ lập trình -> OOP
- phong
- Đồ dùng
...
Quản lý sinh viên:
- Thực thể của dự án
- Sinh viên -> Student
- Thuộc tính:
tên -> fullname -> String
tuổi -> age -> int
địa chỉ -> address -> String
email -> String
...
- Hành động -> function (hàm) & method (phương thức)
ăn
ngủ
học
...
A, 20, Ha Noi, a@gmail.com
- Kỳ học
- Môn học
- Điểm thi
...
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson02;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
int x = 1;
//x = x + 10;
//x+=10;
//x = x + 1;
//x += 1;
//x++ -> gan trc -> thay doi gia tri sau
//++x -> thay doi gia tri trc -> sau do moi gan
//x = x - 10;
//x -= 10;
//x = x - 1
//x -= 1
//x--
//--x
int y = x++; //y = 1, x = 2
System.out.println("x = " + x + ", y = " + y);
//x = 2, y = 1 -> 1
//ko co dap an.
int z = ++y;
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
//x = 2, y = 2, z = 2
int t = x++ + ++x + y-- - --z + 1;
//x = 4, y = 1, z = 1, t = 2(x=3) + (x=4)4 + 2(y=1) - (z=1)1 + 1 = 8
System.out.println("x = " + x + ", y = " + y + ", z = " + z + ", t = " + t);
}
}
#Student.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson02;
/**
*
* @author teacher
*/
public class Student {
public String fullname;
public int age;
public String address;
public String email;
//TH -> class object -> ko co ham tao -> Tu dong tao ra 1 ham tao ko doi
public Student() {
System.out.println("Khoi tao doi tuong");
}
// public Student(String abc) {
// System.out.println("Khoi tao doi tuong > " + abc);
// }
public Student(String fullname) {
//Local Var
this.fullname = fullname;
}
public Student(String fname, int tuoi) {
fullname = fname;
age = tuoi;
}
public Student(String fullname, int age, String address, String email) {
this.fullname = fullname;
this.age = age;
this.address = address;
this.email = email;
}
public void eating() {
System.out.println("Eating ...");
}
public void sleeping() {
System.out.println("Sleeping ...");
}
public void learning() {
System.out.println("Learning ... " + fullname);
}
}
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson02;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
//Khai bao mang index
//B1. Khai bao 1 mang 9 so nguyen -> thay int bang: char, boolean, String, float, double
// int[] t = new int[9];
// int[] t;
int t[];
t = new int[9];
int k[] = new int[9];
//B2. Gan du lieu vao mang
//Độ dài mảng index -> length: 9
//index: 0 -> length - 1 = 8
// t[10] = 100;
t[3] = 10;
System.out.println("t[3] = " + t[3]);
Scanner scan = new Scanner(System.in);
System.out.println("Nhap t[0] = ");
t[0] = scan.nextInt();
System.out.println("t[0] = " + t[0]);
//Su dung loop: for -> de nhap du lieu
for (int i = 0; i < 9; i++) {
System.out.println("Nhap t[" + i + "] = ");
t[i] = scan.nextInt();
}
// t = new int[20];
System.out.println("Danh sach phan tu: ");
for (int i = 0; i < 9; i++) {
System.out.println(t[i] + " ");
}
//TH -> mang gom cac phan tu san co
int[] u = {5, 6, 3, 1, 8};
}
}
#Test2.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson02;
import java.util.ArrayList;
/**
*
* @author teacher
*/
public class Test2 {
public static void main(String[] args) {
//ArrayList -> Thay bang -> Vector
//B1. Khai bao mang dong -> Kieu du lieu la so int
//length: t.size(), index: 0 -> length - 1
ArrayList<Integer> t = new ArrayList<>();
//B2. Them phan tu vao mang
//Them 5 vao mang
t.add(5);//length: 1, index = 0, {5}
t.add(8);//length: 2, index = 0 -> 1, {5, 8}
t.add(10);//length: 3, index = 0 -> 2, {5, 8, 10}
//B3. Lay phan tu trong magn ra
System.out.println("t[0] = " + t.get(0));
System.out.println("t[1] = " + t.get(1));
System.out.println("t[2] = " + t.get(2));
//B4. Chen 1 phan tu vao trong mang
//chen 100 -> vao vi tri index: 1 -> {5, 100, 8, 10}
t.add(1, 100); //length: 4, index = 0 -> 3, {5, 100, 8, 10}
//B5. Xoa 1 phan tu trong mang di
//Xoa phan tu 8 (index = 2) -> {5, 100, 10}
t.remove(2);//length: 3, index" 0 -> 2, {5, 100, 10}
//B6. Xoa het cac phan tu trong mang
t.clear();
t = new ArrayList<>();
//B7. Sua 1 phan tu trong mang
//sua index = 1 (100) -> 1000
t.set(1, 1000);//length: 3, index: 0 -> 2, {5, 1000, 10}
}
}
#Test3.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.c2209i.lesson02;
/**
*
* @author teacher
*/
public class Test3 {
public static void main(String[] args) {
Student stdA;
stdA = new Student(); //Khoi tao ra 1 vung nho cho bien stdA -> Quan ly thong
stdA.fullname = "A";
stdA.age = 20;
stdA.address = "Ha Noi";
stdA.email = "a@gmail.com";
stdA = new Student(); //Khoi tao ra 1 vung nho cho bien stdA -> Quan ly thong
stdA.learning();
System.out.println(stdA.fullname);
Student stdB;
// stdB = new Student(); //Khoi tao ra 1 vung nho cho bien stdA -> Quan ly thong
// stdB.fullname = "B";
// stdB.age = 23;
stdB = new Student("B", 23);
stdB.address = "Ha Noi";
stdB.email = "b@gmail.com";
System.out.println(stdB.fullname);
stdB.learning();
Student std = new Student("Xin Chao ABC");
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)