การออกแบบ Class แบบ Composition                                                                                           ...
ตัวอยางการสราง Class แบบ Composition เพื่อเก็บขอมูลการลงทะเบียนClass Subjectขั้นตอนที่ 1 สราง Class Subject ขึ้นมา  pu...
Class Studentขั้นตอนที่ 1 ...................................................................................................
Class Registerขั้นตอนที่ 1 ..................................................................................................
การทดสอบการทำงานของ Class แบบ Composition1) สราง testComposition.html เอาไวสำหรับ Run2) สราง testComposition.java เอาไว...
!         public void initGui(){  !         !        c = getContentPane();  !         !        c.setLayout( new FlowLayout...
ขั้นตอนที่ 6 ใสการรองรับการกระทำกับ ปุมตางๆ ทั้งหมดที่ปุม Add Student  !     !        if (event.getSource() == addStdB...
Method resetAll  !          public void resetAll() {  !          !        stdCodeText.setText("");  !          !        st...
Upcoming SlideShare
Loading in …5
×

Applet 4 class_composition

701 views
678 views

Published on

slide เสริมวิชา JAVA จากอาจารย์ฝนครับ
Applet 4 class_composition

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
701
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Applet 4 class_composition

  1. 1. การออกแบบ Class แบบ Composition sscการออกแบบ Class แบบ Composition คือ .......................................................................................................................................................................................................................................................................................................................Constructor method) ออกแบบไวเพื่อ ........................................................................................................................“get” method) ) ออกแบบไวเพื่อ ........................................................................................................................“set” method) ) ออกแบบไวเพื่อ ........................................................................................................................Facilities method)) ออกแบบไวเพื่อ ........................................................................................................................ตัวอยางการออกแบบ Class แบบ Composition เพื่อเก็บขอมูลการลงทะเบียนจากรูป สามารถอธิบายไดวา ...............................................................................................................................................................................................................................................................................................................................................Class Subject..............................................................................................................................................................................................................................................................................................................................................................................................................................Class Student..............................................................................................................................................................................................................................................................................................................................................................................................................................Class Register.............................................................................................................................................................................................................................................................................................................................................................................................................................. Page 1 of 8
  2. 2. ตัวอยางการสราง Class แบบ Composition เพื่อเก็บขอมูลการลงทะเบียนClass Subjectขั้นตอนที่ 1 สราง Class Subject ขึ้นมา public class Subject { }ขั้นตอนที่ 2 สราง Attribute ตางๆ ตามที่ไดออกแบบไว ! private String SubjectCode; ! private String SubjectName; ! private int SubjectCredit;ขั้นตอนที่ 3 สราง Constructor method ตางๆ ตามที่ไดออกแบบไว ! public Subject() { ! ! setSubjectCode(""); ! ! setSubjectName(""); ! ! setSubjectCredit(0); ! } ! public Subject(String Code, String Name, int Credit) { ! ! setSubjectCode(Code); ! ! setSubjectName(Name); ! ! setSubjectCredit(Credit); ! }Constructor method 2 method นี้มีไวเพื่อ .........................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 4 สราง “set” method ตางๆ ตามที่ไดออกแบบไว ! public void setSubjectCode(String Code) { ! ! SubjectCode = Code; ! } ! public void setSubjectName(String Name) { ! ! SubjectName = Name; ! } ! public void setSubjectCredit(int Credit) { ! ! SubjectCredit = Credit; ! }“set” method 3 method นี้มีไวเพื่อ .....................................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 5 สราง “get” method ตางๆ ตามที่ไดออกแบบไว ! public String getSubjectCode() { ! ! return(SubjectCode); ! } ! public String getSubjectName() { ! ! return(SubjectName); ! } ! public int getSubjectCredit() { ! ! return(SubjectCredit); ! }“get” method 3 method นี้มีไวเพื่อ ....................................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 6 สราง Facilities method ตางๆ ตามที่ไดออกแบบไว ! public String toString() { ! ! String str = ""; ! ! str = getSubjectCode()+" "+getSubjectName()+" "; ! ! str += getSubjectCredit(); ! ! return(str); ! }Facilities method นี้ มีชื่อวา ........................................ มีไวเพื่อ .........................................................................................................................................................................................................................................................................................ขั้นตอนที่ 7 ทำการ Compileโดยใชคำสั่ง ..........................................................................................................................................................................ผลลัพธที่ได .......................................................................................................................................................................... Page 2 of 8
  3. 3. Class Studentขั้นตอนที่ 1 ................................................................................................................. public class Student { }ขั้นตอนที่ 2 ................................................................................................................. ! String StudentCode; ! String StudentName; ! String StudentSurName;ขั้นตอนที่ 3 ................................................................................................................. ! public Student() { ! ! setStudentCode(""); ! ! setStudentName(""); ! ! setStudentSurName(""); ! } ! public Student(String Code, String Name, String SurName) { ! ! setStudentCode(Code); ! ! setStudentName(Name); ! ! setStudentSurName(SurName); ! }Constructor method 2 method นี้มีไวเพื่อ ........................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 4 ................................................................................................................. ! public void setStudentCode(String Code) { ! ! StudentCode = Code; ! } ! public void setStudentName(String Name) { ! ! StudentName = Name; ! } ! public void setStudentSurName(String SurName) { ! ! StudentSurName = SurName; ! }“set” method 3 method นี้มีไวเพื่อ .....................................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 5 ................................................................................................................. ! public String getStudentCode() { ! ! return(StudentCode); ! } ! public String getStudentName() { ! ! return(StudentName); ! } ! public String getStudentSurName() { ! ! return(StudentSurName); ! }“get” method 3 method นี้มีไวเพื่อ ....................................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 6 ................................................................................................................. ! public String toString() { ! ! String str = ""; ! ! str = getStudentCode()+" "+getStudentName()+" "; ! ! str += getStudentSurName(); ! ! return(str); ! }Facilities method นี้ มีชื่อวา ........................................ มีไวเพื่อ .........................................................................................................................................................................................................................................................................................ขั้นตอนที่ 7 ทำการ Compileโดยใชคำสั่ง ..........................................................................................................................................................................ผลลัพธที่ได .......................................................................................................................................................................... Page 3 of 8
  4. 4. Class Registerขั้นตอนที่ 1 ................................................................................................................. public class Register { }ขั้นตอนที่ 2 ................................................................................................................. ! private Student std; ! private Subject sub[]; ! private int max, count = -1;ขั้นตอนที่ 3 ................................................................................................................. ! public Register() { ! ! std = new Student(); ! ! max = 0; ! } ! public Register(Student std, int n) { ! ! this.std = std; ! ! max = n; ! ! createSubject(max); ! }Constructor method 2 method นี้มีไวเพื่อ ........................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 4 ................................................................................................................. ! public void setStudent(Student std) { ! ! this.std = std; ! } ! public void setSubject(Subject sub) { ! ! this.sub[++count] = sub; ! } ! public void setSubject(Subject sub, int n) { ! ! this.sub[n] = sub; ! }“set” method 3 method นี้มีไวเพื่อ .....................................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 5 ................................................................................................................. ! public String getStudent() { ! ! return(std.toString()); ! } ! public String getSubject(int n) { ! ! return(sub[n].toString()); ! }“get” method 3 method นี้มีไวเพื่อ ....................................................................................................................................................................................................................................................................................................................................ขั้นตอนที่ 6 ................................................................................................................. ! private void createSubject(int n) { ! ! sub = new Subject[n]; ! }Facilities method นี้ มีชื่อวา ........................................ มีไวเพื่อ .........................................................................................................................................................................................................................................................................................ขั้นตอนที่ 7 ทำการ Compileโดยใชคำสั่ง ..........................................................................................................................................................................ผลลัพธที่ได .......................................................................................................................................................................... Page 4 of 8
  5. 5. การทดสอบการทำงานของ Class แบบ Composition1) สราง testComposition.html เอาไวสำหรับ Run2) สราง testComposition.java เอาไวทดสอบตัวอยางหนาจอการทำงานtestComposition.html <html> ! <body> <h1>Test Composition: Registration Program</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testComposition.class" height="250" width="750"> ! ! </applet> ! </body> </html>testComposition.javaขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import javax.swing.*; import java.awt.event.*; public class testComposition extends JApplet implements ActionListener{ ! public void init(){ ! } ! public void actionPerformed(ActionEvent event){ ! } }ทำไมตอง extends JApplet ..................................................................................................................................................ทำไมตอง implements ActionListener ..................................................................................................................................method init มีไวเพื่อ .............................................................................................................................................................method actionPerformed มีไวเพื่อ .......................................................................................................................................ขั้นตอนที่ 2 สรางหนาจอการทำงาน ! Container c; ! ! JLabel stdCodeLabel, stdNameLabel, stdSurnameLabel; ! JTextField stdCodeText, stdNameText, stdSurnameText, stdText; ! JButton addStdBtn; ! JLabel subCodeLabel, subNameLabel, subCreditLabel; ! JTextField subCodeText, subNameText, subCreditText; ! JButton addSubBtn; ! JTextArea subTextArea; ! JScrollPane subScroll; ! ! JButton saveBtn, cancleBtn; ! public void init(){ ! ! initGui(); ! } Page 5 of 8
  6. 6. ! public void initGui(){ ! ! c = getContentPane(); ! ! c.setLayout( new FlowLayout()); ! ! ! ! stdCodeLabel = new JLabel("Enter Student Code:"); ! ! c.add(stdCodeLabel); ! ! stdCodeText = new JTextField(6); ! ! c.add(stdCodeText); ! ! stdNameLabel = new JLabel("Name:"); ! ! c.add(stdNameLabel); ! ! stdNameText = new JTextField(10); ! ! c.add(stdNameText);! ! ! ! stdSurnameLabel = new JLabel("Surname:"); ! ! c.add(stdSurnameLabel); ! ! stdSurnameText = new JTextField(10); ! ! c.add(stdSurnameText);! ! ! addStdBtn = new JButton("Add Student"); ! ! addStdBtn.addActionListener(this); ! ! c.add(addStdBtn); ! ! ! ! ! ! stdText = new JTextField(60); stdText.setEditable(false); Note :: ..................................................................... ! ! c.add(stdText);! ! ! ! ! subCodeLabel = new JLabel("Enter Subject Code:"); ! ! c.add(subCodeLabel); ! ! subCodeText = new JTextField(6); ! ! c.add(subCodeText);! ! ! ! ! ! subNameLabel = new JLabel("Name:"); ! ! c.add(subNameLabel); ! ! subNameText = new JTextField(17); ! ! c.add(subNameText);! ! ! ! ! ! subCreditLabel = new JLabel("Credit:"); ! ! c.add(subCreditLabel); ! ! subCreditText = new JTextField(5); ! ! c.add(subCreditText);! ! ! ! ! addSubBtn = new JButton("Add Subject"); ! ! addSubBtn.setEnabled(false); ! ! addSubBtn.addActionListener(this); ! ! c.add(addSubBtn); ! ! ! ! subTextArea = new JTextArea(5,60); ! ! subTextArea.setEditable(false); ! ! subScroll = new JScrollPane(subTextArea); ! ! c.add(subScroll);! ! ! ! ! ! saveBtn = new JButton(" Save "); ! ! saveBtn.setEnabled(false); ! ! saveBtn.addActionListener(this); ! ! c.add(saveBtn); ! ! ! ! cancleBtn = new JButton(" Cancle "); ! ! cancleBtn.setEnabled(false); ! ! cancleBtn.addActionListener(this); ! ! c.add(cancleBtn);! ! ! }ขั้นตอนที่ 3 ทดลอง Compile และ Runวิธีในการ Run คือ .................................................................................................................................................................ขั้นตอนที่ 4 สราง Object Register เพื่อทดสอบการทำงาน ! Register reg; Note :: ................................................................ ! int count = 0; ............................................................................... ! ! public void init(){ ! initGui(); ............................................................................... ! ! ! } reg = new Register(new Student(), 5); ............................................................................... ...............................................................................ขั้นตอนที่ 5 ทดลอง Compile และ Run อีกครั้ง ............................................................................... Page 6 of 8
  7. 7. ขั้นตอนที่ 6 ใสการรองรับการกระทำกับ ปุมตางๆ ทั้งหมดที่ปุม Add Student ! ! if (event.getSource() == addStdBtn) { ! ! ! Student std = new Student(stdCodeText.getText(), stdNameText.getText(), stdSurnameText.getText()); ! ! ! stdText.setText(std.toString()); ! ! ! reg.setStudent(std); ! ! ! addStdBtn.setEnabled(false); ! ! ! addSubBtn.setEnabled(true); ! ! ! saveBtn.setEnabled(true); ! ! ! cancleBtn.setEnabled(true); ! ! ! stdCodeText.setText(""); ! ! ! stdNameText.setText(""); ! ! ! stdSurnameText.setText("");! ! ! ! ! ! }Note ::.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................ที่ปุม Add Subject ! ! else if (event.getSource() == addSubBtn) { ! ! ! Subject sub = new Subject(subCodeText.getText(), subNameText.getText(), Integer.parseInt(subCreditText.getText())); ! ! ! subTextArea.append(sub.toString()+"n"); ! ! ! reg.setSubject(sub, count); ! ! ! count++; ! ! ! if (count == 5 ){ ! ! ! ! addSubBtn.setEnabled(false); ! ! ! } ! ! ! subCodeText.setText(""); ! ! ! subNameText.setText(""); ! ! ! subCreditText.setText(""); ! ! }Note ::.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................ที่ปุม Save ! ! else if (event.getSource() == saveBtn) { ! ! ! String output=""; ! ! ! output = "Student :" + reg.getStudent(); ! ! ! output += "nSubject:n"; ! ! ! for(int n = 0 ; n < count; n++) ! ! ! ! output += reg.getSubject(n) + "n"; ! ! ! JOptionPane.showMessageDialog(null, output,"Registration Data",JOptionPane.INFORMATION_MESSAGE); ! ! ! resetAll(); ! ! }Note ::.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................ที่ปุม Cancle ! ! else if (event.getSource() == cancleBtn) { ! ! ! resetAll(); ! ! }Note ::............................................................................................................................................................................................. Page 7 of 8
  8. 8. Method resetAll ! public void resetAll() { ! ! stdCodeText.setText(""); ! ! stdNameText.setText(""); ! ! stdSurnameText.setText(""); ! ! stdText.setText(""); ! ! addStdBtn.setEnabled(true); ! ! ! ! subCodeText.setText(""); ! ! subNameText.setText(""); ! ! subCreditText.setText(""); ! ! addSubBtn.setEnabled(false); ! ! subTextArea.setText(""); ! ! ! ! saveBtn.setEnabled(false); ! ! cancleBtn.setEnabled(false); ! ! ! ! count = 0; ! }Note ::..........................................................................................................................................................................................................................................................................................................................................................................................การบาน1) ตัวแปร count ที่อยูใน Class testComposition มีไวเพื่ออะไร2) ใหยกเลิกการใชงานตัวแปร count ที่ Class testComposition และใหไปใชตัวแปร count ใน Class Register แทน3) ใหเพิ่มการทำงานของโปรแกรมโดยใหสามารถคนหา(Search) แกไข(Edit) และลบ(Delete) Subject ได ตัวอยางหนาจอการทำงาน ตัวอยางการสราง Confirm Dialog int ans = JOptionPane.showConfirmDialog(null, "Delete?","Confirm",JOptionPane.YES_NO_OPTION); if (ans == 0) { //ในกรณีที่ผูใชกดปุม Yes แลว ans จะมีคาเทากับ 0 } Page 8 of 8

×