JAVA Source Code
// ::: โปรแกรมลำดับที่ 1 (2549-10-17)
// 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
}
}
// :::: โปรแกรมลำดับที่ 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);
}
}
// :::: โปรแกรมลำดับที่ 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
}
}
// :::: โปรแกรมลำดับที่ 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);
}
}
}
// :::: โปรแกรมลำดับที่ 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
}
}
// :::: โปรแกรมลำดับที่ 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;
}
}
}
// :::: โปรแกรมลำดับที่ 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());
}
}
// :::: โปรแกรมลำดับที่ 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
}
}// ::: โปรแกรมลำดับที่ 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");
}
}
}
// ::: โปรแกรมลำดับที่ 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);
}
}
// ::: โปรแกรมลำดับที่ 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);
}
}
// ::: โปรแกรมลำดับที่ 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');
}
}
// ::: โปรแกรมลำดับที่ 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);
}
}
// ::: โปรแกรมลำดับที่ 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);
}
}
// :::: โปรแกรมลำดับที่ 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"); }
}
// :::: โปรแกรมลำดับที่ 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);
}
}
// ::: โปรแกรมลำดับที่ 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);
}
}
// :::: โปรแกรมลำดับที่ 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");
}
}
// ::: โปรแกรมลำดับที่ 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]);
}
}
}
// ::: โปรแกรมลำดับที่ 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]);
}
}
}
// ::::: โปรแกรมลำดับที่ 21
// 1. การแสดงรายละเอียดของแฟ้ม c:/windows/win.ini
// 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("c:/windows/win.ini");
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());
// create hello1.txt have 0 byte
File file = new File("hello1.txt");
boolean success = file.createNewFile(); // 0 byte
// move hello1.txt to hello2.txt
File file2 = new File("hello2.txt");
success = file.renameTo(file2);
// move hello2.txt to c:/hello2.txt
File b = new File("c:/");
success = file2.renameTo(new File(b, file2.getName()));
// not found file to delete so is false
success = (new File("hello2.txt")).delete();
System.out.println(success); // false
// found file to delete so is true
success = (new File("c:/hello2.txt")).delete();
System.out.println(success); // true
}
}
// ::: โปรแกรมลำดับที่ 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());
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// ::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// ::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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]);
}
}
}
}
}
// :::: โปรแกรมลำดับที่ 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]);
}
}
}
}
}
// ::: โปรแกรมลำดับที่ 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]);
}
}
}
// ::: โปรแกรมลำดับที่ 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]);
}
}
}
// ::::: โปรแกรมลำดับที่ 38
// 1. พิมพ์คำว่า test1 ด้วย 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.*; // Microsoft VM
import java.awt.*; // Color
public class j1101 extends java.applet.Applet {
public void paint(Graphics g) {
g.setColor(new Color(0,0,255));
g.drawString("test1",10,20);
g.setColor(Color.red);
g.drawString("test2",10,40);
}
}
// ::::: โปรแกรมลำดับที่ 39
// 1. พิมพ์คำว่า 1 - 10 ด้วย Applet
// 2. รับค่าจาก getParameter
// <applet code=j1102.class width=200 height=300>
// <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); // 72 pixels = 1 inch
i++;
}
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
}
// :::: โปรแกรมลำดับที่ 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);
}
}
// :::: โปรแกรมลำดับที่ 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();
}
}
// :::: โปรแกรมลำดับที่ 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");
}
}
// :::: โปรแกรมลำดับที่ 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");
}
}
// :::: โปรแกรมลำดับที่ 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");
}
}
class 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);
}
}
}class 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();
}
}
}class 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();
}
}
}class 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();
}
}
}