September 06, 2007

WEEK8

String

.legth() - หาขนาดหรือความยาวของstring

.charAt() - สมาชิกของstring

.indexOf() - หาตำแหน่งของสมาชิกของstring

.lastIndexOf() - หาตำแหน่งของสมาชิกของstringตำแหน่งสุดท้าย

+ - เอาstringมาต่อกัน

+= - เอาstringมาต่อกันแล้วเก็บไว้ที่ตัวหน้า

.concat() เอาstringมารวมกัน

.substring() - หาstringย่อยในstringหลัก

.replace() - เปลี่ยนแปลงstring

.replaceAll() - เปลี่ยนแปลงstring

.trim() - ทำให้เล็กลง

.toLowerCase() - เปลี่ยนค่าในstringให้เป็นตัวเล็ก

.toUpperCase() - เปลี่ยนค่าในstringให้เป็นตัวใหญ่

August 16, 2007

WEEK7

Array

การใช้งานตัวแปลarrayเหมือนกับมีกล่องที่เอาไว้เก็บค่าต่างๆ
โดยเราสามารถกำหนดได้ว่ากล่องจะใหญ่แค่ไหนและแบ่งเป็นกี่ช่อง

ตัวอย่างการใช้arrayแบบ2x3
เราจะทำการเรียกใช้ช่องต่างๆในarrayได้เหมือนกับคู่อันดับ
โดยเลขข้างหน้าจะเป็นแกนy และด้านหลังเป็นแกนx
abc[0][0] คือช่องมุมซ้ายบนสุด โดยช่องถัดไปก้อคือ abc[0][1] และ abc[0][2]
แล้วเมื่อต้องการเรียกแถวที่สองก็ทำเหมือนกัน แถวที่2ช่องซ้ายสุดคือ abc[1][0]

การใช้ตัวแปรarrayจะใช้เมื่อเราต้องการเก็บข้อมูลหลายๆค่า
โดยเราสามารถนำมาเก็บในตัวแปรตัวเดียวได้ โดยarrayจะเก็บข้อมูลเป็นลำดับไปเรื่อยๆตั้งแต่0

การนำloopเข้ามาใช้ในตัวแปรarrayทำให้เราสามารถใช้งานarrayได้ดีขึ้น
โดยการป้อนข้อมูลหลายๆค่าใส่ลงในตัวแปรarrayเราจะนำการใช้loopมาใช้
ในการเรียกตำแหน่งต่างๆในตัวแปร เพื่อให้ง่ายต่อการเขียนคำสั่งในการรับค่า

ตัวอย่างการใช้loopในการเรียกใช้ข้อมูลในตัวแปรarray
for(i=0; i<3; i++) {
System.out.print(abc[i]);
}
ในการเขียนคำสั่งแบบนี้หมายความว่าprogramจะแสดงค่า
ของตัวแปร abc[i] ตั้งแต่ i=0 ถึง i=2
จะเห็นได้ว่าการใช้loopเข้ามาช่วยจะทำให้เราไม่ต้องเขียนคำสั่งหลายบรรทัด
ถ้าไม่ใช่loop
System.out.print(abc[0]);
System.out.print(abc[1]);
System.out.print(abc[2]);



// studScore.java

import java.util.Scanner;
public class studScore {
    public static void main(String args[]) {

       Scanner obj = new Scanner(System.in);

       int i, j;
       int studNum;

       System.out.print("Student Number= ");
       studNum = obj.nextInt();

       String stud[][] = new String [studNum][4];

       for(i=0; i< studNum; i++) {
          for(j=0; j<1; j++) {
             System.out.print("Name= ");
             stud[i][j] = obj.next();
          }
          for(j=1; j<4; j++) {
             System.out.print("Point"+j+"= ");
             stud[i][j] = obj.next();
         }
      }

       for(i=0; i< studNum; i++) {
          for(j=0; j<1; j++) {
             System.out.print("Name= ");
             System.out.print(stud[i][j]);
          }
          for(j=1; j<4; j++) {
             System.out.print(" Point"+j+"= ");
             System.out.print(stud[i][j]);
          }
          System.out.println();
       }

       double sum = 0;

       for(i=0; i< studNum; i++) {
             for(j=1; j<4; j++) {
                sum += Double.parseDouble(stud[i][j]);
             }
       }

       double max = 0;
       double min = Double.POSITIVE_INFINITY;

       for(i=0; i< studNum; i++) {
          for(j=1; j<4; j++) {
             if (max < (Double.parseDouble(stud[i][j]))) {
                max = Double.parseDouble(stud[i][j]);
             }
             if (min > (Double.parseDouble(stud[i][j]))) {
                min = Double.parseDouble(stud[i][j]);
             }
          }
       }

       System.out.println("Sum= " + sum);
       System.out.println("Max= " + max);
       System.out.println("Min= " + min);

    }
}


