วันอังคารที่ 31 กรกฎาคม พ.ศ. 2550

ตัวดำเนินการในภาษาจาวา (Operator) < ลำดับการประมวลผล >

ตัวดำเนินการ (Operator) ในการเขียนโปรแกรมภาษาจาวาประกอบด้วย
- ตัวดำเนินการทางคณิตศาสตร์ (Arithmetic Operator)
- ตัวดำเนินการกำหนดค่า (Assignment Operator)
- ตัวดำเนินการเปรียบเทียบ (Equality and Relational Operator)
- ตัวดำเนินการทางตรรกะ (Logical Operator)


----------

ตัวดำเนินการทางคณิตศาสตร์ (Arithmetic Operator)

เป็นตัวดำเนินการที่จำลองมาจากสมการทางคณิตศาสตร์ โดยในภาษาจาวาจะใช้เครื่องหมายต่างๆ

แทนตัวดำเนินการทางคณิตศาสตร์ต่างๆ ดังนี้



ลำดับการทำงานของตัวดำเนินการทางคณิตศาสตร์เมื่อมีตัวดำเนินการหลายตัวอยู่ในสมการเดียวกัน

เช่น int myNumber = 2 + 2 * 5 / 3; ภาษาจาวามีกฏในการเรียงลำดับ ดังนี้
1. คำนวนสมการที่อยู่ภายในวงเล็บก่อนเป็นอันดับแรก โดยเริ่มจากวงเล็บในสุด


2. ถ้าสมการที่เหลือไม่มีวงเล็บแล้ว จะเรียงลำดับดังนี้


2.1 คูณ, หาร และ mod จะถูกคำนวนก่อน โดยมีความสำคัญอยู่ในระดับเดียวกัน ถ้ามีหลายตัว จะเริ่มคำนวนจากซ้ายไปขวา


2.2 บวก และ ลบ จะถูกคำนวนเป็นลำดับถัดมา โดยมีความสำคัญอยู่ในระดับเดียวกัน ถ้ามีหลายตัว จะเริ่มคำนวนจากซ้ายไปขวา


Ex. 1 y = 2 * 5 * 6 + 3 * 4 + 7


ลำดับการทำงานคือ


1. 2 คูณ 5 ( y = 2 * 5 * 6 + 3 * 4 + 7 )


2. 10 คูณ 6 ( y = 10 * 6 + 3 * 4 + 7 )


3. 3 คูณ 4 ( y = 60 + 3 * 4 + 7 )


4. 60 บวก 12 ( y = 60 + 12 + 7 )


5. y = 72 + 7สุดท้าย ผลลัพธ์เท่ากับ 79


----- ถ้า y = 2 * 5 * (6 + 3) * 4 + 7 -----


ลำดับการทำงานคือ


1. 6 บวก 3 (y = 2 * 5 * 9 * 4 + 7)


2. 2 คูณ 5 (y = 10 * 9 * 4 + 7)


3. 10 คูณ 9 (y = 90 * 4 + 7)


4. 90 คูณ 4 (y = 360 + 7)


5. 360 บวก 7 สุดท้ายผลลัพธ์ เท่ากับ 367


Ex. 2 y = 60 - 6 * 5 + ( 8 + ( 10 / 4 % 2 ) ) * 3


ลำดับการทำงานคือ


1. 10 หาร 4 ( y = 60 - 6 * 5 + ( 8 + ( 10 / 4 % 2 ) ) * 3 )


ปล.1 การหารถ้าทั้งสองตัวมีค่าเป็นจำนวนเต็ม (int) จะเป็นการหารแบบตัดจุดทศนิยมทิ้ง 10 หาร 4 จะได้ 2 ไม่ใช่ 2.5 ยกเว้นเสียแต่ตัวใดตัวหนึ่งจะเป็นตัวแปรชนิด double หรือ float จึงจะหารแล้วคิดเศษทศนิยม


2. 2 mod 2 ( y = 60 - 6 * 5 + ( 8 + ( 2 % 2 ) ) * 3 )


ปล.2 การ mod ถ้าการหารนั้นลงตัว คือไม่เหลือเศษหรือเรียกอีกอย่างว่าเหลือเศษศูนย์ ค่าที่ได้จะเป็น 0


