4. • ให้สร้าง Class ขึ้นมาใหม่ให้ชื่อว่า
TestMethodMath
ตัวอย่าง Method ที่มีใน Class Math
5. • ให้มันสามารถรับค่า Double ได้ 1 ค่า
• พร้อมทั้งแสดงค่าที่รับออกมา
ตัวอย่าง Method ที่มีใน Class Math
Enter number: 9.2
Number is: 9.2
6. • ให้เรียกใช้ method ceil(x)
• วิธีการเรียกใช้ --> Math.ceil(x);
ตัวอย่าง Method ที่มีใน Class Math
ตัวแปรที่รับตัวแปรที่รับ
ค่าเข้ามาค่าเข้ามา
Enter number: 9.2
Number is: 9.2
Ceiling of 9.2 is: ???
7. • ให้ลองเรียกใช้ Method อื่นๆ
• Math.floor(x);
• Math.sqrt(x);
• Math.pow(x,3);
• ...
ตัวอย่าง Method ที่มีใน Class Math
ตัวยกกำาลังตัวยกกำาลัง
Floor of 9.2 is: ???
Square root of 9.2 is: ???
Power three of 9.2 is: ???
8. • ให้แสดงผลของ Math.pow(x,3); ให้อยู่
ในรูปแบบของทศนิยม 1 ตำาแหน่ง
ตัวอย่าง Method ที่มีใน Class Math
Power three of 9.2 is: ???
OneDigit of Power three of 9.2 is: ???
9. การประกาศ Method
public X Y (para1, ..., paraN){...}
ชนิดของการชนิดของการ returnreturn ค่าค่า
ถ้าเป็นถ้าเป็น voidvoid จะไม่มีการจะไม่มีการ returnreturn ค่ากลับค่ากลับ
ถ้าเป็นถ้าเป็น int, double, float, ...int, double, float, ... จะมีการจะมีการ returnreturn ค่ากลับค่ากลับ
ชื่อของชื่อของ MethodMethod
10. การประกาศ Method
public class Test
{
public static void main(String[] args)
{... }
public static int Display()
{... return ...}
public void Display()
{...}
}
13. การเรียกใช้ Method
Static Method
public static void Display(int x){...}
Instance Method
public String Display(){... return... }
Display(y);
Test t = new Test();
t.Display();