DOWNLOAD : studScore.java

ในตอนแรกprogramจะรับค่า จำนวนนักเรียน จากkeyboardเข้ามาใส่ตัวแปร studNum
แล้วก็จะรับค่าต่างๆ:ชื่อนักเรียน, คะแนน1, คะแนน2, คะแนน3
เพื่อบันทึกลงในตัวแปล stud[][] แบบ array โดยความสูงของตารางarrayจะขึ้นอยู่กับจำนวนนักเรียนและความกว้างเท่ากับจำนวนข้อมูล

เมื่อprogramรับค่าทั้งหมดแล้วเก็บไว้ในตัวแปลครบหมดแล้ว
ก็จะคำนวณหา sum, max และ min โดยใช้loopเข้ามาช่วย



// run

Student Number= 1

Name= abc
Point1= 1
Point2= 2
Point3= 3

Name= abc Point1= 1 Point2= 2 Point3= 3

Sum= 6.0
Max= 3.0
Min= 1.0


ใส่จำนวนนักเรียน แล้วใส่คะแนนของแต่ละคน programจะหาแสดงผลข้อมูลที่ป้อนเข้าไป และคำนวณหาผลรวม ค่าต่ำสุด สูงสุด


// boxSum.java

import java.util.Scanner;
public class boxSum {
  public static void main(String args[]) {

    Scanner obj = new Scanner(System.in);

       int i, j;
       int box1[][] = new int [3][3];
       int box2[][] = new int [3][3];
       int box3[][] = new int [3][3];

       for(i=0; i<3; i++) {
          for(j=0; j<3; j++) {
             System.out.print("box1["+i+"]["+j+"]= ");
             box1[i][j] = obj.nextInt();
          }
       }

       for(i=0; i<3; i++) {
          for(j=0; j<3; j++) {
             System.out.print("box2["+i+"]["+j+"]= ");
             box2[i][j] = obj.nextInt();
          }
       }

       for(i=0; i<3; i++) {
          for(j=0; j<3; j++) {
             box3[i][j] = box1[i][j] + box2[i][j];
             System.out.print("box3["+i+"]["+j+"]= " + box3[i][j] + " ");
          }
          System.out.println();
       }

    }
}


DOWNLOAD : boxSum.java


// run

box1[0][0]= 1
box1[0][1]= 2
box1[0][2]= 3
box1[1][0]= 4
box1[1][1]= 5
box1[1][2]= 6
box1[2][0]= 7
box1[2][1]= 8
box1[2][2]= 9

box2[0][0]= 1
box2[0][1]= 2
box2[0][2]= 3
box2[1][0]= 4
box2[1][1]= 5
box2[1][2]= 6
box2[2][0]= 7
box2[2][1]= 8
box2[2][2]= 9

box3[0][0]= 2 box3[0][1]= 4 box3[0][2]= 6
box3[1][0]= 8 box3[1][1]= 10 box3[1][2]= 12
box3[2][0]= 14 box3[2][1]= 16 box3[2][2]= 18


ป้อนข้อมูลเพื่อเก็บค่าในตาราง3x3ทั้ง2ตาราง programจะคำนวณหาผลบวกของแต่ละช่องของตารางแล้วแสดงผลในตารางที่3

July 26, 2007

WEEK6

else if & case switch


// numberif.java

