1 / 27

Variable, Data type, Expression, Operators Data input, Data output

1106 102 การเขียนโปรแกรมเชิงวัตถุ (Object – Oriented Programming). Variable, Data type, Expression, Operators Data input, Data output. Ubon R atchathani University Mukdahan Campus : อ.วรยุทธ วงศ์นิล. Variable. การประกาศตัวแปร ต้องตั้งชื่อตัวแปร ต้องกำหนดประเภทข้อมูลที่จะเก็บ

sandro
Télécharger la présentation

Variable, Data type, Expression, Operators Data input, Data output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1106 102 การเขียนโปรแกรมเชิงวัตถุ (Object – Oriented Programming) Variable, Data type, Expression, OperatorsData input, Data output Ubon Ratchathani University Mukdahan Campus : อ.วรยุทธ วงศ์นิล

  2. Variable การประกาศตัวแปร • ต้องตั้งชื่อตัวแปร • ต้องกำหนดประเภทข้อมูลที่จะเก็บ • ต้องประกาศก่อนใช้ public class HelloWorld { public static void main(String[] args) { int number = 0; double sum = 0.0; } }

  3. Variable • ตัวแปร คือ ชื่อที่กำหนดขึ้นมาเพื่อใช้ในการจัดเก็บข้อมูล • รูปแบบการตั้งชื่อ data_typevariableName - ชื่อประกอบด้วยตัวอักษร ตัวเลข $ หรือ _ - ห้ามขึ้นต้นด้วยตัวเลข - ตัวอักษรใหญ่ไม่เหมือนตัวเล็ก - ต้องไม่ซ้ำกับคำสงวนของภาษาจาวา - ต้องไม่ซ้ำกับชื่ออื่นของโปรแกรมที่ใช้มาก่อน

  4. Variable virus11 pailin Public month_of_year 7eleven String Yahoo! Kilo per-hour • รูปแบบการกำหนดค่า variableName = variable • หรืออาจจะทำการกำหนดค่าให้ตัวแปรในขั้นตอนการประกาศเลยก็ได้ เช่น float score = 73.25f

  5. Variable ถูก ผิด intn ; double a, b, c ; double x = 0, y, z = 8.5 ; String s = “hello” ; int p ; double q ; Int s ; string t ; y = 6.6 ; double y ; int p, double q ;

  6. Variable การให้ค่ากับตัวแปร • ตัวแปรเก็บได้เฉพาะแบบที่ประกาศไว้ int a = 20 ; //ถูก double x = 1.25 ; //ถูก int b = 1.5 ; //ผิด double y = 2 ; //ถูก int n = “1” ; //ผิด String s = 1 ; //ผิด

  7. Variable • ตัวแปร (variable) มีความหมายเดียวกันกับฟิลด์ (field) • ชนิดของตัวแปร - instance variable (ที่ไม่ใช่ตัวแปรแบบ static) เป็นค่าที่จะไม่เหมือนกันในแต่ละ object - class variable (ตัวแปร static) คือตัวแปรในคลาส จะมีเพียงชุดเดียวเท่านั้น - local variable คือ ตัวแปรที่ถูกใช้ภายในเมธอดเท่านั้น เพื่อใช้เก็บข้อมูลชั่วคราว - parameters คือ ตัวแปรที่ถูกส่งไปยังเมธอดต่างๆ

  8. Operators • ตัวดำเนินการทางคณิตศาสตร์มีลำดับความสำคัญไม่เท่ากัน ดังนี้

  9. Variable จำนวนเต็ม VS จำนวนจริง 5 / 2 ผลลัพธ์ 2 5.0 / 2.0 ” 2.5 5.0 / 2 ” 2.5 5 / 2.0 ” 2.5 5.25 / 0.5 ” 10.5

  10. Expression ตัวอย่าง 2 * 3 + 8 / - (2 – 4) – 1 = 2 * 3 + 8 / - ( - 2) – 1 = 2 * 3 + 8 / 2 – 1 = 6 + 8 / 2 – 1 = 6 + 4 – 1 = 10 – 1 = 9

  11. Expression • 2 + 3 * 4 = • - 5 – 3 * 2 = • (5 + 1 * 5) % 5 + 1 = • 2 * (5 + (17 % 3 / 2) + 26) * 2 + 4 =

  12. การต่อ String System.out.println( “a” + “b” ); //”ab” System.out.println( “a” + 1 ); //”a1” System.out.println( 1 + “a” ); // System.out.println( “1” + “2” ); // System.out.println( “1” + 2 ); // System.out.println( 1 + “2” ); // System.out.println( “a” + 1 + 2 ); // System.out.println( “a” + ( 1 + 2) ); // System.out.println( “a” + ( 1 – 2 )); // System.out.println( “a” + 1 – 2 ); //

  13. Expression ตัวอย่าง การหาพื้นที่ของวงกลมรัศมี 4 ได้ผลลัพธ์เป็น พื้นที่วงกลมรัศมี 4.0 = 50.26544 public class HelloWorld { public staticvoid main(String[] args) { double r = 4; double pi = 3.14159; double area = pi * r * r; System.out.println("พื้นที่วงกลมรัศมี "+ r +" = " + area); } }

  14. Type Conversion int a = 1, b = 2, c ; double d = 2.6 ; double x = 7 ; c = (int)d ; ผลลัพธ์ 2 d = (double)a; ” 1.0 d = (double)( a / b ); ” 0

  15. Constant การกำหนดค่าคงที่ให้กับตัวแปรสามารถทำได้โดยเพิ่มคำว่า “final” เข้าไป เช่น final float TAX_RATE = 0.0725f ; หากต้องการให้ตัวแปรหรือเมธอดใดๆ ถูกเรียกใช้ได้จากทุกที่ หรือทุก class สามารถทำได้โดยเพิ่มคำว่า “static” เช่น static final float TAX_RATE = 0.0725f ;

  16. Data input (Package java.io) แบ่งออกเป็น 2 วิธี คือ - การรับข้อมูลโดยใช้เมธอด read() เป็นการรับทีละตัวอักษรแบบ char ch = (char)System.in.read() ; - การใช้ BufferedReaderร่วมกับ InputStreamReaderโดยใช้เมธอด readLine() รับข้อมูลแบบ Stringทางแป้นพิมพ์ครั้งละหลายตัวอักษร InputStreamReader reader = new InputStreamReader(System.in); BufferedReaderstdin = new BufferedReader(reader); String input = stdin.readLine();

  17. การใช้ read() import java.io.* ; class Read1{ public static void main (String args[])throws IOException { char ch; System.out.print(“Input one character : ”); ch = (char)System.in.read(); System.out.println(“Your input : ” + ch ); } }

  18. การใช้ BufferedReaderร่วมกับ InputStreamReader import java.io.*; class Read2 { public static void main(String arge[]) throws IOException{ InputStreamReader reader = new InputStreamReader(System.in); BufferedReaderstdin = new BufferedReader(reader); String input = “”; System.out.println(“Please input any character : ”); input = stdin.readLine(); System.out.println(“Your input :” + input); } }

  19. Data input (Package java.util) เป็นการรับข้อมูลที่เป็นตัวเลขและนำไปคำนวณต่อได้เลยด้วยคลาส Scanner โดยใช้เมธอด nextInt() import java.util.*; public class InputTest1{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(“Please input number 1”); int num1 = sc.nextInt(); System.out.println(“Please input number2”); int num2 = sc.nextInt(); int result = num1 + num2; System.out.println(“num1 + num2 = ” + result); } }

  20. Data input (Package java.util) คลาส Scanner จะไม่มี Exception มาตรวจสอบความผิดพลาดของข้อมูล แต่จะมีเมธอด hasNext() ไว้ตรวจสอบเงื่อนไขแบบ boolean NextLine() ไว้เลื่อนไปที่บรรทัดถัดไป มาช่วยในการตรวจสอบ

  21. Data input & output (Swing) Swing ถือ package หนึ่งใน Java ที่มีการทำงานของข้อมูลรับเข้าและการแสดงผลในรูปแบบกราฟิก เรียกใช้ด้วยคำสั่ง Import javax.swing.* ;

  22. JOptionPane คลาส JOptionPane มีเมธอดหลักอยู่ 2 เมธอด คือ - showInputDialog() คือ เมธอดสำหรับสร้างจอภาพเพื่อให้ user ป้อนข้อมูล ซึ่งเมธอดจะรับข้อมูลที่อยู่ในรูปของ String String in =JOptionPane.showInputDialog(“input”); - showMessageDialog() คือ เมธอดที่ใช้แสดงผลลัพธ์แจ้งแก่ user เท่านั้น JOptionPane.showMessageDialog(nulll, “sum is :”, “result”, JOptionPane.INFORMATION_MESSAGE);

  23. Data input & output (Swing) ตัวอย่างการแสดงผลแบบ GUI import javax.swing.* ; Class TestSwing{ public static void main(String args[]){ JOptionPane.showMessageDialog(null, “Welcom\nto\nJava\nProgramming!”); System.exit(0); } }

  24. ชนิดของจอภาพที่ใช้แสดงใน showMessageDialog()

  25. ตัวอย่าง การหาผลรวมของเลข 2 จำนวน Import javax.swing.JOptionPane class Addition{ public static void main(String args[]){ String firstNum, secondNum; int num1, num2, sum; firstNum=JOptionPane.showInputDialog(“Enter first integer”); secondNum=JOptionPane.showInputDialog(“Enter second integer”); num1=Integer.parseInt(firstNum); num2=Integer.parseInt(secondNum); sum=num1 + num2; JOptionPane.showMessageDialog(null, “The sum is” + sum, “Result”, JOptionPane.INFORMATION_MESSAGE); System.exit(0); } }

  26. Lab 3 ให้เขียนโปรแกรมคำนวณจำนวนเงินต่อเดือนในการผ่อนรถ Toyota new Vios โดยมีเงื่อนไขคือ - ราคารถ 559,000 บาท - user สามารถกำหนดเอง ต้องการดาวน์กี่เปอร์เซ็นต์ก็ได้ - ที่เหลือผ่อนโดยให้ user กรอก จำนวนเดือนที่ต้องการผ่อน เช่น 12 24 36 48 60 72 หรือ 84 เดือน - ไม่มีดอกเบี้ยในการผ่อน

  27. Lab 3 ตัวอย่างผลลัพธ์ เปอร์เซ็นต์ที่ต้องการดาวน์ : 25 จำนวนเดือนที่ต้องการผ่อน : 48 คุณต้องผ่อนชำระเป็นเงิน 8734.38 บาทต่อเดือน

More Related