1 / 42

Lec05 :: สายอักขระ String , String Builder and String Buffer Lec06 :: คลาส Math คลาส Format

Lec05 :: สายอักขระ String , String Builder and String Buffer Lec06 :: คลาส Math คลาส Format. การเขียนโปรแกรมเชิงวัตถุ โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com xnattapong@hotmail.com. String , StringBuilder และ StringBuffer.

gada
Download Presentation

Lec05 :: สายอักขระ String , String Builder and String Buffer Lec06 :: คลาส Math คลาส Format

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. Lec05 :: สายอักขระ String , String Builder and String BufferLec06 :: คลาส Math คลาส Format การเขียนโปรแกรมเชิงวัตถุ โดย อ. นัฐพงศ์ ส่งเนียม http://www.siam2dev.com xnattapong@hotmail.com

  2. String , StringBuilder และ StringBuffer • ชนิดข้อมูลพื้นฐานที่เป็นตัวอักษร char สารมารถนำเสนอตัวอักษรได้เพียงตัวเดียว ซึ่งไม่เพียงพอต่อการนำไปใช้งานทั่วไปได้ โปรแกรมส่วนใหญ่จึงต้องการนำเสนอข้อมูลที่เป็นคำหรือประโยค ก็คือสามารถนำตัวอักษรมาเรียงต่อกันเป็นสาย คล้ายกับสายอักษร แต่ นอกจากเก็บตัวอักษรได้แล้ว ยังสามารถเก็บตัวเลข และสัญลักษณ์อื่นได้ อีกด้วย จึงเรียกรวมกันว่าสายอักขระ ภาษาจาวามีชนิดข้อมูลที่เป็นสายอักขระได้ ซึ่งก็คือชนิดข้อมูล String , StringBuilder และ StringBuffer นั่นเอง

  3. สายอักขระ • จากโปรแกรม HelloWorld! • จะเห็นว่า คำที่พิมพ์ออกมานั้นประกอบไปด้วย H,e,l,l,o,... เรียงต่อกัน เราจึงเรียกว่า สายอักขระ (String) public class hello_world { public static void main(String args[]) { System.out.println(“Hello World!”); } }

  4. คลาสสตริง (String class) • เนื่องจากสายอักขระมีประโยชน์มากมาย ภาษาจาวาจึงมีชนิดข้อมูลที่นำเสนอสายอักขระได้ แต่ชนิดข้อมูลที่นำเสนอสายอักขระนี้ไม่จัดเป็นข้อมูลชนิดพื้นฐาน แถมยังมีลักษณะพิเศษเพิ่มเติมเข้ามา เราจึงใช้คำว่า คลาส(class) แทนคำว่าชนิดข้อมูล (data type)

  5. Java String • สายอักษร (String) ในภาษาจาวา • ไม่ใช่เป็นเพียงเนื้อที่ในหน่วยความจำที่จองไว้สำหรับประเภทข้อมูล แต่เป็นวัตถุ (Object) • ตัวแปร String ในภาษาจาวา • เก็บค่าอ้างอิงของ String Object • เมื่อมีการอ้างอิงค่าไปยัง String Object แล้ว จะไม่สามารถเปลี่ยนแปลงค่าใน String Object ได้โดยตรง แต่สามารถให้ค่าอ้างอิงใหม่ได้ String message = new String("Hello"); หรือ String message = "Hello";

  6. 1 String empty = new String(""); 2 String message = "Hello"; 3 String repeat = message; empty "" aaa stored at aaa stored at xxx bbb String "" String "" aaa xxx After Line 1 yyy stored at aaa zzz Conceptual View Memory Address View String Declaration (1) • การประกาศตัวแปร String ในภาษาจาวา

  7. 1 String empty = new String(""); 2 String message = "Hello"; 3 String repeat = message; empty stored at aaa stored at xxx empty String "" "" "" aaa aaa String "" stored at aaa stored at xxx " Hello" bbb bbb stored at aaa After Line 2 String "" String "" aaa aaa xxx xxx message After Line 1 bbb yyy yyy stored at yyy stored at bbb stored at aaa String ”Hello" zzz zzz String ”Hello" stored at bbb Conceptual View Memory Address View Memory Address View Conceptual View String Declaration (2) • การประกาศตัวแปร String ในภาษาจาวา

  8. 1 String empty = new String(""); 2 String message = "Hello"; 3 String repeat = message; empty empty stored at aaa stored at aaa stored at xxx stored at xxx String "" "" aaa String "" "" aaa String "" String "" " Hello" bbb " Hello" bbb stored at aaa message After Line 3 stored at aaa After Line 2 aaa xxx aaa xxx message stored at yyy bbb bbb yyy String ”Hello" yyy stored at bbb stored at yyy stored at bbb String ”Hello" bbb Repeat zzz String ”Hello" zzz String ”Hello" stored at bbb stored at bbb stored at zzz Memory Address View Memory Address View Conceptual View Conceptual View String Declaration (3) • การประกาศตัวแปร String ในภาษาจาวา

  9. The “String” Class • String Class • ค่าใน String Object ไม่สามารถเปลี่ยนแปลงได้ แต่สามารถให้ค่าอ้างอิงจากตัวแปร String ใหม่ได้ • Methods ของ String Class • Create New String concat, replace, subString, toLowerCase, toUpperCase • Search endswith, startswith, indexOf, lastIndexOf • Comparison equals, equalsIgnoreCase, CompareTo • others charAt, length

  10. String Comparison • การเปรียบเทียบ String ในภาษาจาวา • เปรียบเทียบค่าใน String Object ว่าเท่ากันหรือไม่ • ใช้เมทธอด boolean equals(String s) • เปรียบเทียบว่าตัวแปร String อ้างอิงถึง String Object เดียวกันหรือไม่ • ใช้โอเปอร์เรเตอร์ (Operator) “==” • เปรียบเทียบค่าใน String Object เรียงตามลำดับตัวอักษร • ใช้เมทธอดint compareTo(String s) • return ค่า 0 เมื่อเป็นค่าเดียวกัน, ค่าจำนวนเต็มบวกเมื่อ String Object นี้มากกว่า s และ ค่าจำนวนเต็มลบ เมื่อ String Object นี้น้อยกว่า s

  11. String greeting = new String("Hello"); String message = "Hello"; if (greeting.equals(message)) { System.out.println("equal content!"); } else { System.out.println("content NOT equal!"); } greeting stored at aaa stored at xxx String "Hello" "Hello" aaa String "Hello" "Hello" bbb stored at aaa aaa xxx message bbb yyy stored at yyy stored at bbb String "Hello" zzz String "Hello" stored at bbb Memory Address View Conceptual View Comparing String Contents • ตัวอย่าง

  12. greeting stored at aaa stored at xxx String "Hello" "Hello" aaa String "Hello" "Hello" bbb stored at aaa aaa xxx message bbb yyy stored at yyy stored at bbb String "Hello" zzz String "Hello" stored at bbb Memory Address View Conceptual View Comparing String Reference • ตัวอย่าง String greeting = new String("Hello"); String message = "Hello"; if (greeting ==message) { System.out.println("equal reference!"); } else { System.out.println(”reference NOT equal!"); }

  13. String greeting = new String("Hello"); String message = ”World"; System.out.println(greeting.compareTo(message)); Comparing String Lexicographical Order • การเปรียบเทียบค่า String ตามลำดับตัวอักษรใช้เมทธอด int compareTo(String s) • return ค่า 0 เมื่อเป็นค่าเดียวกัน • return ค่าจำนวนเต็มบวกเมื่อ String Object นี้มากกว่า s • return ค่าจำนวนเต็มลบ เมื่อ String Object นี้น้อยกว่า s

  14. String Concatenation • การนำ String มาเชื่อมต่อกัน • ใช้โอเปอร์เรเตอร์ Concatenate (+) • แต่ไม่สามารถเปรียบเทียบ String โดยใช้โอเปอร์เรเตอร์แบบ Relational (>, <, >=, <=) และโอเปอร์เรเตอร์แบบ Equality ( ==, !=) ได้ String greetings = new String("Hello”); int ThisYear = 2001; String name = " World " + 2001; String message = greetings + name; • หมายเหตุ • ใช้โอเปอร์เตอร์ (+) กับ String ได้เมื่อมีพจน์ใดพจน์หนึ่งเป็น String

  15. String • วัตถุ String สามารถนำเสนอข้อมูลที่เป็นสายอักขระได้ การใช้งานสตริงมีขั้นตอนดังต่อไปนี้ 1. การประกาศตัวแปร โดยเขียน คำว่า String แล้วตามด้วยชื่อตัวแปร String name; 2.การกำหนดค่า ให้กับตัวแปร name ให้ใช้เครื่องหมาย = และสร้างวัตถุ String ด้วยคำสั่ง new name = new String(“Nattapong Songneam”) 3. แสดงผลด้วยคำสั่ง System.out.println(name);

  16. การสั่งงาน String • วัตถุ String ไม่เพียงแค่ใช้เก็บบันทึกสายอักขระและแสดงผลออกมาเท่านั้น แต่ยังมีความสามารถพิเศษ อื่นอีกดังนี้ 1. การนับความยาวของสายอักขระ จะใช้ lenght() name.lenght(); 2.แสดงตัวอักษรในตำแหน่งที่กำหนด จะใช้ charAt() name.charAt(1); 3. แสดงตำแหน่งของสายอักขระในสตริง จะใช้ indexof() name.indexof(“a”); 3. หาสตริงในช่วงที่กำหนด จะใช้ substring() name.substring(1,3);

  17. การสั่งงาน String public class TestString01 { public static void main(String args[]) { String Name =“Nattapong Songneam”; System.out.println(name.lenght()); System.out.println(name.charAt(3)); System.out.println(name.indexof(“S”)); System.out.println(name. substring(1,9)); } }

  18. การเปรียบเทียบ String public class TestString02 { public static void main(String args[]) { String Name1 =“Nattapong Songneam”; String Name2 =“Supaporn Jarnsuk”; if(Name1 == Name2) { System.out.println(“name is as once”); } else { System.out.println(“name is as once”); } } }

  19. การเปรียบเทียบ String public class TestString01 { public static void main(String args[]) { String Name1 =“Nattapong Songneam”; String Name2 =“Supaporn Jarnsuk”; if(Name1.equal(Name2)) { System.out.println(“name is as once”); } else { System.out.println(“name is as once”); } } }

  20. การประยุกต์ใช้งาน String กับการเข้ารหัสข้อมูล

  21. package encrypt; /** * @author Nattapong Songneam * http://www.siam2dev.com * xnattapong@hotmail.com */ public class Main { public static void main(String[] args) { String KEY = new String("zxcvbnmasdfghjklqwertyuiop"); char KeyTable[][]= new char[26][2]; for(int i=0;i<26;i++) { KeyTable[i][0] =(char)('a' + i); KeyTable[i][1] = (KEY.charAt(i)); } String PlainText = "Nattapong Songneam"; String CipherText=""; int Len = PlainText.length(); for(int k=0;k<Len;k++) { for(int i=0;i<26;i++) { char a = KeyTable[i][0]; char b = PlainText.charAt(k); if(a==b) { CipherText +=Character.toString(KeyTable[i][1]); } } } System.out.println(CipherText); } }

  22. คลาส Math • ชนิดข้อมูลพื้นฐานที่เป็นตัวเลข เช่น int หรือ double นั้น สามารถนำมาคำนวณได้ ภาษาจาวามีคำสั่งสำหรับการคำนวณง่าย ๆ เช่น การบวก ลบ คูณ หาร ซึ่งสำหรับการคำนวณที่ซับซ้อนขึ้น เช่น การคำนวณ ตรีโกณมิติ ลอกการิทึม นั้นภาษาจาวา ก็ทำได้เช่นกันโดย ภาษาจาวาได้ จัดเตรียมคลาส Math ไว้ช่วยในการคำนวณให้เรียบร้อยแล้วดังนี้

  23. เมธอดต่าง ๆ ใน คลาส Math • sqrt() • cbrt() • pow() • abs() • ceil() • floor() • round() • min() • max()

  24. โปรแกรมคำนวณค่า พ.ท. วงกลม public class TestMath01 { public static void main(String args[]) { double r = 10.0d; // r = radius double circleArea= Math.pi*r*r System.out.println(circleArea) } }

  25. โปรแกรมคำนวณค่า พ.ท. วงกลม แบบที่ 2 public class TestString01 { public static void main(String args[]) { double r = 10.0d; // r = radius double circleArea= Math.pi*Math.pow(r,2); System.out.println(circleArea) } }

  26. โปรแกรมคำนวณค่า เส้นรอบ วงกลม public class TestString01 { public static void main(String args[]) { double r = 10.0d; // r = radius double circular = 2*Math.pi*r; System.out.println(circular) } }

  27. public static void main(String[] args) { • // TODO code application logic here • int x=2; • long y= (long)(2*Math.pow(x,8) + 5*Math.pow(x,6) • + 3*Math.pow(x,3) + 2*x + 7); • System.out.println(y); • }

  28. โปรแกรมคำนวณค่า โปรแกรมสุ่มตัวเลข 1-100 public class TestMath03 { public static void main(String args[]) { for(int i=1;i<100;i++) { int k = (int)(Math.random()*100); System.out.println(k); } } }

  29. การรับข้อมูลจากผู้ใช้งาน ทางคีย์บอร์ด java.util.Scanner sc; sc = new java.util.Scanner(System.in); • int y = sc.nextInt(); // รับข้อมูลที่เป็น ตัวเลขจำนวนเต็ม int • double y = sc.nextDouble(); // รับข้อมูลที่เป็น ตัวเลขทศนิยม double • String y = sc.next() // รับข้อมูลที่เป็นข้อความ String

  30. Excercise 6_1 • จงเขียนโปรแกรม รับ ค่า ความกว้าง , ความสูง จาก คีย์บอร์ดแล้ว คำนวณ ค่า พ.ท. สี่เหลี่ยมผืนผ้า • จงเขียนโปรแกรม รับ ค่า ความกว้าง , ฐาน จาก คีย์บอร์ดแล้ว คำนวณ ค่า พ.ท. สามเหลี่ยม • จงเขียนโปรแกรม คำนวณ ค่า พ.ท. สี่เหลี่ยมคางหมู โดย รับ ข้อมูลต่าง ๆ จากคีย์บอร์ด

  31. คลาสในกลุ่ม Format แทรกคำสั่ง ก่อน public class Main { import java.text.DecimalFormat; double d = sc.nextDouble(); DecimalFormat cF = new DecimalFormat("0.00"); System.out.println(cF.format(d)); DecimalFormat cF = new DecimalFormat(“#,###.00"); System.out.println(cF.format(d));

  32. Java Programming [Class library] By Nattapong Songneam http://www.siam2dev.com

  33. Java Class library • java เป็นภาษาขนาดเล็ก เพราะมีเพียงกลไกพื้นฐานสำหรับการโปรแกรมทั่วไปและที่เกี่ยวกับการโปรแกรมเชิงวัตถุเท่านั้น แต่ได้กลไกเฉพาะอย่างถูกสร้างขึ้นเป็นคลาสเก็บไว้ใน standard library ของภาษา คือไฟล์ classes.zip ที่มากับ jdk ซึ่งคลาสจำนวนมากจะเก็บไว้ใน package มาตรฐานของภาษา มีชื่อว่า java ซึ่งจะถูกแบ่งเป็น sub-package ย่อย ๆ ลงไปสำหรับแต่ละงาน เช่น java.io. เป็นที่เก็บคลาสเกี่ยวกับ stream เป็นต้น

  34. java.lang • มีคลาสที่สำคัญ ๆ เช่น String,math,System,Object,Runtime,Process,Class,Compiler,SecurityManager • คลาส String มี constructors หลายตัวดังนี้ • public String(); • public String(String value); • public String(char[] value); • public String(char[] value,int offset,int count); • public String(StringBuffer buffer);

  35. //string class StringDef { public static void main(String args[]) { String a = new String(“sawatdee”); String b = new String(); String c = “sawatdee”; char d[] = {‘s’,’a’,’w’,’a’,’t’,’d’,’e’,’e’}; String e = new String(d); String f = new String(d,0,4); byte g[] = {‘s’,’a’,’w’,’a’,’t’,’d’,’e’,’e’}; String h = new String(g); } } *คลาส string ยังมี method ที่จำเป็นสำหรับใช้งานมากมาย เช่น length(),charAt(),substring(), trim(),valueOf()

  36. java.util.* • ประกอบด้วยคลาสที่สำคัญ ๆ ดังนี้ • Enumeration /เก็บกลุ่มข้อมูล เรียกมาดูทีละตัว • Vector / เก็บกลุ่มข้อมูลที่ไม่ทราบล่วงหน้าว่ามีจำนวนเท่าใด • Stack / ใช้เป็น polymorphism คือ สมาชิกใน stack อาจเป็น instance ของคลาสใดก็ได้ • BitSet / สร้าง bit vector คือ vector ขนาดไม่จำกัด แต่สมาชิกเป็น 1 bit เก็บค่า boolean • Dictionary / สำหรับสร้างคลาสเพื่อเป็นแหล่งเก็บข้อมูลของคู่ลำดับ ในลักษณะ <key, value> • Hashtable /ขยายจาก dictionary โดยใช้ hash code ของ key เป็น index และ value อาจเป็น instance ของคลาสใด ๆ • Properties / ขยายจาก hashtable เพื่อเป็นแหล่งเก็บข้อมูลคู่ลำดับของ string กับ string

  37. StringTokenizer / สำหรับตัด string หนึ่งออกเป็นลำดับของ tokens • Date / เก็บข้อมูลเกี่ยวกับ เวลา และวันที่ • Random / คลาสมีฟังก์ชันสำหรับสร้างลำดับของเลข random • java.util.zip Package • GZIPOutputStream /filter stream สำหรับบีบอัดข้อมูล • GZIPInputStream / filter stream สำหรับขยายข้อมูล • ZipOutputStream /filter stream สำหรับบีบอัดข้อมูลเพื่อส่งออก • output stream ที่นำมาต่อไว้ • ZipInputStream / filter stream สำหรับขยายข้อมูลเพื่อส่งเข้า • input stream ที่นำมาต่อไว้

  38. //mydate.java public class mydate { public static void main(String args[]) { Date d = new Date(); System.out.println(d); System.out.println(d.getTime()); Date d1 = new Date(1000000L); System.out.println(d1); System.out.println(d1.getTime()); System.out.println(d.after(d1)); System.out.println(d.before(d1)); System.out.println(d.equals(d1)); } }

  39. java.io.* • package ที่สนับสนุนการอ่าน-เขียนไฟล์ string sockets • File /มีไว้เพื่อให้ทราบข้อมูลและจัดการไฟล์หรือไดเรกทอรี คลาสนี้ไม่มี method สำหรับสร้างไฟล์ใหม่ขึ้นมา แต่มี renameTo() สำหรับเปลี่ยนชื่อไฟล์ และ delete() สำหรับลบไฟล์

  40. //myfile.java import java.io.*; class myfile { public static void main(String args[]) throws IOException { File f =new File("myfile.java"); System.out.println("File name :" +f.getName()); System.out.println("Path :" +f.getPath()); System.out.println("Parent:" +f.getParent()); System.out.println("AbsolutePath :" +f.getAbsolutePath()); System.out.println("isAbsolute :" +f.isAbsolute()); System.out.println("exists :" +f.exists()); System.out.println("isFile :" +f.isFile()); System.out.println("isDirectory :"+f.isDirectory()); System.out.println("Writeable :" +f.canWrite()); System.out.println("Readable :" +f.canRead()); System.out.println("Length :" +f.length()); } }

  41. RandomAccessFile //myRandomfile.java import java.io.*; class myRandomFile { public static void main(String args[]) throws IOException { RandomAccessFile f = new RandomAccessFile("myRandomFile.java","r"); f.seek(15); System.out.println(f.readLine()); f.seek(3); System.out.println(f.readLine()); } }

  42. FileInputStream / FileOutputStream สำหรับอ่านและส่ง byte stream ออกไปที่ไฟล์ ซึ่งถ้ามีอยู่แล้วจะเขียนทับของเดิม แต่ถ้าไม่มีจะเป็นการสร้างใหม่ โดยใช้ read() & write() method //myfileStream.java import java.io.*; class myfileStream { public static void main(String args[]) throws IOException { int n = 0; byte b[] = new byte[128]; FileInputStream inf =new FileInputStream("myRandomFile.java"); FileOutputStream outf =new FileOutputStream("mytmpFile.java"); while ((n =inf.read(b))!= -1) outf.write(b,0,n); inf.close(); outf.close(); } }

More Related