-JDK :
-PHÍM NÓNG Eclipse :
ALT + Left/Right Arrow
CTRL + M
ALT + Up/Down Arrow
CTRL + D
- Graphic User interface - GUI :
- interface :
- Layout managers : bộ quản lý trình bày.
- thư viện JFC : Java Foundation Class. Sự thành lập lớp java.
- Struts : là một framwork để phát triển các ứng dụng web bằng java.
- framwork :
-API : Application Programming Interface. giao diện lập trình ứng dụng.
-setBounds :
-IDE : Integrated Development Environment
-J2SE : Java 2 Standard Edition
-J2ME: Java 2 Micro Edition
-J2EE : java 2 Platform, Enterprise Edition
cơ bản là em không biết dùng Eclipse
29:14
trong Java không còn khái niệm thư mục nữa
29:26
đã gọi là source java thì gọi là package
29:48
và khi nói về package thì em mở sách phần core java chỗ package và tham khảo lại
30:01
trong Eclipse, bên dưới project của em thường sẽ có thư mục src
30:12
tất cả package, file java đều nằm trong đó
Thứ Sáu, 31 tháng 10, 2008
Thứ Năm, 30 tháng 10, 2008
ý nghĩa các câu lệnh
-public void windowClosing(WindowEvent e){
-CÂU LỆNH TRONG GUI:
public void setLocation(Point p);
Public void setSize(Demention p);
Public void setBounds (rectangle r);
Ví dụ:
MyButton.setSize(new Dimention(20, 10));
MyButton.setLocation(new Point(10, 10));
MyButton.setBounds(10, 10, 20, 10);
import java.awt.*; nhập vào kiểu đồ hoạ.
import java.util.*;
radius = (circleRadius >= 0 ? circleRadius: 0);
(điều kiện ? command1 : command2);
* so sánh câu lệnh này với câu lệnh if:
- Tạo đường kẻ ngang trong MenuItem con ví dụ trong menu help:
menuHelp.addSeparator();
-CÂU LỆNH TRONG GUI:
public void setLocation(Point p);
Public void setSize(Demention p);
Public void setBounds (rectangle r);
Ví dụ:
MyButton.setSize(new Dimention(20, 10));
MyButton.setLocation(new Point(10, 10));
MyButton.setBounds(10, 10, 20, 10);
import java.awt.*; nhập vào kiểu đồ hoạ.
import java.util.*;
radius = (circleRadius >= 0 ? circleRadius: 0);
(điều kiện ? command1 : command2);
* so sánh câu lệnh này với câu lệnh if:
- Tạo đường kẻ ngang trong MenuItem con ví dụ trong menu help:
menuHelp.addSeparator();
Thứ Hai, 20 tháng 10, 2008
các từ khoá(key words)
- public :
- protected :
- private :
- extends :
- implements :
-Modal Dialog :
-None-Modal Dialog :
- từ khoá new :
- định nghĩa constructor:
-Static :
-void:
-nextToken() :
-Biến this:
-super:
-tomcat:
-gridx, gridy :
-gridwidth, gridheight :
-Insets:
-weightx, weighty :
- protected :
- private :
- extends :
- implements :
-Modal Dialog :
-None-Modal Dialog :
- từ khoá new :
- định nghĩa constructor:
-Static :
-void:
-nextToken() :
-Biến this:
-super:
-tomcat:
-gridx, gridy :
-gridwidth, gridheight :
-Insets:
-weightx, weighty :
thủ thuật it
- cách chỉnh mã trong notepad khi gõ tiếng việt :
đầu tiên mở notepad, gõ vài dòng tiếng viêt, save as: trong khung Encoding : select unicode/ok.
đầu tiên mở notepad, gõ vài dòng tiếng viêt, save as: trong khung Encoding : select unicode/ok.
Thứ Bảy, 18 tháng 10, 2008
ý nghĩa các cấu trúc
- cấu trúc try{
}
catch{
}
-CẤU TRÚC LẶP:
Dạng 1: while(...)
while(Điều_kiện_lặp){
khối lệnh;
}
Dạng 2: Do{...} while;
Do{
khối_lệnh;
} while (điều_kiện); thực hiện khối lệnh trước, áp dụng điều kiện sau.
Dạng 3: cấu trúc for(...)
for(khởi_tạo_biến_đếm; đk_lặp; tăng_biến){
khối_lệnh;
}
-CẤU TRÚC ĐIỀU KHIỂN: SWITCH ... CASE
switch ()
{
case:
;
break;
….
case:
;
break;
default:
;
}
}
catch{
}
-CẤU TRÚC LẶP:
Dạng 1: while(...)
while(Điều_kiện_lặp){
khối lệnh;
}
Dạng 2: Do{...} while;
Do{
khối_lệnh;
} while (điều_kiện); thực hiện khối lệnh trước, áp dụng điều kiện sau.
Dạng 3: cấu trúc for(...)
for(khởi_tạo_biến_đếm; đk_lặp; tăng_biến){
khối_lệnh;
}
-CẤU TRÚC ĐIỀU KHIỂN: SWITCH ... CASE
switch (
{
case
break;
….
case
break;
default:
}
Thứ Sáu, 17 tháng 10, 2008
thực hành
-programming gui:
/*
* Created on Nov 8, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day18_2008nov8;
/**
* @author Administrator
*
* TODO programming gui
* page 115
*/
import java.awt.*;
import java.awt.event.*;
public class WindowClosingDemo {
public static void main(String[] args) {
//ten Frame la Window closing Demo
Frame f = new Frame("Window Closing Demo");
//WindowCloser lay tu tap tin WindowCloser.java
WindowCloser closer = new WindowCloser();
f.addWindowListener(closer);
//dinh kich thuoc Frame
f.setBounds(10, 10, 300, 200);
//hien thi Frame
f.setVisible(true);
}
}
/*
* Created on Nov 8, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day18_2008nov8;
/**
* @author Administrator
*
* TODO programming gui
* page 116
*/
import java.awt.event.*;
class WindowCloser implements WindowListener{
public void windowClosing(WindowEvent e){
System.out.println("WindowClosing ...");
//thoat ung dung
System.exit(0);
}
public void windowActivated(WindowEvent e){
System.out.println("WindowActivated ...");
}
public void windowClosed(WindowEvent e){
System.out.println("WindowClosed...");
}
public void windowDeactivated(WindowEvent e){
System.out.println("windowDeactivated...");
}
public void windowDeiconified(WindowEvent e){
System.out.println("windowDeiconified...");
}
public void windowIconified(WindowEvent e){
System.out.println("windowIconified...");
}
public void windowOpened(WindowEvent e){
System.out.println("windowOpened...");
}
}
- progaming gui :
/*
* Created on Nov 5, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day17_2008sep6;
/**
* @author Administrator
*
* TODO programing gui
* page 112
*/
import java.awt.*;
import java.awt.event.*;
//sau implement la mot interface.
//implement thuc hien hanh dong cua interface
public class KeyDemo extends Frame implements KeyListener{
//private chi truy cap ben trong ban than lop khai bao
private String line1 = "", line2 = "";
private String line3 = "";
private TextArea textArea;
//set up GUI
//ham constructor
public KeyDemo(){
//super la khi lop con muon nho lop cha lam giup mot viec do.
//tao tieu de cua so ung dung.
super("Demonstrating Keystroke Events");
//set up Area
textArea = new TextArea(10, 15);
//tao dong chu trong giao dien chuong trinh khoi dong.
textArea.setText("Press any key on the keyboard...");
//(false) thuc hien chuong trinh nhap mot phim
//(true) nhap nhu van ban, giong word.
textArea.setEnabled(false);
this.add(textArea);
//allow Frame to process Key event
addKeyListener(this);
//dinh kich thuoc cua frame
setSize(350,100);
//hien thi khung frame
setVisible(true);
}
//handle press any key
public void keyPressed(KeyEvent event){
line1 = "Key pressed:" + event.getKeyText(event.getKeyCode());
setLines2and3(event);
}
//handle release of any key
public void keyReleased(KeyEvent event){
//hien thi dong 1 trong ung dung
line1 = "Key release : " + event.getKeyText(event.getKeyCode());
setLines2and3(event);
}
//handle press of an action key
public void keyTyped(KeyEvent event){
//hien thi dong 1 trong ung dung
line1 = "Key typed:" + event.getKeyChar();
setLines2and3(event);
}
//set second and third lines of out put
private void setLines2and3(KeyEvent event){
//hien thi dong 2 trong cua so
line2= "This key is" + (event.isActionKey()? " " : " not ") +
"an action key";
String temp = event.getKeyModifiersText(event.getModifiers());
//hien thi dong 3 trong cua so
line3 = "Modifier keys pressed : " + (temp.equals(" ")?
"none" : temp);
textArea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n");
//create command close window
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//execute application
public static void main(String arg[]){
//tao doi tuong trong lop KeyDemo,
//de application thuc hien cac nhiem vu cua lop
KeyDemo application = new KeyDemo();
}
}//end class KeyDemo
- Practicse programing gui :
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day16_2008sep5;
/**
* @author Administrator
*
* TODO programing gui
* page 109
*/
import java.awt.*;
import java.awt.event.*;
import Day5_2008oct22.stringArray;
public class MouseTracker extends Frame implements MouseListener, MouseMotionListener{
private Label statusBar;
//set up GUI and register mouse event handlers
public MouseTracker(){
super("Demonstrating Mouse Events");
statusBar = new Label();
this.add(statusBar, BorderLayout.SOUTH);
//Application listens to its own mouse events
addMouseListener(this);
addMouseMotionListener(this);
setSize(275, 100);
setVisible(true);
}
//MouseListener event handlers
//handle event when mouse release immediately after press
public void mouseClicked(MouseEvent event){
statusBar.setText("Clicked at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when mouse pressed
public void mousePressed(MouseEvent event){
statusBar.setText("Pressed at [" + event.getX() +
"," + event.getY() + "]");
}
// handle event when mouse pressed
public void mouseReleased(MouseEvent event){
statusBar.setText("Released at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when mouse enters area
public void mouseEntered(MouseEvent event){
statusBar.setText("Mouse in window");
}
//handle event when mouse exit area
public void mouseExited(MouseEvent event){
statusBar.setText("Mouse outside window");
}
//MouseMotionListener event handlers
//handle event when user drags mouse with button pressed
public void mouseDragged(MouseEvent event){
statusBar.setText("Dragged at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when user moves mouse
public void mouseMoved(MouseEvent event){
statusBar.setText("Moved at [" + event.getX() +
", " + event.getY() + "]");
//create command close window
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//execute application
public static void main(String args[]){
MouseTracker application = new MouseTracker();
}
}//end class MouseTracker
- practise programing gui:
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day16_2008sep5;
/**
* @author Administrator
*
* TODO programing gui
* page 102
*/
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelDemo extends Frame{
private Button next, prev, fisrt;
private List li;
public PanelDemo(String sTitle){
super(sTitle);
//tao 3 nut bam
next = new Button("Next >>");
prev = new Button("Prev <<"); fisrt = new Button(" Fisrt "); Panel southPanel = new Panel(); southPanel.add(next); southPanel.add(prev); southPanel.add(fisrt); //BoderLayout.SOUTH: vung duoi this.add(southPanel, BorderLayout.SOUTH); Panel northPanel = new Panel(); //gan ten cho Label northPanel.add(new Label("Make a selection")); //BoderLayout.NORTH: vung tren this.add(northPanel, BorderLayout.NORTH); //tao danh sach doc Selection i li = new List(); for(int i=0; i< f =" new" i =" 2;">b){
System.out.println("Min is : "+ b);
}
else{
System.out.println("Min is : "+ a);
}
}
public static void min2(int a, int b, int c){
if(a<=b && a<=c){ System.out.println("gia tri nho nhat:"+a); } if(b<=a && b<=c){ System.out.println("gia tri nho nhat:"+b); } else{ System.out.println("gia tri nho nhat:"+c); } } } SẮP XẾP NỔI BỌT: /* * Created on Oct 20, 2008 by huu truc * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package Day3_2008oct20; /** * @author Administrator * * TODO sap xep mang dung phuong phap sap xep noi bot. * Window - Preferences - Java - Code Style - Code Templates */ public class bubbleSort { public static void main(String[] args) { int nums[]={99, -10, 100123, 18, -978, 5623, 463, -9, 278, 49}; int a,b,t; //khai bao 3 bien a, bien b, va bien t. int size; size = 10; //number of element to sort. // display original array System.out.print("Original array is:"); for(int i=0;i=a; b--){/**vong for sau b giam con lai 8,
va cu tiep tuc. nếu điều kiện b>a không thôi thì kết quả sắp xếp sai.*/
if(nums[b-1] > nums[b]){
//if out of order(neu ngoai thu tu).
//exchange element(thay doi yeu to).
t = nums[b-1];
nums[b-1] = nums[b];//gan chuyen, dua nums[8]thanh nums[9]
nums[b] = t;//chuyen nums[9]thanh t tuc la nums[8].
}
}
//display sorted array.
System.out.print("\n sorted array is:");
for(int i=0; i
System.out.print(" " + nums[i]);
System.out.print(" ");
}
}
}
}
IN BẢNG CỬU CHƯƠNG:
/*
* Created on Oct 21, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day4_2008oct21;
/**
* @author Administrator
*
* TODO in bang cuu chuong 2 den 9
* Window - Preferences - Java - Code Style - Code Templates
*/
public class inCuuchuong {
public static void main(String[] args) {
int i, t;
String m = "";
for(i=2; i<10; t="1;">=10) m = "" ;//canh le so ben trai.
else m = " ";//canh le so ben trai, day so nho hon 10 ra bang
//cac so lon hon.
System.out.print(t + " x " + i + " = " + i * t + m+ " | " );
}
System.out.println("");
}
}
}
IN DÃY SỐ TỪ 1 ĐẾN 100 THÀNH 10 HÀNG 10 CỘT:
/*
* Created on Oct 20, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day4_2008oct21;
/**
* @author Administrator
*
* TODO in day so tu nhien tu 1 den 100 thanh 10 dong.
* moi dong chua 10 so.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class indayso {
public static void main(String[] args) {
int i, t;
String m = "";
for(i=1; i<=100; i++){ System.out.print(" " + i); if (i % 10 == 0){ System.out.println(" "); } if(i<10){system.out.print(""+m+" in =" new" str =" in.readLine();//doc" i=" str.length()-1;i">= 0; i--){
//vi java dem tu vi tri dau la: 0
System.out.print(str.charAt(i));
//in ra ky tu tai vi tri thu i trong chuoi str
//tinh tu vi tri cuoi
}
}catch(IOException e){// e doi tuong Exception
System.out.println(e.toString());//in ra loi neu sai Try
}
}
}
LẤY CHUỖI CON CỦA MỘT CHUỖI:
chú ý lý thuyết: java luôn đếm ký tự đầu tiên là 0.
/*
* Created on Oct 22, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day5_2008oct22;
/**
* @author Administrator
*
* TODO Lay chuoi con cua mot chuoi.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SubStr {
public static void main(String[] args) {
String orgstr = "mot hai ba bon";
//lay chuoi con dung ham.
//public String substring(int beginIndex, int endindex)
String substr= orgstr.substring(4,7);
//lay tu ky tu 4 den 7 trong chuoi.
//vi chuoi cung dem tu so 0 nhu mang tai vi tri dau tien.
//java luon dem tu 0.
System.out.println("Chuoi goc:"+ orgstr);
System.out.println("Chuoi con:"+substr);
System.out.print("ky tu dau tien la 0: "+orgstr.charAt(0));
}
}
THỰC HÀNH CÁC PHÉP GÁN:
/**
* @author Administrator
*
* TODO day 2 trong 21 ngay code.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class VolcanoRobot {
String status;
int speed;
float temperature;//la kieu so thuc nen co so le la :510.0
//neu chon kieu int thi cho ra so nguyen la :510
void checkTemperature(){
if(temperature > 660){ //cau truc dieu khien
status = "returning home";//thuc hien khoi lenh.
speed = 5;//nen thuc hien luon dong nay.
}
}
void showAttribute(){//khong tra ve gi het, nhung thuc hien cac lenh trong {}.
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("temperature: " + temperature);
}
public static void main(String[]arguments){
VolcanoRobot dante = new VolcanoRobot();
dante.status = "exploring";//
dante.speed = 2;
dante.temperature = 510;
dante.showAttribute();//dung de thuc hien trong ham void
// khi duoc goi void showAttribute(){} o tren.
System.out.println("increasing speed to 3.");
dante.speed = 3;//gan lenh moi
dante.showAttribute();//thuc hien lenh voi phep gan gia tri moi.
System.out.println("changing temperature to 670.");
dante.temperature = 670;//phep gan cho lenh duoi,moi.
dante.showAttribute();//thuc hien lenh voi phep gan gia tri moi.
System.out.println("Checking the temperture" );
dante.checkTemperature();//goi ham checkTemperature o tren.
dante.showAttribute();
}
}
SO SÁNH BẰNG VÀ KHÔNG BẰNG TRONG CHUỔI VỀ ĐỐI TƯỢNG VÀ GIÁ TRỊ:
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class equalsTest {
public static void main(String[] arguments) {
String str1, str2;
str1 = "Free the bound periodicals.";
str2 = str1;//phep gan thi bang
System.out.println("string1: " + str1);
System.out.println("string2: " + str2);
System.out.println("Same object? " + (str1==str2));
System.out.println("Same value? " + (str1.equals(str2)));
str2 = new String(str1);
// them phan duoi nay de cho biet khoi tao doi tuong thi so sanh
//khong bang ve doi tuong nhung co the bang ve gia tri
System.out.println("string1 :" + str1);
System.out.println("String2 :" + str2);
System.out.println("Same object? " + (str1==str2));
System.out.println("Same value? " + str1.equals(str2));
}
}
KIỂM TRA CHUỔI, IN RA DẤU NHÁY KÉP (") CỦA CHUỔI: TẠI TỪ IBM.
/*
* Created on Oct 26, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;
/**
* @author Administrator
*
* TODO Day 4 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class checkString {
public static void main(String[] arguments) {
String str = "Nobody ever went broke by buying IBM";
System.out.println("The string is:" + str);
System.out.println("Length of this string: " + str.length());
System.out.println("The character at position 5:" + str.charAt(5));
System.out.println("The substring from 26 to 32:"
+str.substring(26,32));;
System.out.println("The index of the character v:"+ str.indexOf('v'));
System.out.println("The index of the beginning of the "
+ "substring \"IBM\": " + str.indexOf("IBM"));
//\" dung de in ra dau nhay kep.
System.out.println("The string in upper case: "
+ str.toUpperCase());
System.out.print("The string in lower case: "
+ str.toLowerCase());
}
}
GÁN TOẠ ĐỘ XY HÌNH HỌC PHẲNG:
/*
* Created on Oct 26, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;
/**
* @author Administrator
*
* TODO Day 4 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.Point;//thu vien do hoa, chua cac thanh phan lap
//giao dien nhu Form, checkbox, button,...
public class preferencesTest {
public static void main(String[] args) {
Point pt1,pt2;//point la diem co toa do x y tren man hinh
pt1 = new Point(100,100);//toa do x = 100, y = 100
pt2 = pt1;
pt1.x = 200;//toa do x cua pt1 la 200.
pt1.y = 200;//toa do y cua pt1 la 200.
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
}
}
THÊM PHẦN ĐIỂM TOẠ ĐỘ:
/*
* Created on Oct 26, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.Point;//thu vien do hoa, chua cac thanh phan lap
//giao dien nhu Form, checkbox, button,...
public class setPoints {
public static void main(String[] arguments) {
Point location = new Point(4,13);
System.out.println("Starting location:");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
System.out.println("Moving to (7,6)");
location.x = 7;//gan x = 7
location.y = 6;//gan y = 6
System.out.println("\nEnding location: ");
//them \n de xuong them 1 hang nua.
System.out.println("X equals " + location.x);
System.out.println("y equals " + location.y);
}
}
CỘNG VÀ TÍNH TRUNG BÌNH GIÁ TRỊ CÁC MẢNG:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;
/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class halfDollars {
public static void main(String[] arguments) {
int [] denver = {15000006, 18810000, 20752110};
int [] philadelphia = new int[denver.length];
int [] total = new int[denver.length];
int average;
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2])/3;
System.out.println("1993 production: " + total[0]);
System.out.println("1994 production: " + total[1]);
System.out.println("1995 production: " + total[2]);
System.out.println("Average production: " + average);
}
}
TÍNH GIÁ TRỊ CỦA CHUỖI:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;
/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class halfLoop {
public static void main(String[] arguments) {
int [] denver = {15000006, 18810000, 20752110};
int [] philadelphia = {15020000, 18708000, 21348000};
int [] total = new int[denver.length];
int sum = 0;
for(int i=0; i< length =" 3" yearin =" 2008;" monthin =" 2;"> 0){
monthIn = Integer.parseInt(arguments[0]);
//phan tu thu 0 cua mang tra ve so thang.parseInt tra ve
//kieu so nguyen
}
if(arguments.length > 1){
yearIn = Integer.parseInt(arguments[1]);
//phan tu thu 1 cua mang tra ve so nam
}
System.out.println(monthIn + "/" + yearIn + " has " +
countDays(monthIn, yearIn) + " days.");
//dem so ngay cua thang nam.
}
static int countDays(int month, int year){
int count =1;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if(year%4 ==0){
count = 29;
}else{
count = 28;
}
if((year%100 ==0) & (year%400 !=0)){
count = 28;
}
}
return count;// return thuoc ham, khong co dau ngoac ( hay {
}
}
ÉP KIỂU DỮ LIỆU VÀ VÒNG LẶP WHILE:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;
/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CopyArrayWhile {
public static void main(String[] arguments) {
double[] array1 = {7.9, 4.1, 8, 1, 4, 1, 4};
int [] array2 = new int[array1.length];
System.out.print("array1 : [");
for(int i=0; i< count =" 0;" x1 =" 0;" y1 =" 0;" x2 =" 0;" y2 =" 0;" x1 =" x1;" y1 =" y1;" x2 =" x2;" y2 =" y2;" x1 =" topLeft.x;" y1 =" topLeft.y;" x2 =" bottomRight.x;" y2 =" bottomRight.y;" x1 =" topLeft.x;" y1 =" topLeft.y;" x2 =" (x1" y2 =" (y1">");
}
public static void main(String[]arguments){
myRect2 rect;//khai bao mot doi tuong rect co kieu la myRect2
System.out.println("Calling myRect2 with coordinates 25,25 50,50:");
rect = new myRect2(25, 25, 50, 50);
rect.printRect();//goi ham printRect() o cua lop myRect2
System.out.println("***");
System.out.print("Calling myRect2 with 1 point(10,10)");
System.out.println("width(50) and height(50):");
rect = new myRect2(new Point(10,10), 50, 50);
//new Point(10,10) khoi tao doi tuong point.
rect.printRect();
System.out.println("***");
}
}
- IN CLASS: lấy tên package.class để in ra:
/*
* Created on Oct 29, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day11_2008oct30;
/**
* @author Administrator
*
* TODO Day 6 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
class printClass {
int x = 0;
int y = 1;
void printMe(){
System.out.println("x is " + x + ", y is " + y);
System.out.println("I am an instance of the class " +
this.getClass().getName()) ;
//this la con tro cua class.
//getClass(): lay class. tra ve class nen khong in duoc.
//getName() : lay ten package. trong truong hop nay la ten class.
}
}
class printSubClass extends printClass{
int z = 3;
public static void main(String[]arguments){
printClass aClass = new printClass();
aClass.printMe();
printSubClass obj = new printSubClass();
obj.printMe();
}
}
-THỰC HIỆN HÀM CÓ GIÁ TRỊ TRẢ VỀ LÀ MỘT KIỂU MẢNG:
/*
* Created on Oct 29, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day11_2008oct30;
/**
* @author Administrator
*
* TODO Day 6 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
class rangeClass {
int[] makeRange(int lower, int upper){
//makeRange la ham co gia tri tra ve la mot kieu mang int[].
//nen co dau ngoac tron( va dau nhon mo{.
int arr[] = new int[(upper-lower) + 1];//khai bao mang int arr[];
for(int i=0; i< therange =" new" thearray =" theRange.makeRange(1," i="0;" arg =" new" sum =" 0;">0){//so luong tham so cua argument.
for(int i=0; i< butterscotch =" new" lasttime = "" screen2d =" (Graphics2D)screen;" type =" new" day =" new" time =" day.getTime().toString();" lasttime =" time;" pi =" 3.141592653589793" pi = " + Math.PI); //tao ra cac doi tuong hinh hoc. Point point = new Point(7,11);//khoi tao doi tuong point trong lop Circle circle = new Circle(3.5, 22, 8); Cylinder cylinder = new Cylinder(10, 3.3, 10, 10); //tao mot mang cac doi tuong hinh hoc. shape arrOfShapes[] = new shape[3]; //arrayOfshapes[0] la mot doi tuong Point. arrOfShapes[0] = point; //arrayOfshapes[1] la mot doi tuong Circle. arrOfShapes[1] = circle; //arrayOfshapes[2] la mot doi tuong cylinder. arrOfShapes[2] = cylinder; //lay ten va bieu dien cua moi doi tuong hinh hoc. String output = point.getName() + " precision =" new" i =" 0;" narea = " + precision.format(arrOfShapes[i].area())+ " nvolume =" + precision.format(arrOfShapes[i].volume()); } System.out.println(output); System.exit(0); } }//end class Test - hiển thị khung Frame: /* * Created on Nov 3, 2008 by huu truc * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package Day14_2008sep3; /** * @author Administrator * * TODO Lap trinh GUI page 86 * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; class FrameDemo { public static void main(String[] args) { //tao doi tuong khung chua Frame Frame fr = new Frame(" fr =" new" buttok =" new" f =" new" style="color: rgb(0, 0, 0);">-Programming gui:
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day15_2008sep4;
/**
* @author Administrator
*
* TODO programing gui
* page 96
*/
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import com.sun.java.swing.plaf.windows.resources.windows;
public class GridBagLayoutDemo {
public static void main(String[] args) {
//ten cua so ung dung
Frame f = new Frame("GridBagLayoutDemo");
//thiet lap Layout Manager
//tao doi tuong rang buoc cho cach trinh bay
//GridBagLayout.
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
f.setLayout(layout);
//Tao ra chin nut nhan voi cac ten Mot, ..., Chin.
String[] ButtName = {"Mot", "Hai", "Ba", "Bon", "Nam",
"Sau", "Bay", "Tam", "Chin"
};
//gan phan tu cho mang
Button[] buttons = new Button[9];
for(int i = 0; i < insets =" new" fill =" GridBagConstraints.BOTH;" gridx =" 1;" gridy =" 1;" gridheight =" 2;" gridwidth =" 1;" gridx =" 2;" gridy =" 1;" gridheight =" 1;" gridwidth =" 2;" gridx =" 2;" gridy =" 2;" gridheight =" 1;" gridwidth =" 1;" gridx =" 1;" gridy =" 3;" gridheight =" 1;" gridwidth =" 2;" gridx =" 3;" gridy =" 2;" gridheight =" 2;" gridwidth =" 1;" gridx =" 4;" gridy =" 1;" gridheight =" 3;" gridwidth =" 1;" fill =" GridBagConstraints.NONE;" gridx =" 1;" gridy =" 4;" gridheight =" 1;" gridwidth =" 1;" weightx =" 1.0;" gridx =" 2;" gridy =" 5;" gridheight =" 1;" gridwidth =" 1;" weightx =" 2.0;" gridx =" 3;" gridy =" 6;" gridheight =" 1;" gridwidth =" 1;" weightx =" 3.0;" i="0;">
/*
* Created on Nov 8, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day18_2008nov8;
/**
* @author Administrator
*
* TODO programming gui
* page 115
*/
import java.awt.*;
import java.awt.event.*;
public class WindowClosingDemo {
public static void main(String[] args) {
//ten Frame la Window closing Demo
Frame f = new Frame("Window Closing Demo");
//WindowCloser lay tu tap tin WindowCloser.java
WindowCloser closer = new WindowCloser();
f.addWindowListener(closer);
//dinh kich thuoc Frame
f.setBounds(10, 10, 300, 200);
//hien thi Frame
f.setVisible(true);
}
}
/*
* Created on Nov 8, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day18_2008nov8;
/**
* @author Administrator
*
* TODO programming gui
* page 116
*/
import java.awt.event.*;
class WindowCloser implements WindowListener{
public void windowClosing(WindowEvent e){
System.out.println("WindowClosing ...");
//thoat ung dung
System.exit(0);
}
public void windowActivated(WindowEvent e){
System.out.println("WindowActivated ...");
}
public void windowClosed(WindowEvent e){
System.out.println("WindowClosed...");
}
public void windowDeactivated(WindowEvent e){
System.out.println("windowDeactivated...");
}
public void windowDeiconified(WindowEvent e){
System.out.println("windowDeiconified...");
}
public void windowIconified(WindowEvent e){
System.out.println("windowIconified...");
}
public void windowOpened(WindowEvent e){
System.out.println("windowOpened...");
}
}
- progaming gui :
/*
* Created on Nov 5, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day17_2008sep6;
/**
* @author Administrator
*
* TODO programing gui
* page 112
*/
import java.awt.*;
import java.awt.event.*;
//sau implement la mot interface.
//implement thuc hien hanh dong cua interface
public class KeyDemo extends Frame implements KeyListener{
//private chi truy cap ben trong ban than lop khai bao
private String line1 = "", line2 = "";
private String line3 = "";
private TextArea textArea;
//set up GUI
//ham constructor
public KeyDemo(){
//super la khi lop con muon nho lop cha lam giup mot viec do.
//tao tieu de cua so ung dung.
super("Demonstrating Keystroke Events");
//set up Area
textArea = new TextArea(10, 15);
//tao dong chu trong giao dien chuong trinh khoi dong.
textArea.setText("Press any key on the keyboard...");
//(false) thuc hien chuong trinh nhap mot phim
//(true) nhap nhu van ban, giong word.
textArea.setEnabled(false);
this.add(textArea);
//allow Frame to process Key event
addKeyListener(this);
//dinh kich thuoc cua frame
setSize(350,100);
//hien thi khung frame
setVisible(true);
}
//handle press any key
public void keyPressed(KeyEvent event){
line1 = "Key pressed:" + event.getKeyText(event.getKeyCode());
setLines2and3(event);
}
//handle release of any key
public void keyReleased(KeyEvent event){
//hien thi dong 1 trong ung dung
line1 = "Key release : " + event.getKeyText(event.getKeyCode());
setLines2and3(event);
}
//handle press of an action key
public void keyTyped(KeyEvent event){
//hien thi dong 1 trong ung dung
line1 = "Key typed:" + event.getKeyChar();
setLines2and3(event);
}
//set second and third lines of out put
private void setLines2and3(KeyEvent event){
//hien thi dong 2 trong cua so
line2= "This key is" + (event.isActionKey()? " " : " not ") +
"an action key";
String temp = event.getKeyModifiersText(event.getModifiers());
//hien thi dong 3 trong cua so
line3 = "Modifier keys pressed : " + (temp.equals(" ")?
"none" : temp);
textArea.setText(line1 + "\n" + line2 + "\n" + line3 + "\n");
//create command close window
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//execute application
public static void main(String arg[]){
//tao doi tuong trong lop KeyDemo,
//de application thuc hien cac nhiem vu cua lop
KeyDemo application = new KeyDemo();
}
}//end class KeyDemo
- Practicse programing gui :
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day16_2008sep5;
/**
* @author Administrator
*
* TODO programing gui
* page 109
*/
import java.awt.*;
import java.awt.event.*;
import Day5_2008oct22.stringArray;
public class MouseTracker extends Frame implements MouseListener, MouseMotionListener{
private Label statusBar;
//set up GUI and register mouse event handlers
public MouseTracker(){
super("Demonstrating Mouse Events");
statusBar = new Label();
this.add(statusBar, BorderLayout.SOUTH);
//Application listens to its own mouse events
addMouseListener(this);
addMouseMotionListener(this);
setSize(275, 100);
setVisible(true);
}
//MouseListener event handlers
//handle event when mouse release immediately after press
public void mouseClicked(MouseEvent event){
statusBar.setText("Clicked at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when mouse pressed
public void mousePressed(MouseEvent event){
statusBar.setText("Pressed at [" + event.getX() +
"," + event.getY() + "]");
}
// handle event when mouse pressed
public void mouseReleased(MouseEvent event){
statusBar.setText("Released at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when mouse enters area
public void mouseEntered(MouseEvent event){
statusBar.setText("Mouse in window");
}
//handle event when mouse exit area
public void mouseExited(MouseEvent event){
statusBar.setText("Mouse outside window");
}
//MouseMotionListener event handlers
//handle event when user drags mouse with button pressed
public void mouseDragged(MouseEvent event){
statusBar.setText("Dragged at [" + event.getX() +
"," + event.getY() + "]");
}
//handle event when user moves mouse
public void mouseMoved(MouseEvent event){
statusBar.setText("Moved at [" + event.getX() +
", " + event.getY() + "]");
//create command close window
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//execute application
public static void main(String args[]){
MouseTracker application = new MouseTracker();
}
}//end class MouseTracker
- practise programing gui:
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day16_2008sep5;
/**
* @author Administrator
*
* TODO programing gui
* page 102
*/
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class PanelDemo extends Frame{
private Button next, prev, fisrt;
private List li;
public PanelDemo(String sTitle){
super(sTitle);
//tao 3 nut bam
next = new Button("Next >>");
prev = new Button("Prev <<"); fisrt = new Button(" Fisrt "); Panel southPanel = new Panel(); southPanel.add(next); southPanel.add(prev); southPanel.add(fisrt); //BoderLayout.SOUTH: vung duoi this.add(southPanel, BorderLayout.SOUTH); Panel northPanel = new Panel(); //gan ten cho Label northPanel.add(new Label("Make a selection")); //BoderLayout.NORTH: vung tren this.add(northPanel, BorderLayout.NORTH); //tao danh sach doc Selection i li = new List(); for(int i=0; i< f =" new" i =" 2;">b){
System.out.println("Min is : "+ b);
}
else{
System.out.println("Min is : "+ a);
}
}
public static void min2(int a, int b, int c){
if(a<=b && a<=c){ System.out.println("gia tri nho nhat:"+a); } if(b<=a && b<=c){ System.out.println("gia tri nho nhat:"+b); } else{ System.out.println("gia tri nho nhat:"+c); } } } SẮP XẾP NỔI BỌT: /* * Created on Oct 20, 2008 by huu truc * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package Day3_2008oct20; /** * @author Administrator * * TODO sap xep mang dung phuong phap sap xep noi bot. * Window - Preferences - Java - Code Style - Code Templates */ public class bubbleSort { public static void main(String[] args) { int nums[]={99, -10, 100123, 18, -978, 5623, 463, -9, 278, 49}; int a,b,t; //khai bao 3 bien a, bien b, va bien t. int size; size = 10; //number of element to sort. // display original array System.out.print("Original array is:"); for(int i=0;i
va cu tiep tuc. nếu điều kiện b>a không thôi thì kết quả sắp xếp sai.*/
if(nums[b-1] > nums[b]){
//if out of order(neu ngoai thu tu).
//exchange element(thay doi yeu to).
t = nums[b-1];
nums[b-1] = nums[b];//gan chuyen, dua nums[8]thanh nums[9]
nums[b] = t;//chuyen nums[9]thanh t tuc la nums[8].
}
}
//display sorted array.
System.out.print("\n sorted array is:");
for(int i=0; i
System.out.print(" " + nums[i]);
System.out.print(" ");
}
}
}
}
IN BẢNG CỬU CHƯƠNG:
/*
* Created on Oct 21, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day4_2008oct21;
/**
* @author Administrator
*
* TODO in bang cuu chuong 2 den 9
* Window - Preferences - Java - Code Style - Code Templates
*/
public class inCuuchuong {
public static void main(String[] args) {
int i, t;
String m = "";
for(i=2; i<10; t="1;">=10) m = "" ;//canh le so ben trai.
else m = " ";//canh le so ben trai, day so nho hon 10 ra bang
//cac so lon hon.
System.out.print(t + " x " + i + " = " + i * t + m+ " | " );
}
System.out.println("");
}
}
}
IN DÃY SỐ TỪ 1 ĐẾN 100 THÀNH 10 HÀNG 10 CỘT:
/*
* Created on Oct 20, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day4_2008oct21;
/**
* @author Administrator
*
* TODO in day so tu nhien tu 1 den 100 thanh 10 dong.
* moi dong chua 10 so.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class indayso {
public static void main(String[] args) {
int i, t;
String m = "";
for(i=1; i<=100; i++){ System.out.print(" " + i); if (i % 10 == 0){ System.out.println(" "); } if(i<10){system.out.print(""+m+" in =" new" str =" in.readLine();//doc" i=" str.length()-1;i">= 0; i--){
//vi java dem tu vi tri dau la: 0
System.out.print(str.charAt(i));
//in ra ky tu tai vi tri thu i trong chuoi str
//tinh tu vi tri cuoi
}
}catch(IOException e){// e doi tuong Exception
System.out.println(e.toString());//in ra loi neu sai Try
}
}
}
LẤY CHUỖI CON CỦA MỘT CHUỖI:
chú ý lý thuyết: java luôn đếm ký tự đầu tiên là 0.
/*
* Created on Oct 22, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day5_2008oct22;
/**
* @author Administrator
*
* TODO Lay chuoi con cua mot chuoi.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SubStr {
public static void main(String[] args) {
String orgstr = "mot hai ba bon";
//lay chuoi con dung ham.
//public String substring(int beginIndex, int endindex)
String substr= orgstr.substring(4,7);
//lay tu ky tu 4 den 7 trong chuoi.
//vi chuoi cung dem tu so 0 nhu mang tai vi tri dau tien.
//java luon dem tu 0.
System.out.println("Chuoi goc:"+ orgstr);
System.out.println("Chuoi con:"+substr);
System.out.print("ky tu dau tien la 0: "+orgstr.charAt(0));
}
}
THỰC HÀNH CÁC PHÉP GÁN:
/**
* @author Administrator
*
* TODO day 2 trong 21 ngay code.
* Window - Preferences - Java - Code Style - Code Templates
*/
public class VolcanoRobot {
String status;
int speed;
float temperature;//la kieu so thuc nen co so le la :510.0
//neu chon kieu int thi cho ra so nguyen la :510
void checkTemperature(){
if(temperature > 660){ //cau truc dieu khien
status = "returning home";//thuc hien khoi lenh.
speed = 5;//nen thuc hien luon dong nay.
}
}
void showAttribute(){//khong tra ve gi het, nhung thuc hien cac lenh trong {}.
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("temperature: " + temperature);
}
public static void main(String[]arguments){
VolcanoRobot dante = new VolcanoRobot();
dante.status = "exploring";//
dante.speed = 2;
dante.temperature = 510;
dante.showAttribute();//dung de thuc hien trong ham void
// khi duoc goi void showAttribute(){} o tren.
System.out.println("increasing speed to 3.");
dante.speed = 3;//gan lenh moi
dante.showAttribute();//thuc hien lenh voi phep gan gia tri moi.
System.out.println("changing temperature to 670.");
dante.temperature = 670;//phep gan cho lenh duoi,moi.
dante.showAttribute();//thuc hien lenh voi phep gan gia tri moi.
System.out.println("Checking the temperture" );
dante.checkTemperature();//goi ham checkTemperature o tren.
dante.showAttribute();
}
}
SO SÁNH BẰNG VÀ KHÔNG BẰNG TRONG CHUỔI VỀ ĐỐI TƯỢNG VÀ GIÁ TRỊ:
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class equalsTest {
public static void main(String[] arguments) {
String str1, str2;
str1 = "Free the bound periodicals.";
str2 = str1;//phep gan thi bang
System.out.println("string1: " + str1);
System.out.println("string2: " + str2);
System.out.println("Same object? " + (str1==str2));
System.out.println("Same value? " + (str1.equals(str2)));
str2 = new String(str1);
// them phan duoi nay de cho biet khoi tao doi tuong thi so sanh
//khong bang ve doi tuong nhung co the bang ve gia tri
System.out.println("string1 :" + str1);
System.out.println("String2 :" + str2);
System.out.println("Same object? " + (str1==str2));
System.out.println("Same value? " + str1.equals(str2));
}
}
KIỂM TRA CHUỔI, IN RA DẤU NHÁY KÉP (") CỦA CHUỔI: TẠI TỪ IBM.
/*
* Created on Oct 26, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;
/**
* @author Administrator
*
* TODO Day 4 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class checkString {
public static void main(String[] arguments) {
String str = "Nobody ever went broke by buying IBM";
System.out.println("The string is:" + str);
System.out.println("Length of this string: " + str.length());
System.out.println("The character at position 5:" + str.charAt(5));
System.out.println("The substring from 26 to 32:"
+str.substring(26,32));;
System.out.println("The index of the character v:"+ str.indexOf('v'));
System.out.println("The index of the beginning of the "
+ "substring \"IBM\": " + str.indexOf("IBM"));
//\" dung de in ra dau nhay kep.
System.out.println("The string in upper case: "
+ str.toUpperCase());
System.out.print("The string in lower case: "
+ str.toLowerCase());
}
}
GÁN TOẠ ĐỘ XY HÌNH HỌC PHẲNG:
/*
* Created on Oct 26, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;
/**
* @author Administrator
*
* TODO Day 4 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.Point;//thu vien do hoa, chua cac thanh phan lap
//giao dien nhu Form, checkbox, button,...
public class preferencesTest {
public static void main(String[] args) {
Point pt1,pt2;//point la diem co toa do x y tren man hinh
pt1 = new Point(100,100);//toa do x = 100, y = 100
pt2 = pt1;
pt1.x = 200;//toa do x cua pt1 la 200.
pt1.y = 200;//toa do y cua pt1 la 200.
System.out.println("Point1: " + pt1.x + ", " + pt1.y);
System.out.println("Point2: " + pt2.x + ", " + pt2.y);
}
}
THÊM PHẦN ĐIỂM TOẠ ĐỘ:
/*
* Created on Oct 26, 2008
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day8_2008oct27;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.Point;//thu vien do hoa, chua cac thanh phan lap
//giao dien nhu Form, checkbox, button,...
public class setPoints {
public static void main(String[] arguments) {
Point location = new Point(4,13);
System.out.println("Starting location:");
System.out.println("X equals " + location.x);
System.out.println("Y equals " + location.y);
System.out.println("Moving to (7,6)");
location.x = 7;//gan x = 7
location.y = 6;//gan y = 6
System.out.println("\nEnding location: ");
//them \n de xuong them 1 hang nua.
System.out.println("X equals " + location.x);
System.out.println("y equals " + location.y);
}
}
CỘNG VÀ TÍNH TRUNG BÌNH GIÁ TRỊ CÁC MẢNG:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;
/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class halfDollars {
public static void main(String[] arguments) {
int [] denver = {15000006, 18810000, 20752110};
int [] philadelphia = new int[denver.length];
int [] total = new int[denver.length];
int average;
philadelphia[0] = 15020000;
philadelphia[1] = 18708000;
philadelphia[2] = 21348000;
total[0] = denver[0] + philadelphia[0];
total[1] = denver[1] + philadelphia[1];
total[2] = denver[2] + philadelphia[2];
average = (total[0] + total[1] + total[2])/3;
System.out.println("1993 production: " + total[0]);
System.out.println("1994 production: " + total[1]);
System.out.println("1995 production: " + total[2]);
System.out.println("Average production: " + average);
}
}
TÍNH GIÁ TRỊ CỦA CHUỖI:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;
/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class halfLoop {
public static void main(String[] arguments) {
int [] denver = {15000006, 18810000, 20752110};
int [] philadelphia = {15020000, 18708000, 21348000};
int [] total = new int[denver.length];
int sum = 0;
for(int i=0; i< length =" 3" yearin =" 2008;" monthin =" 2;"> 0){
monthIn = Integer.parseInt(arguments[0]);
//phan tu thu 0 cua mang tra ve so thang.parseInt tra ve
//kieu so nguyen
}
if(arguments.length > 1){
yearIn = Integer.parseInt(arguments[1]);
//phan tu thu 1 cua mang tra ve so nam
}
System.out.println(monthIn + "/" + yearIn + " has " +
countDays(monthIn, yearIn) + " days.");
//dem so ngay cua thang nam.
}
static int countDays(int month, int year){
int count =1;
switch (month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count = 31;
break;
case 4:
case 6:
case 9:
case 11:
count = 30;
break;
case 2:
if(year%4 ==0){
count = 29;
}else{
count = 28;
}
if((year%100 ==0) & (year%400 !=0)){
count = 28;
}
}
return count;// return thuoc ham, khong co dau ngoac ( hay {
}
}
ÉP KIỂU DỮ LIỆU VÀ VÒNG LẶP WHILE:
/*
* Created on Oct 27, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day9_2008oct28;
/**
* @author Administrator
*
* TODO Day 5 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CopyArrayWhile {
public static void main(String[] arguments) {
double[] array1 = {7.9, 4.1, 8, 1, 4, 1, 4};
int [] array2 = new int[array1.length];
System.out.print("array1 : [");
for(int i=0; i< count =" 0;" x1 =" 0;" y1 =" 0;" x2 =" 0;" y2 =" 0;" x1 =" x1;" y1 =" y1;" x2 =" x2;" y2 =" y2;" x1 =" topLeft.x;" y1 =" topLeft.y;" x2 =" bottomRight.x;" y2 =" bottomRight.y;" x1 =" topLeft.x;" y1 =" topLeft.y;" x2 =" (x1" y2 =" (y1">");
}
public static void main(String[]arguments){
myRect2 rect;//khai bao mot doi tuong rect co kieu la myRect2
System.out.println("Calling myRect2 with coordinates 25,25 50,50:");
rect = new myRect2(25, 25, 50, 50);
rect.printRect();//goi ham printRect() o cua lop myRect2
System.out.println("***");
System.out.print("Calling myRect2 with 1 point(10,10)");
System.out.println("width(50) and height(50):");
rect = new myRect2(new Point(10,10), 50, 50);
//new Point(10,10) khoi tao doi tuong point.
rect.printRect();
System.out.println("***");
}
}
- IN CLASS: lấy tên package.class để in ra:
/*
* Created on Oct 29, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day11_2008oct30;
/**
* @author Administrator
*
* TODO Day 6 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
class printClass {
int x = 0;
int y = 1;
void printMe(){
System.out.println("x is " + x + ", y is " + y);
System.out.println("I am an instance of the class " +
this.getClass().getName()) ;
//this la con tro cua class.
//getClass(): lay class. tra ve class nen khong in duoc.
//getName() : lay ten package. trong truong hop nay la ten class.
}
}
class printSubClass extends printClass{
int z = 3;
public static void main(String[]arguments){
printClass aClass = new printClass();
aClass.printMe();
printSubClass obj = new printSubClass();
obj.printMe();
}
}
-THỰC HIỆN HÀM CÓ GIÁ TRỊ TRẢ VỀ LÀ MỘT KIỂU MẢNG:
/*
* Created on Oct 29, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day11_2008oct30;
/**
* @author Administrator
*
* TODO Day 6 trong 21 ngay code
* Window - Preferences - Java - Code Style - Code Templates
*/
class rangeClass {
int[] makeRange(int lower, int upper){
//makeRange la ham co gia tri tra ve la mot kieu mang int[].
//nen co dau ngoac tron( va dau nhon mo{.
int arr[] = new int[(upper-lower) + 1];//khai bao mang int arr[];
for(int i=0; i< therange =" new" thearray =" theRange.makeRange(1," i="0;" arg =" new" sum =" 0;">0){//so luong tham so cua argument.
for(int i=0; i< butterscotch =" new" lasttime = "" screen2d =" (Graphics2D)screen;" type =" new" day =" new" time =" day.getTime().toString();" lasttime =" time;" pi =" 3.141592653589793" pi = " + Math.PI); //tao ra cac doi tuong hinh hoc. Point point = new Point(7,11);//khoi tao doi tuong point trong lop Circle circle = new Circle(3.5, 22, 8); Cylinder cylinder = new Cylinder(10, 3.3, 10, 10); //tao mot mang cac doi tuong hinh hoc. shape arrOfShapes[] = new shape[3]; //arrayOfshapes[0] la mot doi tuong Point. arrOfShapes[0] = point; //arrayOfshapes[1] la mot doi tuong Circle. arrOfShapes[1] = circle; //arrayOfshapes[2] la mot doi tuong cylinder. arrOfShapes[2] = cylinder; //lay ten va bieu dien cua moi doi tuong hinh hoc. String output = point.getName() + " precision =" new" i =" 0;" narea = " + precision.format(arrOfShapes[i].area())+ " nvolume =" + precision.format(arrOfShapes[i].volume()); } System.out.println(output); System.exit(0); } }//end class Test - hiển thị khung Frame: /* * Created on Nov 3, 2008 by huu truc * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ package Day14_2008sep3; /** * @author Administrator * * TODO Lap trinh GUI page 86 * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; class FrameDemo { public static void main(String[] args) { //tao doi tuong khung chua Frame Frame fr = new Frame(" fr =" new" buttok =" new" f =" new" style="color: rgb(0, 0, 0);">-Programming gui:
/*
* Created on Nov 4, 2008 by huu truc
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Day15_2008sep4;
/**
* @author Administrator
*
* TODO programing gui
* page 96
*/
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import com.sun.java.swing.plaf.windows.resources.windows;
public class GridBagLayoutDemo {
public static void main(String[] args) {
//ten cua so ung dung
Frame f = new Frame("GridBagLayoutDemo");
//thiet lap Layout Manager
//tao doi tuong rang buoc cho cach trinh bay
//GridBagLayout.
GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
f.setLayout(layout);
//Tao ra chin nut nhan voi cac ten Mot, ..., Chin.
String[] ButtName = {"Mot", "Hai", "Ba", "Bon", "Nam",
"Sau", "Bay", "Tam", "Chin"
};
//gan phan tu cho mang
Button[] buttons = new Button[9];
for(int i = 0; i < insets =" new" fill =" GridBagConstraints.BOTH;" gridx =" 1;" gridy =" 1;" gridheight =" 2;" gridwidth =" 1;" gridx =" 2;" gridy =" 1;" gridheight =" 1;" gridwidth =" 2;" gridx =" 2;" gridy =" 2;" gridheight =" 1;" gridwidth =" 1;" gridx =" 1;" gridy =" 3;" gridheight =" 1;" gridwidth =" 2;" gridx =" 3;" gridy =" 2;" gridheight =" 2;" gridwidth =" 1;" gridx =" 4;" gridy =" 1;" gridheight =" 3;" gridwidth =" 1;" fill =" GridBagConstraints.NONE;" gridx =" 1;" gridy =" 4;" gridheight =" 1;" gridwidth =" 1;" weightx =" 1.0;" gridx =" 2;" gridy =" 5;" gridheight =" 1;" gridwidth =" 1;" weightx =" 2.0;" gridx =" 3;" gridy =" 6;" gridheight =" 1;" gridwidth =" 1;" weightx =" 3.0;" i="0;">
Thứ Năm, 16 tháng 10, 2008
đọc lý thuyết java
ra đời năm 1995 do sun microsystem.
một số vấn đề trong java:
-javabean
- thiết kế giao diện thư viện dùng JFC(JAVA FOUNDATION CLASS)
- lập trình ứng dụng web dùng J2EE(JAVA 2 ENTERPRISE EDITION)
-MÁY ảo java(Java Vitual Machine)
chương trình nguồn có đuôi *.java được biên dịch thành tập tin có đuôi *.class.
sau đó được trình thông dịch, thông dịch thành mã máy.
Một số thuật ngữ:
- tính đa kế thừa (multi inheritance)
Java và ứng dụng:
- ứng dụng Console: là ứng dụng nhập xuất như màn hình văn bản, như màn hình MS DOS
-ứng dụng Applet : là loại ứng dụng có thể nhúng và chạy trong trang web của trình duyệt web.
-ứng dụng desktop dùng AWT VÀ JFC tạo giao diện trực quan của bất kì ứng dụng nào.
-ứng dụng web thông qua công nghệ J2EE(JAVA 2 ENTERPRISE EDITION)
CÁC BƯỚC DỊCH VÀ THỰC THI MỘT CHƯƠNG TRÌNH VIẾT BẰNG JAVA
-Viết mã nguồn:dùng một chương trình soạn thảo để viết, lưu lại tên có đuôi .java
-Biên dịch ra mã máy ảo : dùng trình biên dịch javac để biên dịch mã nguồn ".java" thành mã của máy ảo(java bytecode) có đuôi ".class" và lưu lên đĩa.
Các kiểu dữ liệu cơ sở: có 8 kiểu:Byte, Short, int, long, float, double, boolean và char.
-java phân biệt chữ in hoa và chữ thường, do đó cần chú ý khi đặt tên cho các đối tượng cũng như xử lý trong chương trình.
-cần đọc các qui tắc viết code quốc tế bằng tiếng Anh để có thể đi làm cho nước ngoài.
một số vấn đề trong java:
-javabean
- thiết kế giao diện thư viện dùng JFC(JAVA FOUNDATION CLASS)
- lập trình ứng dụng web dùng J2EE(JAVA 2 ENTERPRISE EDITION)
-MÁY ảo java(Java Vitual Machine)
chương trình nguồn có đuôi *.java được biên dịch thành tập tin có đuôi *.class.
sau đó được trình thông dịch, thông dịch thành mã máy.
Một số thuật ngữ:
- tính đa kế thừa (multi inheritance)
Java và ứng dụng:
- ứng dụng Console: là ứng dụng nhập xuất như màn hình văn bản, như màn hình MS DOS
-ứng dụng Applet : là loại ứng dụng có thể nhúng và chạy trong trang web của trình duyệt web.
-ứng dụng desktop dùng AWT VÀ JFC tạo giao diện trực quan của bất kì ứng dụng nào.
-ứng dụng web thông qua công nghệ J2EE(JAVA 2 ENTERPRISE EDITION)
CÁC BƯỚC DỊCH VÀ THỰC THI MỘT CHƯƠNG TRÌNH VIẾT BẰNG JAVA
-Viết mã nguồn:dùng một chương trình soạn thảo để viết, lưu lại tên có đuôi .java
-Biên dịch ra mã máy ảo : dùng trình biên dịch javac để biên dịch mã nguồn ".java" thành mã của máy ảo(java bytecode) có đuôi ".class" và lưu lên đĩa.
Các kiểu dữ liệu cơ sở: có 8 kiểu:Byte, Short, int, long, float, double, boolean và char.
-java phân biệt chữ in hoa và chữ thường, do đó cần chú ý khi đặt tên cho các đối tượng cũng như xử lý trong chương trình.
-cần đọc các qui tắc viết code quốc tế bằng tiếng Anh để có thể đi làm cho nước ngoài.
Chương trình java cơ bản
Viết một class, đặt tên file bằng tên class và đuôi là java.
Cú pháp của 1 class:
Thuộc tính class tên_class {
}
Thuộc tính: public, private, static ...
Thành phần của 1 class:
- Thuộc tính (biến, properties)
- Phương thức (hàm, method)
Ví dụ : class người, thuộc tính: màu da, chiều cao, cân nặng, giới tính ... còn đi, ăn, chạy làm việc ... là phương thức
class xe: giá cả, hãng, model ... là thuộc tính. Phương thức: chạy, dừng, khởi động...
Để một chương trình java chạy được thì class phải chứa phương thức main.
Cách khai báo phương thức main
public static void main(String[] args) {
//nội dung
}
Phương thức main có thuộc tính là public, static. Void: giá trị trả về là không có gì. main: tên phương thức. String[] args: tham số. String: kiểu của tham số, args: tên tham số
Ví dụ: private boolean DauDaiHoc(String name, String sobaodanh){
//return false or true
}
Cú pháp của 1 class:
Thuộc tính class tên_class {
}
Thuộc tính: public, private, static ...
Thành phần của 1 class:
- Thuộc tính (biến, properties)
- Phương thức (hàm, method)
Ví dụ : class người, thuộc tính: màu da, chiều cao, cân nặng, giới tính ... còn đi, ăn, chạy làm việc ... là phương thức
class xe: giá cả, hãng, model ... là thuộc tính. Phương thức: chạy, dừng, khởi động...
Để một chương trình java chạy được thì class phải chứa phương thức main.
Cách khai báo phương thức main
public static void main(String[] args) {
//nội dung
}
Phương thức main có thuộc tính là public, static. Void: giá trị trả về là không có gì. main: tên phương thức. String[] args: tham số. String: kiểu của tham số, args: tên tham số
Ví dụ: private boolean DauDaiHoc(String name, String sobaodanh){
//return false or true
}
Thứ Tư, 15 tháng 10, 2008
Công cụ cần thiết để lập trình java
- Bộ JDK 1.5, 1.6
- IDE: Editplus, Netbean, Eclipse
1. Install JDK
- Download
- Install
2. Set path environment for java in WINDOWS
- Right click My Computer, choose Propertites, Click tab Advanced, Click button Environment Variables
- In system variables section, choose variable "Path", click button Edit
- In the end of textbox "Variable value", input ; and path of jdk folder installation
example :
- before: ...%SystemRoot%\System32\Wbem
- after: ...%SystemRoot%\System32\Wbem;C:\j2sdk1.4.2_02\bin
3. Install Editplus
4. Config Editplus to compile and run java
- Click menu Tools, choose Configure user tools,
- Click group name to set new group "Java"
- Click Add Tool>> Program

- Set for run java: Command: ...java.exe , Argument: $(FileNameNoExt)
5. Test first program "Hello World!"
- open editplus, choose a directory, new file hello.java
- input this text
public class hello {
public static void main(String[] arguments) {
System.out.println("Hello World");
}
}
Save lại với tên file là hello.java
- Compile: Ctrl + 1, nó sẽ tạo ra một file class: java.class
cú pháp dịch bằng tay : javac hello.java
- Run: Ctrl + 2
cú pháp run bằng tay : java hello
6. Using Eclipse
- IDE: Editplus, Netbean, Eclipse
1. Install JDK
- Download
- Install
2. Set path environment for java in WINDOWS
- Right click My Computer, choose Propertites, Click tab Advanced, Click button Environment Variables
- In system variables section, choose variable "Path", click button Edit
- In the end of textbox "Variable value", input ; and path of jdk folder installation
example :
- before: ...%SystemRoot%\System32\Wbem
- after: ...%SystemRoot%\System32\Wbem;C:\j2sdk1.4.2_02\bin
3. Install Editplus
4. Config Editplus to compile and run java
- Click menu Tools, choose Configure user tools,
- Click group name to set new group "Java"
- Click Add Tool>> Program
- Set for run java: Command: ...java.exe , Argument: $(FileNameNoExt)
5. Test first program "Hello World!"
- open editplus, choose a directory, new file hello.java
- input this text
public class hello {
public static void main(String[] arguments) {
System.out.println("Hello World");
}
}
Save lại với tên file là hello.java
- Compile: Ctrl + 1, nó sẽ tạo ra một file class: java.class
cú pháp dịch bằng tay : javac hello.java
- Run: Ctrl + 2
cú pháp run bằng tay : java hello
6. Using Eclipse
Đăng ký:
Bài đăng (Atom)