import java.util.Scanner;

public class numberif {

    public static void main(String args[]) {

       Scanner obj = new Scanner(System.in);

       System.out.print("num= ");
       int num = obj.nextInt();

       if(num == 50) {

          System.out.println("Number = 50");

       }

       else if (num > 50) {

          System.out.println("Number > 50");

       }

       else if(num < 50) {

          System.out.println("Number < 50");

       }

    }

}


DOWNLOAD : numberif.java

ใช้ if และ else if ในการสร้างเงื่อนไข


// run

num= 50

Number= 50


ถ้าใส่ค่า50programจะขึ้นว่า Number= 50


// run

Number= 20

Number < 50


ถ้าใส่ค่าน้อยกว่า50programจะขึ้นว่า Number < 50


// run

Number= 80

Number > 50


ถ้าใส่ค่ามากกว่า50programจะขึ้นว่า Number > 50


// javaswitch.java

import java.util.Scanner;

public class numberif {

    public static void main(String args[]) {


             Scanner obj = new Scanner(System.in);

             System.out.print("num= ");
             int num = obj.nextInt();

             switch (num) {

                   case 0 :; case 1 :; case 2 :; case 3 :; case 4 :; case 5 :; case 7 :; case 8 :; case 9 :; case 10 :; case 11 :; case 12 :; case 13 :; case 14 :; case 15 :; case 16 :; case 17 :; case 18 :; case 19 :; case 20 :; case 21 :;case 22 :;case 23 :; case 24 :; case 25 :; case 26 :; case 27 :; case 28 :; case 29 :; case 30 :; case 31 :; case 32 :; case 33 :; case 34 :; case 35 :; case 36 :; case 37 :; case 38 :; case 39 :; case 40 :; case 41 :; case 42 :; case 43 :; case 44 :; case 45 :; case 46 :; case 47 :; case 48 :;
                   case 49 : System.out.println("Number < 50"); break;

                   case 50 : System.out.println("Number = 50"); break;

                   default : System.out.println("Number > 50");

       }

    }

}


DOWNLOAD : javaswitch.java

ใช่ switch ในการสร้างเงื่อนไข


// run

num= 50

Number= 50


ถ้าใส่ค่า50programจะขึ้นว่า Number= 50


// run

Number= 20

Number < 50


ถ้าใส่ค่าน้อยกว่า50programจะขึ้นว่า Number < 50


// run

Number= 80

Number > 50


ถ้าใส่ค่ามากกว่า50programจะขึ้นว่า Number > 50

การใช้คำสั่งทั้ง2แบบจะให้ผลลัพธ์เหมือนกัน แต่การใช้ if จะเหมาะสมกว่าในการสร้างprogramนี้

July 19, 2007

WEEK5

If Else


// jackpot.java

import java.util.*;

public class jackpot {

    public static void main(String args[]) {

       Scanner obj = new Scanner(System.in);

       System.out.print("Number= ");
       int num1 = obj.nextInt();

       if(num1 == 120) {

          System.out.println("JACKPOT");

       } else {

          System.out.println("FAIL");

       }


    }

}


DOWNLOAD : jackpot.java

ให้รับค่า num1 มาจากkeyboard ถ้า num1 เท่ากับ 120 ให้programขึ้นว่า JACKPOT แต่ถ้า num1 ไม่เท่ากับ 120 ให้ขึ้นว่า FAIL


// run

Number= 120

JACKPOT


ถ้าใส่ค่าถูกต้องprogramจะขึ้นว่า JACKPOT


// run

Number= 20

FAIL


ถ้าใส่ค่าผิดprogramจะขึ้นว่า FAIL

July 05, 2007

WEEK4

Keyboard Input


// input.java

import java.util.*;

public class input {

    public static void main(String [] args) {

       Scanner obj = new Scanner(System.in);

       System.out.print("score1= ");
       float score1 = obj.nextInt();

       System.out.print("score2= ");
       float score2 = obj.nextInt();

       System.out.print("score3= ");
       float score3 = obj.nextInt();

       System.out.println("sum score= " + ( score1 + score2 + score3 ));

       System.out.println("average score= " + (( score1 + score2 + score3)/3 ));

    }

}