3. 8 บวก 0 ( y = 60 - 6 * 5 + ( 8 + 0 ) * 3 )


4. 6 คูณ 5 ( y = 60 - 6 * 5 + 8 * 3 )


5. 8 คูณ 3 ( y = 60 - 30 + 8 * 3 )


6. 60 ลบ 30 ( y = 60 - 30 + 24 )


7. y = 30 + 24สุดท้าย ผลลัพธ์เท่ากับ 54


credit by: http://www.thaiarchaeology.com/viewtopic.php?board=&id=890


วันเสาร์ที่ 28 กรกฎาคม พ.ศ. 2550

6-7-2007 In class JAVA

เรื่องคลาสMath เช่น Math.PI,Math.E,Math.min(),Math.max(),Math.round(),Math.floor
(),Math.celi(),Math.random(),Math.pow()

ข้อแตกต่างระหว่าง Math.round() ,Math.ceil()และMath.floor() คือ
Math.round() //ปัดเศษขึ้นลง --->ได้ผลลัพธ์เป็น int
Math.ceil() //ปัดเศษขึ้น--->ได้ผลลัพธ์เป็น Double
Math.floor() //ปัดเศษลง --->ได้ผลลัพธ์เป็น Double


รับข้อมูลทางคีบอร์ด คำนวนพื้นที่วงกลม
import java.util.*;

public class circle{ public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.print("input radian: ");
int x=in.nextInt();
Double y ;
y = Math.PI*x*x;
System.out.println("Area :"+y );
}
}

ตัวอย่างที่ 2
public class circle{ public static void main(String[]args){
System.out.println(Math.round(7.61));
System.out.println(Math.round(7.5));
System.out.println(Math.round(7.4));
System.out.println(Math.round(8.2));
System.out.println(Math.round(8.215));
System.out.println(Math.floor(8.9));
System.out.println(Math.ceil(9.5));
System.out.println(Math.ceil(9.9));
System.out.println(Math.ceil(9.215));
}

ตัวอย่างที่ 3
public class circle{
public static void main(String[]args){
float rand1=(float)Math.random();
float rand2=rand1*10;
int rand3=(int)Math.round(rand2);
System.out.println("Basic random is "+rand1);
System.out.println("Bigger range random is "+rand2);
System.out.println("Rounded up random is "+rand3);
int rand4=(int)Math.ceil(Math.random()*10);
System.out.println("Another Ceiling random is "+rand4);
}
}

อาจารย์แนะนำเวป
http://java.sun.com/javase/6/docs/api/java/lang/Math.html

import java.util.*;

public class score1{
public static void main(String[]arg){
Scanner in=new Scanner(System.in);
System.out.print("Input score1 : ");
Double x=in.nextDouble();
System.out.print("Input score2 : ");
Double y=in.nextDouble();
System.out.print("Input score3 : ");
Double z=in.nextDouble();
Double sum;
sum=x+y+z;
if(sum>=50){
System.out.println("ผ่านและเรียนวิชาใหม่");
System.out.println("Take new Couse");
}
else {
System.out.println("ไม่ผ่าน");
System.out.println("เรียนซ้ำ");
}
System.out.println("Bye Bye");
}
}

credit: http://giffykids.spaces.live.com/

วันพฤหัสบดีที่ 5 กรกฎาคม พ.ศ. 2550

JAVA : 29-6-2007

การเปลี่ยนแปลงข้อมูลสายอักขระเป็นข้อมูลตัวเลข และ การเปลี่ยนตัวเลขเป็นสายอักขระ
Integer Type Wrapper สำหรับจำนวนเต็ม

*public Integer(String s)
*public static String toString(int I, int radix)
*public static int parseInt(String s, int radix)
*public static Integer valueOf(Strings, int radix)
// radix คือระบบเลขฐาน เช่น 2, 8 10, 16 โดยปกติจะเป็นฐานสิบ

Double สำหรับจำนวนจริง

*public Double(String s)
*public static String toString(int I, int radix)
*public static Double valueOf(Strings, int radix)
// radix คือระบบเลขฐาน เช่น 2, 8 10, 16 โดยปกติจะเป็นฐานสิบ