♥ | ||
|
ปรับปรุง : 2568-02-19 (มาร์คดาวน์) |
|
การเขียนโปรแกรมจาวาJAVA ProgrammingThaiall.com/class 3(2-2-5)ปรับปรุง 6 สิงหาคม 2563 |
โฮมเพจที่เกี่ยวข้อง |
เอกสาร Manual ภาษา PHP
|
หน่วยที่ 1 หลักการเขียนโปรแกรม
|
ความเป็นมาของภาษา [1]
|
ตัวแปลภาษา [2]
|
Argument [3]// ::: โปรแกรมลำดับที่ 1
// 1. ประมวลผล เช่น javac j0100.java
// 2. ประมวลผล เช่น java j0100 abc def
// 3. สามารถรับ parameter มาประมวลผลในโปรแกรมได้
// 4. ผลที่ได้จะเห็นเลข 2 เพราะรับข้อมูลเข้าไป 2 ค่า
// 5. http://www.yonok.ac.th/burin/langspec20/
class J0100 {
public static void main(String args[]) {
System.out.println(args.length);
System.out.println(args[0]); // abc
}
} |
การใช้อีดิเตอร์ [4]
|
หน่วยที่ 2 ตัวแปร และชนิดของข้อมูล
|
สัญลักษณ์และคำสงวน [1]1. Access Modifiers
|
สัญลักษณ์และคำสงวน2. Class, Method, and Variable Modifiers
|
สัญลักษณ์และคำสงวน3. Flow Control
|
สัญลักษณ์และคำสงวน4. Error Handling
5. Package Control
|
สัญลักษณ์และคำสงวน6. Primitives
|
สัญลักษณ์และคำสงวน7. Variable Keywords
8. Void Return Type Keyword
9. Unused Reserved Words
|
ชนิดของข้อมูล [2]// :::: โปรแกรมลำดับที่ 2
// 6 Primitive Data Type
class J0101 {
public static void main(String args[]) {
// 1. boolean true of false
boolean b = true;
System.out.println("boolean = "+b);
// 2. character (2 Byte)
char y;
y = 'a';
System.out.println("character = "+y);
// 3. byte -2^7 to 2^7-1 (1 Byte)
byte c;
c = 127;
System.out.println("byte = "+c);
// 4. short -2^15 to 2^15-1 (2 Byte)
short a;
a = 32767;
System.out.println("Short = "+a);
// 5. integer -2^31 to 2^31-1 (4 Byte)
int x;
x = 2147483647;
System.out.println("Integer = "+x);
// 6. long -2^63 to 2^63-1 (8 Byte)
long b;
b = 9223372036854775807L;
System.out.println("long = "+b);
}
} |
pro// :::: โปรแกรมลำดับที่ 3
// 1. 2 Primitive Data Type
// 2. แสดงผลจากค่าที่กำหนดขึ้น และการใช้ function
// 3. ประกาศ และใช้อาร์เรย์
class J0102 {
public static void main(String args[]) {
// 7. float -3.4e38 to 3.4e38 (4 Byte)
float d;
d = 340000000000000000000000000000000000000f;
System.out.println("float = "+d);
// 8. double -1.7e308 to 1.7e308 (8 Byte)
double e;
e = 17900000000000000000000000000000000000000d;
System.out.println("double = "+e);
// String aa = Double.toString(Double.parseDouble("123") + 1);
// String aa = Integer.toString(Integer.parseInt("456") + 2);
// aa = aa.substring(0,3);
// String : Abstact data type
// หรือ String z = new String("ThaiAll");
String z ="ThaiAll";
System.out.println("string = "+z);
System.out.println(z.substring(0,4)); // Thai
System.out.println(z.substring(2,5)); // aiA
System.out.println(z.substring(4)); // All
System.out.println(z.toUpperCase()); // THAIALL
System.out.println(z.toLowerCase()); // thaiall
char ar[] = new char[128];
ar = z.toCharArray();
System.out.println((char)ar[0]); // T
System.out.println(ar[0]); // T
System.out.println(ar[2] + ar[4]); // 162 (97 + 65)
z = "1234.1";
int m = Integer.parseInt(z.substring(0,3)) + 5; // 123 + 5
double n = Double.parseDouble(z) + 0.2; // 1234.3
System.out.println(m + n); // 128 + 1234.3 = 1362.3
System.out.println(Integer.toString(m) + 5); // 1285
}
} |
ตัวดำเนินการ [3]Operator Classification Operators Arithmetic + - * / % Relational Operators < > >= <= == != && || ! Bitwise Operators & | ^ << >> >>> - &= |= ^= Assignments = += -= /= %= Bitwise Assignments &= |= <<= >>= >>>= ^= Ternary Operator (if...else shorthand) ?: Increment ++ Decrement -- |
การแปลงชนิดของข้อมูล [4]int x; x = 8/3; System.out.println(x); // 2 float a = 5/(float)3; // 1.6666666 float b = (float)5/3; // 1.6666666 double c = 5/(double)3; // 1.6666666666666667 float d = (float)(5/3); // 1.0 float e = 5/3; // 1.0 |
หน่วยที่ 3 การโปรแกรมโครงสร้าง
|
การทำงานแบบตามลำดับ [1]// ::: โปรแกรมลำดับที่ 10
// 1. แสดงการรับค่าจากแป้นพิมพ์แบบ character
// 2. รับข้อมูลได้เพียง 1 ตัวอักษร และแสดงผล
import java.io.*;
class J0301 {
public static void main(String args[]) throws IOException {
char buf;
buf = (char)System.in.read();
System.out.println("Output is "+buf);
}
} |
mypro// ::: โปรแกรมลำดับที่ 11
// 1. แสดงการรับค่าจากแป้นพิมพ์แบบ character
// 2. รับ 2 ตัวอักษรมาแสดงผลต่อกัน
import java.io.*;
class J0302 {
public static void main(String args[]) throws IOException {
char buf1,buf2;
buf1 = (char)System.in.read();
buf2 = (char)System.in.read();
System.out.println("Output is "+buf1+buf2);
}
} |
mypro// ::: โปรแกรมลำดับที่ 13
// 1. รับตัวเลข 2 จำนวนแล้วนำมาบวกกัน เพื่อแสดงผล
// 2. ใช้ BufferedReader ในการรับข้อมูล
import java.io.*;
class J0304 {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String buf;
int i1,i2,i3;
buf = stdin.readLine();
i1 = Integer.parseInt(buf);
buf = stdin.readLine();
i2 = Integer.parseInt(buf);
i3 = i1 + i2;
System.out.println("Output is "+i1+" + "+i2+" = "+i3);
}
} |
การเลือกกระทำตามเงื่อนไข [2]// :::: โปรแกรมลำดับที่ 4
// 1. แสดงการใช้คำสั่ง if
// 2. ความแตกต่างของ print และ println
// 3. การใช้ { } หรือไม่ใช้ ต่างกันอย่างไร
class J0201 {
public static void main(String args[]) {
int x;
x = 6;
if (x > 5) System.out.println("more than 5:" + x);
if (x > 5 && x < 10) System.out.println("five to ten");
if (x > 5 || x < 10) System.out.println("all numbers");
if (x > 10) {
System.out.print("more than 10:");
System.out.println(x);
}
}
} |
mypro// :::: โปรแกรมลำดับที่ 5
// 1. แสดงการใช้คำสั่ง if .. else ..
// 2. ถ้าคิดเปรียบเทียบ String ต้องใช้ .equals
// 3. ตัวแปรประเภท Comparable นำมาบวกกันไม่ได้
import java.lang.*;
class J0202 {
public static void main(String args[]) {
int x;
x = 6;
if (x > 5) System.out.println("more than 5");
else System.out.println("less than or equal 5");
if (x > 10) System.out.println("more than 10");
else { System.out.println("less than or equal 10"); }
Comparable a[] = new Comparable[5];
a[0] = new Integer(3);
a[1] = new Integer(10);
a[2] = "abc";
System.out.println(a[0] + " " + a[1] + " " + a[2]);
if (a[2].equals("abc")) { System.out.println("equal"); }
if (a[0].compareTo(a[1]) < 0) System.out.print(a[0]); // 3
if (a[1].compareTo(a[0]) > 0) System.out.print(a[0]+""+a[1]); // 310
if (a[0].compareTo(a[0]) == 0) System.out.print("equal"); // equal
System.out.print(a[0].compareTo(a[1])); // -1
}
} |
mypro// :::: โปรแกรมลำดับที่ 6
// 1. แสดงการใช้คำสั่ง switch, case, default, break
// 2. available for switch = char, byte, short or int
import java.util.Date;
class J0203 {
public static void main(String args[]) {
byte a = (byte) (new Date().getTime() % 5);
switch (a) {
case 1:
System.out.println("one"); break;
case 2:
System.out.println("two"); break;
default:
System.out.println("not found" + a);
break;
}
}
} |
การทำซ้ำ [3]// :::: โปรแกรมลำดับที่ 7
// 1. แสดงการใช้คำสั่ง for
// 2. โปรแกรมนี้ใช้ i นอก for ไม่ได้
// 3. ให้พิจารณา .length และ .length()
class J0204 {
public static void main(String args[]) {
System.out.println("ASCII character :: ");
for (int i=0; i<256; i++) {
System.out.print((char)i + " ");
// System.out.println(i); 0 - 255
}
String s = "thaiall";
System.out.println(s + s.length());
}
} |
mypro// :::: โปรแกรมลำดับที่ 8
// 1. แสดงการใช้คำสั่ง while แบบเช็คก่อนทำ
// 2. กำหนดค่าเริ่มต้น 1 หรือ 0
// 3. ใช้ (double) ทำให้ผลหารถูกต้อง จึงไม่ catch
class J0205 {
public static void main(String args[]) {
System.out.println("print 1 to 10 :: ");
int i;
i = -5;
while (i <= 5) {
try {
i++;
System.out.println((double)5/i); //Infinity
System.out.println(5/i); //catch ok
}
catch (ArithmeticException e) {
System.out.println("may divide by zero");
}
}
int k = 0;
i = 0;
while (i < 5) {
System.out.print(++k);
k = k + (i++);
System.out.print(k--);
} // 11122447711
}
} |
mypro// ::: โปรแกรมลำดับที่ 12
// 1. แสดงการรับค่าจากแป้นพิมพ์แบบ character
// 2. รับต่อกันไปเรื่อย ๆ จนรับตัวอักษร 0 เข้าไปจึงหยุด
import java.io.*;
class J0303 {
public static void main(String args[]) throws IOException {
System.out.println("Get until receive 0 [hidden is 13, 10]");
char buf;
do {
buf = (char)System.in.read();
System.out.println("Output is "+buf);
} while (buf != '0');
}
} |
mypro// ::: โปรแกรมลำดับที่ 14
// 1. รับตัวเลขไปเรื่อย ๆ ไปแสดงผล จนกระทั่งรับเลข 0
// 2. ใช้ BufferedReader ในการรับข้อมูล
import java.io.*;
class J0305 {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String buf;
int i;
System.out.println("Get until receive 0");
do {
buf = stdin.readLine();
i = Integer.parseInt(buf);
System.out.println("Output is "+i);
} while (i != 0);
}
} |
หน่วยที่ 4 การควบคุมเหตุการณ์
|
Exception [1]class testofexception {
public static void main(String[] args) {
try {
System.out.println(1/0);
}
catch (Exception e) {
System.out.println(e);
}
// java.lang.ArithmeticException: / by zero
}
} |
try, catch, finally [2]// ::: โปรแกรมลำดับที่ 9
// 1. แสดงการใช้คำสั่ง do .. while
// 2. ตรวจเงื่อนไขแบบ ทำก่อนเช็ค
// 3. ใช้ try catch จับปัญหา array
class J0206 {
public static void main(String args[]) {
System.out.println("print 1 to 10 :: ");
int i;
i = 1;
try {
do {
System.out.println(i);
i++;
} while (i <= 10);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("over index of array");
}
}
} |
หน่วยที่ 5 คลาส และเมธอด
|
method [1]// :::: โปรแกรมลำดับที่ 15
// 1. มี 3 method ใน class และเรียกใช้
// 2. ผลคือ xyx ให้นศ.ฝึกสร้าง method
class J0401 {
public static void main(String args[]) {
sub1(); sub2(); sub1();
}
static void sub1() {
System.out.print("x");
}
static void sub2() { System.out.print("y"); }
} |
mypro// :::: โปรแกรมลำดับที่ 16
// 1. การเขียน method เพื่อรับค่าไปประมวลผล
// 2. ผลของ sum = 140 เพราะอะไร
class J0402 {
public static void main(String args[]) {
int s = 0;
s = sub(2,8,s);
s = sub(7,3,s);
s = sub(4,6,s);
System.out.println("Sum = "+s);
}
public static int sub(int x, int y, int z) {
int a = y + x + z;
return (a + y + x + z);
}
} |
mypro// ::: โปรแกรมลำดับที่ 17
// 1. การใช้ method หาค่า 2 เท่า
// 2. ประกาศ method ไม่มี public ก็ได้
class J0403 {
public static void main(String args[]) {
int j = 3;
System.out.println(doubleofnumber(j));
}
static int doubleofnumber(int i) {
i = i * 2;
return (i);
}
} |
class [2]// :::: โปรแกรมลำดับที่ 18
// 1. สร้าง class ใหม่และเรียก method ใน class ใหม่
// 2. แสดงการใช้ super และ this ใน constructor
// 3. .java แฟ้มหนึ่ง อาจมีได้หลาย class
class sub01 {
void subx() {
System.out.println("subx in sub01");
}
}
class sub02 {
void subx() {
System.out.println("subx in sub02");
}
}
class J0404 extends sub02 {
j0404() {
super.subx(); // subx in sub02
this.subx(); // subx in main
}
public static void main(String args[]) {
sub01 x = new sub01();
System.out.println("main"); // main
x.subx(); // subx in sub01
j0404 y = new j0404();
}
void subx() {
System.out.println("subx in main");
}
} |
หน่วยที่ 6 การปกป้อง (Encapsulation)
|
public [1]public static int cal1() { } |
private [2]public static int cal2() { } |
protected [3]public static int cal3() { } |
หน่วยที่ 7 การสืบทอด (Inheritance)
|
constructor [1]class Friend {
String name;
Integer age;
Friend(String name,Integer age) {
this.name = name;
this.age = age;
}
}
http://www.thaiall.com/class/oopsam.htm |
superclass [2]
j0404.java
public class sub01 {
void subx() { System.out.println("subx in sub01"); }
}
public class sub02 {
void subx() { System.out.println("subx in sub02"); }
}
public class j0404 extends sub02 {
j0404() {
super.subx(); // subx in sub02
this.subx(); // subx in main
}
public static void main(String args[]) {
sub01 x = new sub01();
System.out.println("main"); // main
x.subx(); // subx in sub01
j0404 y = new j0404();
}
void subx() {
System.out.println("subx in main");
}
} |
subclass [3]
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { System.out.println("Tuut, tuut!"); } // Vehicle method
}
class Car extends Vehicle {
private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {
Car myCar = new Car(); // Create Object from Car
myCar.honk(); // Tuut, tuut!
System.out.println(myCar.brand + " : " + myCar.modelName); // Ford : Mustang
}
} |
หน่วยที่ 8 การพ้องรูป (Polymorphism)
|
overloading [1]public class Human {
public String getName() {
return "I'm Human";
}
public String getName(String firstname, String lastname) {
return firstname + lastname;
}
} |
overriding [2]public class Human {
public String getName() {
return "I'm Human";
}
}
public class Man extends Human {
@Overide
public String getName() {
return "I'm Man";
}
} |
upcasting [3]
public class Animal {
public void eat() { }
}
public class Cat extends Animal {
public void eat() { }
public void meow() { }
}
// Cat cat = new Cat();
// Animal animal = cat;
// animal = (Animal) cat;
// animal.meow(); The method meow() is undefined for the type Animal
|
downcasting// Animal animal = new Cat(); // Upcasting
// ((Cat) animal).meow(); // Downcasting คือเรียกใช้ method ใน subclass
public class AnimalFeeder {
public void feed(List<Animal> animals) {
animals.forEach(animal -> {
animal.eat();
if (animal instanceof Cat) { ((Cat) animal).meow(); }
});
}
} |
downcastingParent p = new Child(); Child c =(Child) p; |
หน่วยที่ 9 แพคเกจ (Package)
|
การสร้าง package [1]
package financeDep;
public class Hello {
public void sayHello() {
System.out.println( "Hello, FinanceDep" );
}
}
// ---
public class myPro {
public static void main(String args[]){
financeDep.Hello x = new financeDep.Hello();
x.SayHello(); // Hello, FinanceDep
}
} |
การเรียกใช้ package [2]import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
} |
หน่วยที่ 10 เทรด (Thread)
|
เทรดเดียว [1]public class ThreadExample {
public static void main(String[] args){
Thread t1 = new Thread(new MyThread());
t1.start();
}
}
class MyThread implements Runnable {
@Override
public void run() { System.out.println("Thread is running..."); }
} |
มัลติเทรด [2]public class ThreadExample {
public static void main(String[] args){
for (int i = 0; i < 4; i++) {
Thread t1 = new Thread(new HelloMessage(i + 1));
t1.start();
}
}
}
class HelloMessage implements Runnable {
public int threadNum;
public HelloMessage(int threadNum) {
this.threadNum = threadNum;
}
@Override
public void run() {
System.out.println("Hello from thread " + threadNum);
}
}
Hello from thread 1
Hello from thread 4
Hello from thread 3
Hello from thread 2 |
หน่วยที่ 11 การจัดการแฟ้มข้อความ
|
การสร้างแฟ้มข้อความ [1]// :::: โปรแกรมลำดับที่ 21
// 1. การแสดงรายละเอียดของแฟ้ม f0601.java
// 2. http://www.yonok.ac.th/pmy/j2sdk-1_4_2-doc.zip
// 3. http://www.exampledepot.com/egs/java.io/
import java.io.*;
class J0601 {
public static void main (String args[]) throws IOException {
File f = new File("j0601.java");
System.out.println("getName: "+f.getName());
System.out.println("getPath: "+f.getPath());
System.out.println("getAbsolutePath: "+f.getAbsolutePath());
System.out.println("exists: "+f.exists());
System.out.println("isFile: "+f.isFile());
System.out.println("isDirectory: "+f.isDirectory());
System.out.println("canWrite: "+f.canWrite());
System.out.println("canRead: "+f.canRead());
System.out.println("length: "+f.length());
File file = new File("hello.txt");
boolean success = file.createNewFile();
File file2 = new File("hello.java");
success = file.renameTo(file2);
File b = new File("c:/");
success = file2.renameTo(new File(b, file2.getName()));
success = (new File("hello.java")).delete();
System.out.println(success); // false
}
} |
mypro// ::: โปรแกรมลำดับที่ 22
// 1. ประมวลผล เช่น java j0602 c:\class
// 2. การแสดงรายชื่อแฟ้มจาก Directory ที่กำหนด
import java.io.*;
class J0602 {
public static void main (String args[]) {
File d = new File(args[0]);
String n[] = d.list();
for (int i = 0; i<n.length; i++) {
File f = new File(args[0] + '/' + n[i]);
System.out.println(i+" : "+n[i]+" Size="+f.length());
}
System.out.println("directory: "+d.getPath());
}
} |
mypro// :::: โปรแกรมลำดับที่ 24
// 1. เขียนข้อมูลลงไปในแฟ้ม tmp.txt
// 2. create or replace file 256 byte in ascii character
import java.io.*;
class J0604 {
public static void main (String args[]) throws IOException {
FileOutputStream fout = new FileOutputStream("tmp.txt");
for(int i=0;i<256;i++) {
fout.write(i);
}
fout.close();
}
} |
mypro// :::: โปรแกรมลำดับที่ 25
// เขียนข้อมูล 10 บรรทัดลงไปในแฟ้ม tmp.txt
import java.io.*;
class J0605 {
public static void main (String args[]) throws IOException {
FileOutputStream fout = new FileOutputStream("tmp.txt");
for(int i=1;i<=10;i++) {
fout.write(i+47);
fout.write(13);
fout.write(10);
}
fout.close();
}
}mypro// ::: โปรแกรมลำดับที่ 26
// 1. อ่านข้อมูลจากแฟ้ม tmp.txt มาแสดงผล
// 2. ใช้ FileReader
// 3. อ่านข้อมูลมาแสดงบรรทัดละ 1 ตัวอักษร
import java.io.*;
class J0606 {
public static void main (String args[]) throws IOException {
int i = 0, n = 0;
char b[] = new char[1];
FileReader fin = new FileReader("tmp.txt");
while ((n = fin.read(b)) != -1) {
System.out.println(i+" : "+b[0]);
i = i + 1;
}
fin.close();
}
} |
mypro// :::: โปรแกรมลำดับที่ 27
// 1. อ่านข้อมูลจากแฟ้ม tmp.txt มาแสดงผลทีละตัวอักษร
// 2. อ่านข้อมูลเก็บลงอาร์เรย์ และนำจากอาร์เรย์มาแสดงผล
// 3. อ่านข้อมูลมาแสดงบรรทัดละ 16 ตัวอักษร
import java.io.*;
class J0607 {
public static void main (String args[]) throws IOException {
int i = 1, n = 0;
char b[] = new char[16];
FileReader fin = new FileReader("tmp.txt");
while ((n = fin.read(b)) != -1) {
System.out.print((i-1)*16 + " - " + (i*16-1) + ":");
System.out.print(b[0]+b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]);
System.out.println(b[9]+b[10]+b[11]+b[12]+b[13]+b[14]+b[15]);
i = i + 1;
}
fin.close();
}
} |
mypro// :::: โปรแกรมลำดับที่ 28
// 1. อ่านข้อมูลจากแฟ้ม data.txt มาแสดงผล
// 2. ใช้วิธีการอ่านข้อมูลแบบ readlin และ String
import java.io.*;
class J0608 {
public static void main (String args[]) throws IOException {
int i = 1;
String b;
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
// System.out.println(b = bin.readLine()); // output is b
while ((b = bin.readLine()) != null) {
System.out.println(i + " : " +b);
i = i + 1;
}
System.out.println(b = bin.readLine()); // null
fin.close();
}
} |
การอ่านแฟ้มข้อความ [2]// :::: โปรแกรมลำดับที่ 23
// อ่านแฟ้ม j0603.java ทีละตัวอักษร มาแสดงผล
import java.io.*;
class J0603 {
public static void main (String args[]) throws IOException {
int n = 0;
byte b[] = new byte[128];
FileInputStream fin = new FileInputStream("j0603.java");
while ((n = fin.read(b)) != -1) {
for(int i=0;i<n;i++) System.out.print((char)b[i]);
}
System.out.println(n = fin.read(b)); // -1
fin.close();
}
} |
การเพิ่มรายการ [3]// ::: โปรแกรมลำดับที่ 31
// 1. อ่านข้อมูลเก็บลงอาร์เรย์ แล้วนำไปเขียนลงแฟ้ม data.htm
// 2. เพิ่มค่าให้ field เงินเดือนจากเดิมอีก 100 บาท
import java.io.*;
class J0703 {
public static void main (String args[]) throws IOException {
int i = 0,d;
String b;
String[] fields;
String[] recs = {"","",""};
String patternStr = ",";
//
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
//
while ((b = bin.readLine()) != null) {
recs[i] = b;
i = i + 1;
}
fin.close();
//
FileOutputStream fout = new FileOutputStream("data.htm");
BufferedOutputStream bout = new BufferedOutputStream(fout);
PrintStream pout = new PrintStream(bout);
for(int j=0;j<i;j++) {
fields = recs[j].split(patternStr);
pout.print(fields[0]+","+fields[1]+",");
// pout.print(Double.valueOf(fields[2]).doubleValue());
d = Integer.valueOf(fields[2]).intValue() + 100;
pout.print(d);
pout.println(","+fields[3]);
}
pout.close();
}
} |
การลบรายการ [4]public static void deleteonid () throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String bufid;
System.out.print("Wait id : ");
bufid = stdin.readLine();
String b;
String ar[] = new String[10];
FileReader fin = new FileReader("updtxt.dat");
BufferedReader bin = new BufferedReader (fin);
int i = 0;
while ((b = bin.readLine()) != null) {
ar[i] = b;
i = i + 1;
}
fin.close();
//
int tot = i;
FileOutputStream fout = new FileOutputStream("updtxt.dat");
BufferedOutputStream bout = new BufferedOutputStream(fout);
PrintStream pout = new PrintStream(bout);
i = 0;
while (i < tot) {
if (bufid.equals(ar[i].substring(0,3))) {
System.out.println("Delete : completely");
} else {
pout.println(ar[i]);
}
i = i + 1;
}
pout.close();
} |
การแก้ไขรายการ [5]// 201,101,P
// 201,102,P
// 201,103,P
// 202,101,P
// 202,104,P
import java.io.*;
import java.lang.*;
import java.lang.String;
import java.util.regex.*;
class editgrade {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String bufsubj,bufid,bufgrade;
System.out.print("Wait subject : ");
bufsubj = stdin.readLine();
System.out.print("Wait id : ");
bufid = stdin.readLine();
System.out.print("Wait grade : ");
bufgrade = stdin.readLine();
String b;
String ar[] = new String[100];
FileReader fin = new FileReader("grade.txt");
BufferedReader bin = new BufferedReader (fin);
int i = 0;
while ((b = bin.readLine()) != null) {
ar[i] = b;
i = i + 1;
}
fin.close();
//
int tot = i;
String fld[];
FileOutputStream fout = new FileOutputStream("grade.txt");
BufferedOutputStream bout = new BufferedOutputStream(fout);
PrintStream pout = new PrintStream(bout);
i = 0;
while (i < tot) {
fld = ar[i].split(",");
// System.out.println(fld[0] + fld[1] + bufsubj + bufid);
if (bufsubj.equals(fld[0]) && bufid.equals(fld[1])) {
pout.println(bufsubj + "," + bufid + "," + bufgrade);
// System.out.println("Change");
} else {
pout.println(ar[i]);
}
i = i + 1;
}
pout.close();
}
} |
หน่วยที่ 12 การประมวลผลแฟ้มข้อความ
|
การค้นหาข้อมูล [1]// :::: โปรแกรมลำดับที่ 33
// 1. รับค่าจากแฟ้มพิมพ์แล้วค้นหาในแฟ้ม data.txt
// 2. ค้นหาด้วย split ถ้าพบผลของ split จะได้มากกว่า 1
import java.io.*;
class J0802 {
public static void main (String args[]) throws IOException {
int found=0;
String b,g = "";
String[] fields;
System.out.println("Wait string and enter");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
g = stdin.readLine();
String patternStr = g;
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
while ((b = bin.readLine()) != null) {
fields = b.split(patternStr);
if (fields.length > 1) {
fields = b.split(",");
System.out.println(fields[0] + fields[1] + fields[2] + fields[3]);
found = 1;
}
}
if (found == 0) System.out.println("Not found");
fin.close();
}
} |
การผสานข้อมูล [2]// :::: โปรแกรมลำดับที่ 29
// 1. อ่านข้อมูลจากแฟ้ม data.txt แบบ csv มาแสดงผล
// 101,tom,2000,single
// 102,somchai,5000,married
// 2. คำสั่ง split แยก field ด้วย patternStr
import java.io.*;
class J0701 {
public static void main (String args[]) throws IOException {
int i = 1;
int tot = 0;
String b;
String[] fields;
String patternStr = ",";
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
while ((b = bin.readLine()) != null) {
fields = b.split(patternStr);
System.out.println(i + " : " + fields[0]);
System.out.println("Name : " + fields[1]);
System.out.println("Salary : " + fields[2]);
System.out.println("Status : " + fields[3]);
tot = tot + Integer.parseInt(fields[2]);
i = i + 1;
}
System.out.println("Total : " + tot);
fin.close();
}
} |
mypro// :::: โปรแกรมลำดับที่ 30
// 1. อ่านข้อมูลจาก data.txt เขียนลง data.htm
// 2. นำข้อมูลที่ถูกแยกด้วย , เขียนลง table อย่างเป็นระเบียบ
// 3. DOS>explorer data.htm
// 4. ต.ย. <input type=radio onclick={alert("a");}>
import java.io.*;
import java.lang.*;
class J0702 {
public static void main (String args[]) throws IOException {
int i = 1;
String b;
String[] fields;
String patternStr = ",";
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
FileOutputStream fout = new FileOutputStream("data.htm");
BufferedOutputStream bout = new BufferedOutputStream(fout);
PrintStream pout = new PrintStream(bout);
pout.println("<body bgcolor=yellow><table border=1 width=100%>");
while ((b = bin.readLine()) != null) {
fields = b.split(patternStr);
pout.println("<tr>");
pout.println("<td>"+i+"</td>");
pout.println("<td>"+"ID = " + fields[0]+"</td>");
pout.println("<td>"+"Name = " + fields[1]+"</td>");
pout.println("<td>"+"Salary = " + fields[2]+"</td>");
pout.println("<td>"+"Status = " + fields[3]+"</td>");
pout.println("</tr>");
i = i + 1;
}
pout.println("</table></body>");
fin.close();
pout.close();
}
} |
การประมวลผลข้อมูล [3]// :::: โปรแกรมลำดับที่ 32
// 1. รับค่าจากแป้นพิมพ์ด้วย System.in.read
// 2. นำไปเลือกข้อมูลในแฟ้ม data.txt แล้วแสดงระเบียนที่ตรง
// 3. ค้นหาด้วย .equals()
import java.io.*;
class J0801 {
public static void main (String args[]) throws IOException {
int found=0;
char buf;
String b,g = "";
String[] fields;
String patternStr = ",";
System.out.println("Wait id and end character with [x]");
buf = (char)System.in.read();
while (buf != 'x') {
g = g + buf;
buf = (char)System.in.read();
}
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
while ((b = bin.readLine()) != null) {
fields = b.split(patternStr);
if (fields[0].equals(g)) {
System.out.println(fields[1]);
found = 1;
}
}
if (found == 0) System.out.println("Not found");
fin.close();
}
} |
mypro// :::: โปรแกรมลำดับที่ 34
// 1. อ่านแฟ้ม data.txt และ status array มาเชื่อมกัน
// 2. โดยมีแฟ้มข้อมูล และแฟ้มสถานะภาพ
// 2.1 data.txt : รหัส,ชื่อ,สกุล,สถานภาพ
// 31001,tom,dang,A
// 31002,boy,spy,R
// 31003,big,chem,A
// 2.2 status array : สถานภาพ,คำอธิบายสถานภาพ
// A,Active
// R,Retire
import java.io.*;
class J0901 {
public static void main (String args[]) throws IOException {
int i = 0,t1,t2;
String b,status;
// same as String[] fields;
String fields[];
String[] recs1 = new String[10];
String[] recs2 = {"A,Active","R,Retire"};
String patternStr = ",";
//
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
while ((b = bin.readLine()) != null) {
recs1[i] = b;
i = i + 1;
}
fin.close();
t1 = i;
t2 = recs2.length;
//
for(int j=0;j<t1;j++) {
fields = recs1[j].split(patternStr);
System.out.print(fields[0] + fields[1] + fields[2]+fields[3]);
status = fields[3];
for(int k=0;k<t2;k++) {
fields = recs2[k].split(patternStr);
if (fields[0].equals(status)) {
System.out.println(fields[1]);
}
}
}
}
} |
mypro// :::: โปรแกรมลำดับที่ 35
// 1. อ่านแฟ้ม data.txt และ datas.txt มาเชื่อมกัน
// 2. โดยมีแฟ้มข้อมูล และแฟ้มสถานะภาพ (จำกัด 6 ระเบียน)
// 2.1 data.txt : รหัส,ชื่อ,สกุล,สถานภาพ
// 2.2 datas.txt : สถานภาพ,คำอธิบายสถานภาพ
import java.io.*;
class J0902 {
public static void main (String args[]) throws IOException {
int i = 0,t1,t2;
String b,status;
String[] fields;
String[] recs1 = {"","","","","",""};
String[] recs2 = new String[2];
FileReader fin = new FileReader("data.txt");
BufferedReader bin = new BufferedReader (fin);
while ((b = bin.readLine()) != null) {
recs1[i] = b;
i = i + 1;
}
fin.close();
t1 = i;
i = 0;
//
FileReader fin2 = new FileReader("datas.txt");
BufferedReader bin2 = new BufferedReader (fin2);
while ((b = bin2.readLine()) != null) {
recs2[i] = b;
i = i + 1;
}
fin2.close();
t2 = i;
//
for(int j=0;j<t1;j++) {
fields = recs1[j].split(",");
System.out.print(fields[0] + fields[1] + fields[2]+fields[3]);
status = fields[3];
for(int k=0;k<t2;k++) {
fields = recs2[k].split(",");
if (fields[0].equals(status)) {
System.out.println(fields[1]);
}
}
}
}
} |
หน่วยที่ 13 อาร์เรย์ และการจัดเรียงลำดับ
|
อาร์เรย์ [1]// ::: โปรแกรมลำดับที่ 19
// 1. ประกาศอาร์เรย์ และแสดงข้อมูลในอาร์เรย์ทั้งหมด
class J0501 {
public static void main(String args[]) {
int x[] = {4,18,12};
System.out.println("Amount of array = " + x.length);
for (int i = 0; i < x.length; i++) {
System.out.println("element "+i+" = "+x[i]);
}
}
} |
mypro// ::: โปรแกรมลำดับที่ 20
// 1. ประกาศอาร์เรย์ 2 มิติ และแสดงข้อมูลในอาร์เรย์
// 2. การเลือกมิติที่ต้องการไปแสดงผล
class J0502 {
public static void main(String args[]) {
String a[][] = new String[2][3];
a[0][0] = "101";
a[0][1] = "102";
a[0][2] = "103";
int i = 0;
a[1][i++] = "tom"; // 1,0
a[1][i++] = "dang"; // 1,1
a[1][i++] = "boy"; // 1,2
for (i = 0; i < a[0].length; i++) {
System.out.println("element of 0,"+i+" = "+a[0][i]);
}
for (i = 0; i < a[1].length; i++) {
System.out.println("element of 1,"+i+" = "+a[1][i]);
}
}
} |
การจัดเตรียมข้อมูล [2]// data.txt : รหัส,ชื่อ,สกุล,สถานภาพ // datas.txt : สถานภาพ,คำอธิบายสถานภา |
การสลับข้อมูล [3]tmp = x[j]; x[j] = x[j-1]; x[j-1] = tmp; |
การจัดเรียงข้อมูล [4]// ::: โปรแกรมลำดับที่ 36
// 1. นำอาร์เรย์มาเปรียบเทียบ และจัดเรียงแบบ bubble sort
// 2. ข้อมูลในอาร์เรย์เป็นแบบตัวเลข
// 3. การสลับค่า เช่น t = a; a = b; b = t;
class J1001 {
public static void main (String args[]) {
int tmp,x[] = {5,6,1,2,9,12,9,3};
for(int i=1;i<x.length;i++) {
for(int j=x.length-1;j>=i;j--) {
if(x[j-1] > x[j]) {
tmp = x[j];
x[j] = x[j-1];
x[j-1] = tmp;
}
}
}
for(int i=0;i<x.length;i++) {
System.out.println(x[i]);
}
}
} |
mypro// ::: โปรแกรมลำดับที่ 37
// 1. นำอาร์เรย์มาเปรียบเทียบ และจัดเรียงแบบ bubble sort
// 2. ข้อมูลในอาร์เรย์เป็นแบบ String
// 3. สร้าง function ช่วยในการอ่านข้อมูลจากอาร์เรย์มาพิมพ์
import java.lang.*;
class J1002 {
public static void main (String args[]) {
String tmp,x[] = {"ac","abc","adb","a","aa","acd","a a","a d"};
System.out.println("Before sorting");
prtlist(x);
for(int i=1;i<x.length;i++) {
for(int j=x.length-1;j>=i;j--) {
if(x[j-1].compareTo(x[j])>0) {
tmp = x[j];
x[j] = x[j-1];
x[j-1] = tmp;
}
}
}
System.out.println("After sorting");
prtlist(x);
}
public static void prtlist(String[] x) {
for(int i=0;i<x.length;i++) {
System.out.println(x[i]);
}
}
} |
หน่วยที่ 14 แอพเพล็ต (applet)
|
การพิมพ์ตัวอักษร [1]import java.lang.*;
import java.applet.*;
import java.awt.Graphics;
public class Hello2 extends java.applet.Applet {
public void paint(Graphics g){
g.drawString("hello",10,10);
}
} |
การวาดเส้น [2]// :::: โปรแกรมลำดับที่ 38
// 1. พิมพ์คำว่า test ด้วย Applet
// 2. ห่างซ้าย 10 pixels ห่างจากบน 20 pixels
// 3. ประมวลผลวิธีแรก appletviewer j1101.htm
// 4. ประมวลผลวิธีที่สอง explorer j1101.htm
// <applet code=j1101.class width=200 height=50></applet>
import java.applet.*;
import java.awt.*;
public class J1101 extends java.applet.Applet {
public void paint(Graphics g) {
g.setColor(new Color(240,240,240));
g.drawString("test",10,20);
}
} |
mypro// :::: โปรแกรมลำดับที่ 39
// 1. พิมพ์คำว่า 1 - 10 ด้วย Applet
// 2. รับค่าจาก getParameter
// <applet code=j1102.class width=200 height=50>
// <param name=x value=hello></applet>
import java.applet.*;
import java.awt.*;
public class J1102 extends Applet {
int i,j;
String istr,p;
public void init() {
setBackground(Color.yellow);
p = getParameter("x");
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawString(p,0,10);
i = 1;
while (i <= 10) {
j = 10 * i;
istr= Integer.toString(i);
g.drawString(istr,72,j); // column = 1 inch
i++;
}
}
} |
mypro// :::: โปรแกรมลำดับที่ 40
// 1. เส้นตรงเลื่อนลง ด้วย Applet
// 2. จุดแรกห่างซ้าย 5 และห่างบน 10
// http://mindprod.com/jgloss/sleep.html
import java.applet.*;
import java.awt.*;
public class J1103 extends Applet implements Runnable{
Thread timer;
int row = 10;
public void paint(Graphics g) {
row = row + 2;
g.drawLine(5,row,30,row);
}
public void start() {
timer = new Thread(this);
timer.start(); // start clock
}
public void run() {
Thread me = Thread.currentThread();
while (timer == me) {
try {
// try required for sleep (1000 = 1 Second)
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) { }
repaint();
}
}
} |
การตกแต่ง [3]// :::: โปรแกรมลำดับที่ 41
// 1. Rect = สี่เหลี่ยม Oval = วงรี Arc = เส้นรอบวง
// 2. Oval กว้าง 20 สูง 30
// 3. Arc เริ่มองศาที่ 0 ไปถึงองศา 120
import java.applet.*;
import java.awt.*;
public class J1104 extends Applet {
Image img;
public void init() {
setBackground(Color.green);
img = getImage(getDocumentBase(),"x.gif");
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.drawLine(5,10,30,40);
g.drawRect(50,50,80,80);
g.drawOval(50,50,20,30);
g.setColor(Color.white);
g.fillOval(50,50,20,30); // backgound is white
g.setColor(Color.red);
g.drawArc(40,30,55,55,0,120);
int[] x={0,80,100,5,10};
int[] y={0,50,80,80,30};
g.drawPolygon(x,y,5);
g.drawImage(img, 0, 200, this);
}
} |
การควบคุม [4]// :::: โปรแกรมลำดับที่ 42
// 1. กดปุ่มแล้วเปลี่ยนการ paint
// 2. ใช้ Button, Label และ TextField
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class J1105 extends Applet implements ActionListener {
Button b1 = new Button("1");
Label l1 = new Label("Hello");
TextField t1 = new TextField("1");
int row = 10;
public void paint(Graphics g) {
row = row + 10;
g.drawLine(5,row,30,row);
}
public void init() {
setBackground(Color.red);
add(l1);
add(b1);
add(t1);
t1.addActionListener(this);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int intb1 = Integer.parseInt(e.getActionCommand());
intb1 = intb1 + 1;
String s = Integer.toString(intb1);
l1.setText(s);
b1.setLabel(s);
t1.setText(s);
repaint();
}
} |
หน่วยที่ 15 กรณีศึกษา
|
เมนู [1]// :::: โปรแกรมลำดับที่ 43
// 1. เมนู และการรับตัวเลือกแบบ System.in.read
// 2. ใช้ switch เลือกกระทำ สำหรับ 48 คือ 0, 49 คือ 1
import java.io.*;
class J1201 {
public static void main(String args[]) throws IOException {
int buf=49;
while (buf != 51) {
if (buf >= 49 && buf <= 51) {
System.out.println("What is your option?");
System.out.println("1. print 1 to 10");
System.out.println("2. print 'ok'");
System.out.println("3. exit");
}
// buf = (char)System.in.read(); (it have 13 and 10 on enter)
buf = System.in.read();
switch (buf) {
case 49: // character 1
for (int i=1;i<=10;i++) {
System.out.println(i);
}
break;
case 50: // character 2
System.out.println("ok");
break;
case 51: break; // character 3
case 13: break;
case 10: break;
default:
System.out.println("Nothing to do");
break;
}
}
System.out.println("See you again");
}
} |
การตรวจสอบสิทธิ์ [2]// :::: โปรแกรมลำดับที่ 44
// 1. เมนู และการรับตัวเลือกแบบ stdin.readLine
// 2. ใช้ if เลือกกระทำ แบบอยู่ใน method เดียวกัน
import java.io.*;
class J1202 {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String buf=" ";
while (!buf.equals("3")) {
System.out.println("What is your option?");
System.out.println("1. print 1 to 10");
System.out.println("2. print 'ok'");
System.out.println("3. exit");
buf = stdin.readLine();
if (buf.equals("1"))
for (int i=1;i<=10;i++) System.out.println(i);
if (buf.equals("2")) System.out.println("ok");
}
System.out.println("See you again");
}
} |
ระบบสมาชิก [3]// :::: โปรแกรมลำดับที่ 45
// 1. เมนู และการรับตัวเลือกแบบ stdin.readLine
// 2. ใช้ if เลือกกระทำ แยก method
import java.io.*;
class J1203 {
public static void main(String args[]) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String buf=" ";
while (!buf.equals("3")) {
System.out.println("What is your option?");
System.out.println("1. print 1 to 10");
System.out.println("2. print 'ok'");
System.out.println("3. exit");
buf = stdin.readLine();
if (buf.equals("1")) oho1();
if (buf.equals("2")) { oho2(); }
}
System.out.println("See you again");
}
public static void oho1() {
for (int i=1;i<=10;i++) {
System.out.println(i);
}
}
public static void oho2() {
System.out.println("ok");
}
} |
ระบบจัดการข้อมูล [4]import java.io.*;
class Pollweb {
public static void main (String args[]) throws IOException {
int i=0;
int questionhave = 14;
int q[] = new int[questionhave];
String b;
String[] fields;
String patternStr = ",";
FileReader fin = new FileReader("pollweb.txt");
BufferedReader bin = new BufferedReader (fin);
while ((b = bin.readLine()) != null) {
fields = b.split(patternStr);
for (int j=1;j<=questionhave-1;j++)
q[j]+= Integer.parseInt(fields[j]);
i = i + 1;
}
System.out.println("Total questions: " + i);
for (int j=1;j<=questionhave-1;j++)
System.out.println(j+":"+q[j]+" | "+(q[j] * 100 / i)+"%");
fin.close();
}
} |
myproclass Hello1 {
public static void main(String args[]) {
System.out.println("hello");
}
} |
myproclass Pyramid01 {
public static void main(String args[]) {
int k = 4;
for (int i=1;i<=k;i++) {
for (int j=2;j<=i;j++) { System.out.print(" "); }
System.out.print(i+""+i);
for (int j=k;j>=(i+1);j--) { System.out.print("**"); }
System.out.println(i+""+i);
} } } |
myproclass Pyramid02 {
public static void main(String args[]) {
int k = 4;
for (int i=1;i<=k;i++) {
for (int j=i;j<=(i+2);j++) { System.out.print(j); }
for (int j=1;j<=(2+i);j++) { System.out.print("*"); }
System.out.println();
} } } |
myproclass Pyramid03 {
public static void main(String args[]) {
int k = 4;
for (int i=1;i<=k;i++) {
System.out.print(i+""+(i+4));
for (int j=1;j<=(4+i);j++) {
System.out.print("*");
}
System.out.println();
} } } |
myproclass Pyramid04 {
public static void main(String args[]) {
int k = 4;
for (int i=1;i<=k;i++) {
for (int j=1;j<=i;j++) { System.out.print("*"); }
for (int j=i;j>=2;j--) { System.out.print(j); }
for (int j=1;j<=i;j++) { System.out.print(j); }
System.out.println();
} } } |
การเรียนรู้ในศตวรรษที่ 21 (1/6)
|
ทักษะที่จำเป็นสำหรับการเรียนรู้ในศตวรรษที่ 21 (2/6)
|
ทักษะสำคัญที่คนส่วนใหญ่ให้ความสำคัญมาก (3/6)
|
ทักษะที่จำเป็นสำหรับการเรียนรู้ในศตวรรษที่ 21 (4/6)
|
ความเป็นพลเมืองดิจิทัล (5/6)
|
ทักษะจำเป็น เพื่อก้าวสู่การเป็นพลเมืองดิจิทัล (6/6)
|
5 ซอฟต์สกิล และ 5 ฮาร์ดสกิล เพื่อได้งานและรักษางาน
|
5 ซอฟต์สกิล และ 5 ฮาร์ดสกิล เพื่อได้งานและรักษางาน
|
สุดยอด 3 เรื่องเล่าเร้าพลังใจ
|
| "ไม่เริ่มต้นในวันนี้ จะไม่มีทางสำเร็จในวันพรุ่ง" โดย โยฮัน ว็อล์ฟกัง ฟ็อน เกอเทอ |