DOWNLOAD : input.java

import java.util.*; - เรียกpackage

Scanner obj = new Scanner(System.in); - obj รับข้อมูลจากkeyboard

float score1 = obj.nextInt(); - ให้ตัวแปร score1 รับค่าจาก obj


// run

score1= 10

score2= 20

score3= 30

sum score= 60.0

average score= 20.0


เมื่อrun programจะให้เราป้อนค่า score1, score2, score3

June 28, 2007

WEEK3

Parameter

จำนวนเต็มมี 4 ชนิด
1. byte
2. short
3. int
4. long

ทศนิยมมี 2 ชนิด
1. float
2. double


// sumScore.java

public class sumScore {

    public static void main(String [] args) {

       System.out.println(args[0]);

       System.out.println("score1= " + args[1]);

       System.out.println("score2= " + args[2]);

       System.out.println("sum score= " + (Integer.parseInt(args[1])+Integer.parseInt(args[2])));

    }

}


DOWNLOAD : sumScore.java

ตัวแปร args[] จะรับข้อมูลเมื่อเราใส่ให้

args[0] - เป็นการอ้างถึงตัวแปร args ลำดับที่1

Integer.parseInt(args[1]) - การเปลี่ยนข้อมูลของตัวแปร ให้เป็น integer


// run on cmd

java sumScore "NAME" 10 20

NAME

score1= 10

score2= 20

sum score= 30



java sumScore "NAME" 10 20 - args[0] = NAME, args[1] = 10, args[2] = 20

sum score= 30 - เมื่อนำ args[1] กับ args[2] มาเปลี่ยนป็นintegerแล้วบวกกัน จะได้คำตอบออกมา

June 21, 2007

WEEK2

Data type & Arithmatix operation

จำนวนมี 2 แบบ คือ จำนวนเต็ม กับ ทศนิยม
เมื่อคำนวนแบบจำนวนเต็มprogramจะไม่แสดงทศนิยม เช่น 1/5 จะเท่ากับ 0


// rec.java

public class rec {

    public static void main(String [] args) {

       System.out.println("Rectangle area = 1/2 * base * height");

       System.out.println("Base = " + "10");

       System.out.println("Heigh = " + "20");

       System.out.println("Rectangle area = " + (1.0/2 * 10 * 20));

    }

}


DOWNLOAD : rec.java

1.0 - ใส่ค่าให้เป็นทศนิยมเพื่อที่้programจะได้คำนวณเป็นทศนิยม

ถ้าเอา 1/2 แบบจำนวนเต็ม จะได้ 0 เพราะ programจะคิดแค่จำนวนเต็มเท่านั้น

ผลที่ได้เมื่อแก้ไขprogramให้ถูกต้อง


// run

Rectangle area = 1/2 * base * height

Base = 10

Heigh = 20

Rectangle area = 100.0

June 15, 2007

WEEK1

My First JAVA PROGRAMMING

1. สร้างfile *.java ขึ้นมาแล้วใส่คำสั่งที่จะrunลงไป

// first.java

public class first {

    public static void main(String [] args) {

       System.out.println("Hello World!");

    }

}


DOWNLOAD : first.java

public class - ทำให้สามารถใช้งานclassนี้ได้

public static void main - เป็นmethodที่ใช้ตอนเริ่มprogram

String [] args - เป็นparameter

println - คำสั่งให้programแสดงสิ่งที่เรากำหนด

; - ใช้เพื่อเป็นการจบคำสั่ง


2. เมื่อสร้างfileเสร็จแล้วให้นำไปrunบน cmd

3. เลือกfolderที่เราsave fileไว้แล้วป้อนคำสั่ง



// run on cmd

javac first.java

java first


javac - java compiler

java - เรียกใช้


4. เมื่อป้อนคำสั่ง javac ถ้าerrorแสดงว่าคำสั่งในfileที่เราสร้างขึ้นมาเกิดข้อผิดพลาด

5. เมื่อป้อนคำสั่ง java คือการrun program