การเขียนโปรแกรมภาษาจาวา ครูวรัญญา วรรณราช
ขอบเขตการบรรยายศึกษาแนวคิดหลกั การเชงิ วตั ถ(ุ Introduction to Object-oriented ) การจาํ ลองสรรพสง่ิ ดว้ ย เทคนิคเชงิ วตั ถุ ความหมายวตั ถ(ุ object)คลาส (class)กระบวนการ(Method)วธิ กี าํ หนดขอ้ มลู ในรูปแบบต่างๆ การใชค้ าํ สงั่ ต่าง ๆ
ขอบเขตการบรรยาย การรบั และแสดงผลขอ้ มลู โครงสรา้ งควบคุมการทาํ งาน ( Control Structures)การตดั สนิ ใจ การวนรอบทาํ งานซาํ้ สตริง (String) อะเรย (Array) การเขยี นโปรแกรมเชงิ วตั ถใุ นการสบื ทอด การนาํ คลาสมาใชง้ าน การใชง้ านไดห้ ลาย รูปแบบ(Polymorphism) การเขยี นโปรแกรมเช่อื มโยงตดิ ต่อฐานขอ้ มลู (MySQL)เบ้อื งตน้
แผนการบรรยายวตั ถปุ ระสงค์ เพอ่ื ใหผ้ ูเ้รยี น มคี วามรู ้ ทกั ษะ เก่ยี วกบั ศึกษาแนวคดิ เกย่ี วกบั หลกั การเชงิ วตั ถ(ุ Introduction to Object-oriented ) การจาํ ลองสรรพสง่ิ ดว้ ยเทคนคิ เชงิ วตั ถุ ความหมายวตั ถ(ุ object) คลาส(class) กระบวนการ(Method) เอนแคปชูเลชนั่ การถา่ ยทอดคุณสมบตั ิ กรรมวธิ โี พลมี อรฟ์ ิซมึ เทคโนโลยจี าวา
วตั ถปุ ระสงค์การเขยี นโปรแกรมเชงิ วตั ถดุ ว้ ยภาษาจาวา (Object Programming with Java)โครงสรา้ งของโปรแกรมจาวา วธิ กี าํ หนดขอ้ มลู ในรูปแบบต่างๆ การใชค้ าํ สงั่ ต่าง ๆ การรบั และแสดงผลขอ้ มลู โครงสรา้ ง ควบคุมการทาํ งาน (Control Structures)
วตั ถปุ ระสงค์การตดั สนิ ใจ การวนรอบทาํ งานซาํ้ การประมวลผลแบบสตรงิ (String)และอะเรย (Array) การเขยี นโปรแกรมเชงิ วตั ถใุ น การสบื ทอด การนาํ คลาสมาใชง้ าน การใชง้ านไดห้ ลายรูปแบบ(Polymorphism) การสรา้ ง packageใชง้ านการเขยี นโปรแกรมเช่อื มโยงตดิ ต่อ ฐานขอ้ มลู (MySQL)เบ้อื งตน้
เอกสารการเรยี น รายชอ่ื หนงั สอื และเอกสารประกอบการบรรยาย ตาํ ราหลกั Computing Concepts with Java Essentials 3rd,Cay Horstmann @John Wiley& Sons,Inc ISBN 0-471- 24371-x
แผนการเรยี น (ต่อ)ตาราประกอบตาราประกอบ1.เขียนโปรแกรม JAVA เบ้ืองตน้ พมิ พค์ รง้ั ท่ี 2ผูแ้ ต่ง/แปล : วรเศรษฐ สวุ รรณิก และคณะ,25502 .เรียนรูโ้ ครงสรา้ งขอ้ มูลและอลั กอรทิ มึ ดว้ ย JAVA (ฉบบั ปรบั ปรุง)ผูแ้ ต่ง/แปล : เนรมติ ร ชมุ สาย ณ อยธุ ยา3.Java GUI using NetBeans วรเศรษฐ สุวรรณิก และคณะ,25514.เอกสารประกอบการสอนรายวชิ า การเขยี นโปรแกรมเชิงวตั ถดุ ว้ ยภาษาจาวา อ.ธวฒั นช์ ยั ศิ ลงั คป์ ระชา 2551
หนงั สอื อ่านนอกเวลา 1.อนรรนงค์ คณุ มณี,คู่มอื การเขยี นโปรแกรมภาษาจาวา ฉบบั ผูเ้ร่มิ ตน้ ,Dev Book 2.พนิดา พานิชกลุ ,การเขยี นโปรแกรมคอมพวิ เตอรเ์ บ้อื งตน้ ดว้ ยภาษาจาวา ,สาํ นกั พมิ พ์ เคทพี ี 3.นิรุธ อาํ นวยศิลป์,Basic Java Programming,ไทยเดฟ. คอม
แนะนาํ Javaความเป็นมาของจาวา, แนวคิดของ \"write once, run anywhere\"คุณสมบตั ิของภาษา JavaการรักษาความปลอดภยัJava กบั World Wide Webข้นั ของการศึกษาจาวา
Object Oriented Technology Concepts
Object การมองเหน็ สง่ิ ตา่ งๆเป็นวตั ถเุ ป็นการมองทเ่ี ป็น ธรรมชาตขิ องมนุษย์ ภาพทเ่ี ราเหน็ จะถกู แยก ออกเป็นวตั ถตุ ่างๆโดยอตั โนมตั ิ ยกตวั อย่าง เช่น เมอ่ื เรามองไปทโ่ี ตะ๊ ทาํ งาน สมอง ของเราจะแยกสว่ นตา่ งๆของภาพโตะ๊ ทาํ งานออกเป็น ปากกา, กระดาษ, แฟ้ม, ล้นิ ชกั , โตะ๊ ฯลฯ
Objectหรอื เมอ่ื เราอยู่ในรา้ นอาหาร เราอาจจะเหน็ บรกิ ร, พนกั งานเก็บสตางค,์ พนกั งานเขน็ รถอาหาร เป็นตน้บรกิ รทาํ หนา้ ทร่ี บั ออเดอรจ์ ากลูกคา้ , พนกั งานเกบ็ สตางคม์ หี นา้ ทเ่ี กบ็ เงนิ และทอนสตางค์
Objectเมอ่ื เรานกึ ถงึ ชอ่ื ของวตั ถหุ รอื อาชพี ของ คน เรามกั จะคดิ ถงึ คณุ สมบตั (ิ Attribute)และ ความสามารถ ของวตั ถุหรือคนเหล่านนั้ ข้ึนมาพรอ้ มๆกนั
Object เช่น เมอ่ื เรานกึ ถงึ ปากกา เรากร็ ูว้ า่ มนั มี สอี ะไรและมนั สามารถใชเ้ขยี นได้
และเมอ่ื เราตอ้ งการทาํ อะไรบางอยา่ ง เราจะหาวตั ถหุ รือคนท่ี สามารถช่วยเราทางานได้ เช่น ถา้ เราตอ้ งการเขยี น เรากจ็ ะหา ปากกาและกระดาษ, หรือถา้ เราตอ้ งการอาหาร เราก็เรียกบริกรเขา้ มารบั ออเดอร์ เราไม่ จาํ เป็นตอ้ งทราบการทาํ งานของปากกาวา่ มนั จ่ายหมกึ ออกมาได้ อย่างไร เหมอื นกบั ท่เี ราไมจ่ าเป็นตอ้ งทราบวา่ เมอ่ื บรกิ รรบั ออเดอรจ์ าก เราไปแลว้ เขาจะไปบอกพอ่ ครวั อย่างไร เราทราบแตว่ า่ เมอ่ื เราตวดั ปลายปากกา หมกึ จะออกมา และเมอ่ื เราสงั่ อาหาร เราก็(มกั )จะ ไดอ้ าหารตามทเ่ี ราสงั่ เสมอ
เหน็ ไดช้ ดั วา่ เราคุน้ เคยกบั การแกป้ ญั หาโดยใชเ้คร่อื งมอื หรือคนเป็นอยา่ งดี เน่อื งจากการแกป้ ญั หาจะทาํ ไดง้ า่ ยข้นึ เมอ่ื ถกู เปลย่ี นใหอ้ ยูใ่ นมมุ มองทค่ี ุน้ เคย ดงั นนั้ ภาษาโปรแกรมท่เี หมาะแก่การแกป้ ญั หาหลายๆปญั หาก็ควรจะมี สนบั สนุนการมองสง่ิ ต่างๆเป็นวตั ถดุ ว้ ยภาษาทส่ี นบั สนุนมมุ มองดงั กลา่ ว เรียกวา่ ภาษาเชิงวตั ถุ (Object-Oriented Programming Languages) ภาษา Java, C++, SmallTalk เป็นภาษาโปรแกรมเชงิ วตั ถุ
Object & Class ObjectObject:Real-world Object-Program
Anatomy of an Object(1) An object is an abstraction and has an identity An object has a name (which may or may not be unique) An object is anything exhibits structure and behavior - Structure Attribute that define the object’s properties Each attribute takes on a single value for a given object - Behavior Operations performed on or by an object Each operation is implemented via a methodObject can be related or linked to other objects
Anatomy of an Object(2)
Object ConceptThings Tangible Things =>Physical Things Intangible Things บทบาท(Tole) (อาจารย์ พนกั งาน ผูป้ ่วย) สง่ิ อบุ ตั ขิ ้นึ (Incident) หรอื Event ต่างๆ เท่ยี วบนิ ฯ การโตต้ อบ(Interaction) (ตงั้ แต่ 2 Object(ผูซ้ ้อื -ขาย))
Object 1.ส่ิงทเ่ี ป็ นรูปธรรม (จบั ต้องได้) Tangible Thingsส่ิงท่จี บั ต้องได้ เช่น สินค้า,ลูกค้า
Intangible Things บทบาท(Tole) (อาจารย์ พนกั งาน ผู้ป่ วย) ส่ิงอุบตั ขิ ึน้ (Incident) หรือ Event ต่างๆ เทยี่ วบนิ ฯ การโต้ตอบ(Interaction) (ต้งั แต่ 2 Object(ผู้ซื้อ-ผ้ขู าย)) 2.ส่ิงท่ีเป็ นนามธรรม(จับ ต้องไม่ได้) เทย่ี วบนิ Object การวง่ิ แสง ความเป็นเจา้ ของ
Object Concepts Object = a “thing” that’s a member of a class Examples … Dwell here a while CLASS vs. OBJECT Objects contain attributes + behaviors Organization Generalization/specialization hierarchies Whole/part hierarchies
Object & Class (Cont.)Person (Person) Person Person John Chan Mary Lee Nelson SzeClass Objects
Object & Class (Cont.) Attribute: An observable property of objects in a class Each object in the class has a value for each attribute The value may or may not be unique for an individual object
Object & Class (Cont.) Person (Person) Person Person John Chan Mary Lee Nelson Sze Name: String Age: int 30 24 60Class with Objectsattributes
Object & Class (Cont.) Operation A function or transformation that may be applied to or by an object in a class; e.g. Debit, Credit All objects in a class share the same operations Each operation has the current object as an implicit argument
Object & Class (Cont.) Method - The implementation of an operation in a class Query - An operation which merely computes a value without modifying any object
Object & Class (Cont.) Person AttributesName OperationsAgeChange JobChange AddressGet AddressGet Job
Class and Object Employee Employee Employeename:String name=‘Smith’ name=‘Sharp’age:Integer age=24 age=52Class Employee Objects instantiated from the class Employee
R eAnporbejescet isnretpinregsenOtedbajsereccttasngles with: Profuesnsdoerrlined names aa++bb==1100 ProfessorClark Class Name Only Professor ClarkProfessorClark : Object Name Only ProfessorClass and Object Name (stay tuned for classes)
What is a Class? A class is a description of a group of objects with common properties (attributes), behavior (operations), relationships, and semantics An object is an instance of a class A class is an abstraction in that it: Emphasizes relevant characteristics Suppresses other characteristics OO Principle: Abstraction
Properties Sample Class Behavior Name Add a student Class Delete a student Location Course Get course rosterDays offered Determine if it is fullCredit hours a + b = 10 Start time End time
Representing Classes A class is represented using a compartmented rectangle a + b = 10Professor Professor Clark
Representing Classes in the UML A class is represented using a rectangle with compartments. Professor Professor J Clark- name- employeeID : UniqueId- hireDate- status- discipline- maxLoad+ submitFinalGrade()+ acceptCourseOffering()+ setMaxLoad()+ takeSabbatical()
The Relationship Between Classes and Objects A class is an abstract definition of anobject It defines the structure and behavior of eachobjeOctbijnectthse class Class It serves as a template for creating objects Objects are grouped into classes ProfessorProfessor Smith Professor Mellon Professor Jones
Basic Principles of Object Technology Object Technology จะกล่าวในรายละเอียดอีกคร้ังเมื่อศึกษาถึงเรื่องตา่ งๆ เหล่าน้ีภายหลงัAbstraction Encapsulation Modularity Hierarchy
About the Java Technology .?
Write on\"ถcนนeทกุ ส,ายมrงุ่ สuู่ โลnกอนิ เตaอรเ์nนตywhere เคร่ืองหลากหลาย ระบบปฏบิ ตั กิ ารตา่ งกนั \"เราต้องการซอฟท์แวร์ที่ .. WeConsistently : แน่นอน ทางานไดเ้ หมือนกนั ในทุกท่ีAnywhere on any platform : รันไดห้ ลายที่ ในหลายระบบปฏิบตั ิการReliability : น่าเช่ือถือSecurity : ปลอดภยัLow Risk: ความเส่ียงต่า ไม่อนั ตรายเลอื กใช้อะไรภาษาอะไรดีC++, JAVA, Small Talk, Object Pascal,VBasic ???choose \"JAVA\".
About the Java Technology .?.Java technology is both a programming language and a platformJava Platforms A platform is the hardware or software environment inwhich a program runs.
A Virtual Machine ภาษาจาวาเป็ นทง้ั Compiler และ InterpreterCompiler : ตอ้ ง Compile ก่อนการใชง้ าน แต่ Compile เป็น ByteCodeInterpreter : การรันโปรแกรมจาวา ByteCode ของจาวาจะถูก Loadเม่ือมีการเรียกใชเ้ ท่าน้นั คาสงั่ ใน ByteCode จะถูกแปลอีกคร้ังในข้นั ตอนการรัน
Byte Code and Java Virtual Machine Existing Development EnvironmentSource Compiler(Pentium) Binary Code Compiler(PowerPC) File%cc Hello.c –o Hello Pentium% Hello Compiler(SPARC) Binary File PowerPC Binary File SPARCRun Binary Code
Bytecodes and the Java Virtual Machine Java Development EnvironmentJava Java Compiler Java JavaCode ByteCode Interpreter (Pentium) (Independent on Pentium Java Compiler Platform) Java (PowerPC) Interpreter%javac Hello.java PowerPC Hello.class created Java Compiler Java Interpreter % java Hello (SPARC) SPARCRun JVM Byte Code
Java is Cross-Platform?
Java is Cross-Platform? Truth: Java programs can compile to machine-independent bytecode Java Java Bytecode Source Code JIT Compiler Compiler or Interpreter (javac) Execution Run TimeJava BytecodeCompile Time Truth: All major operating systems have Java runtime environments Most bundle it (Solaris, MacOS, Windows 2k, OS/2)
Java Platforms สามารถทาํ งานบน OS และ Platform ทต่ี ่างๆ กนั ได้ โดยไมต่ อ้ ง Compile ใหม่ (Write One - Run Anywhere) สามารถรนั บน Web Page สามารถรนั บนอปุ กรณท์ ม่ี ี Java Virtual Machine เช่น โทรศพั ทม์ อื ถอื หนุ่ ยนต์
องค์ประกอบเทคโนโลยจี าวา(Java Technology?) 1. Java Virtual Machine (JVM) 2. Java Runtime Environment (JRE) 3. Java 2 Software Developer Kit (J2SDK)
1. Java Virtual Machine (JVM) เป็นส่วนที่ทาหนา้ ที่เป็นตวั อินเตอร์พรีเตอร์ (interpreter) – เป็นส่วนที่ทาหนา้ ที่เป็นตวั อินเตอร์พรีเตอร์ (interpreter) คือ จะทาการแปลจาวา ไบตโ์ คด้ ใหเ้ ป็นภาษาเคร่ืองเขา้ ใจ – จาวาไบตโ์ คด้ สามารถรันไดห้ ลายแพลตฟอร์ม ถา้ แพลตฟอร์มน้นั มี JVM
2.Java Runtime Environmentเป็น(สJ่วนRที่ใEชใ้ )นการรันโปรแกรม โดยจะทางาน 3 ข้นั ตอน ดงั น้ี1. – โหลดไบตโ์ คด้ โดยใช้ Class loader2. – ตรวจสอบไบตโ์ คด้ โดยใช้ Bytecode Verifier3. – รนั ไบตโ์ คด้ โดยใช้ Runtime Interpreter
Search