SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
บทที่ 9 การออกแบบ Class แบบ Composition และใช้ งาน Garbage Collection

       การออกแบบและสร้าง Class แบบ         Composition   เพื่อเก็บข้อมูลการลงทะเบียน โดยมีการออกแบบ
Class ตาม Class Diagram ดงน้ ี
                         ั
                                     Subject
               - SubjectCode : String
               - SubjectName : String
               - SubjectCredit : int
               + Subject()
               + Subject(String Code, String Name, int Credit)
               + setSubjectCode(String Code) : void
               + setSubjectName(String Name) : void
               + setSubjectCredit(int Credit): void
               + getSubjectCode() : String
               + getSubjectName() : String
               + getSubjectCredit() : int
               + toString() : String


                                    Student
           -   StudentCode : String
           -   StudentName : String
           -   StudentSurName : String
           +   Student()
           +   Student(String Code, String Name, String SurName)
           +   setStudentCode(String Code) : void
           +   setStudentName(String Name) : void
           +   setStudentSurName(String SurName): void
           +   getStudentCode() : String
           +   getStudentName() : String
           +   getStudentSurName() : String
           +   toString() : String


                                             Register
           -   std : Student
           -   sub[] : Subject
           -   max : int
           -   count : int = -1
           +   Register()
           +   Register(Student std , int n)
           -   createSubject(int n) : void
           +   setStudent(Student std) : void
           +   setSubject(Subject sub) : void
           +   setStudentSurName(String SurName): void
           +   setSubject(Subject sub, int n) : void
           +   getStudent() : String
           +   getSubject(int n) : String



662305 Information Technology Laboratory II- Chapter 9                              หน ้า 1 จาก 13
Subject


                            Register


                                                         Student



การทดลองที่ 9-1
// File Name : Subject.java
public class Subject {
   private String SubjectCode;
   private String SubjectName;
   private int SubjectCredit;

    /** Creates a new instance of Subject */
    public Subject() {
       setSubjectCode("");
       setSubjectName("");
       setSubjectCredit(0);
    }

    public Subject(String Code, String Name, int Credit) {
       setSubjectCode(Code);
       setSubjectName(Name);
       setSubjectCredit(Credit);
    }

    public void setSubjectCode(String Code) {
       SubjectCode = Code;
    }

    public void setSubjectName(String Name) {
       SubjectName = Name;
    }

    public void setSubjectCredit(int Credit) {
       SubjectCredit = Credit;
    }

    public String getSubjectCode() {
       return (SubjectCode);
    }

    public String getSubjectName() {
       return(SubjectName);
    }

    public int getSubjectCredit() {
       return(SubjectCredit);
    }
662305 Information Technology Laboratory II- Chapter 9             หน ้า 2 จาก 13
public String toString() {
       String str = "";
       str = getSubjectCode()+" "+getSubjectName()+" ";
       str += getSubjectCredit();
       return(str);
    }
}


// File Name : Student.java

public class Student {
   String StudentCode;
   String StudentName;
   String StudentSurName;

    /** Creates a new instance of Student */
    public Student() {
       setStudentCode("");
       setStudentName("");
       setStudentSurName("");
    }

    public Student(String Code, String Name, String SurName) {
       setStudentCode(Code);
       setStudentName(Name);
       setStudentSurName(SurName);
    }

    public void setStudentCode(String Code) {
       StudentCode = Code;
    }

    public void setStudentName(String Name) {
       StudentName = Name;
    }

    public void setStudentSurName(String SurName) {
       StudentSurName = SurName;
    }

    public String getStudentCode() {
       return(StudentCode);
    }

    public String getStudentName() {
       return(StudentName);
    }

    public String getStudentSurName() {
       return(StudentSurName);
    }


662305 Information Technology Laboratory II- Chapter 9    หน ้า 3 จาก 13
public String toString() {
       String str = "";
       str = getStudentCode()+" "+getStudentName()+" ";
       str += getStudentSurName();
       return(str);
    }
}


// File Name : Register.java

public class Register {
   private Student std;
   private Subject sub[];
   private int max, count = -1;

    /** Creates a new instance of Register */
    public Register() {
       std = new Student();
       max = 0;
    }

    public Register(Student std, int n) {
       this.std = std;
       max = n;
       createSubject(max);
    }

    private void createSubject(int n) {
       sub = new Subject[n];
    }

    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;
    }

    public String getStudent() {
       return(std.toString());
    }

    public String getSubject(int n) {
       return(sub[n].toString());
    }
}



662305 Information Technology Laboratory II- Chapter 9    หน ้า 4 จาก 13
// File Name : Lab9_1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab9_1 extends JApplet implements ActionListener {
   Student std[];
   Subject sub[];
   String studentStr[], subjectStr[];
   Container container;
   JComboBox stdCombo, subCombo;
   JLabel stdLabel, subLabel;
   JButton addstdBtn, addsubBtn, saveBtn, clearBtn, cancleBtn;
   JTextField stdText;
   JTextArea subTextArea;
   JScrollPane subScroll;
   Register reg;
   int count = 0;

    /** Creates a new instance of Lab9_1 */
    public Lab9_1() {
       initStudent();
       initSubject();
       initGui();
       reg = new Register(new Student(), 5);
    }

    public void initStudent() {
       std = new Student[3];
       studentStr = new String[3];
       std[0] = new Student("5066260010","AAAAA","BBBBBBBBBBB");
       std[1] = new Student("5066260024","DDDDD","GGGGGGGGGG");
       std[2] = new Student("5066260035","HHHHH","KKKKKKKKKKK");
       studentStr[0] = std[0].toString();
       studentStr[1] = std[1].toString();
       studentStr[2] = std[2].toString();
    }

    public void initSubject() {
       sub = new Subject[5];
       subjectStr = new String[5];
       sub[0] = new Subject("662305","IT Laboratory II", 1);
       sub[1] = new Subject("662309","Data Structure", 3);
       sub[2] = new Subject("662310","Database System", 3);
       sub[3] = new Subject("662317","Data Communication", 3);
       sub[4] = new Subject("662327",
                         "Advanced Computer Programming", 3);
       subjectStr[0] = sub[0].toString();
       subjectStr[1] = sub[1].toString();
       subjectStr[2] = sub[2].toString();
       subjectStr[3] = sub[3].toString();
       subjectStr[4] = sub[4].toString();
    }


662305 Information Technology Laboratory II- Chapter 9   หน ้า 5 จาก 13
public void initGui() {
       container = getContentPane();
       container.setLayout(new FlowLayout());

        stdLabel = new JLabel("Select Student : ");
        container.add(stdLabel);
        stdCombo = new JComboBox( studentStr );
        stdCombo.setMaximumRowCount(3);
        container.add(stdCombo);
        addstdBtn = new JButton("Add Student");
        addstdBtn.addActionListener(this);
        container.add(addstdBtn);
        stdText = new JTextField(40);
        stdText.setEditable(false);
        container.add(stdText);

        subLabel = new JLabel("Select Subject : ");
        container.add(subLabel);
        subCombo = new JComboBox( subjectStr );
        subCombo.setMaximumRowCount(5);
        container.add(subCombo);
        addsubBtn = new JButton("Add Subject");
        addsubBtn.setEnabled(false);
        addsubBtn.addActionListener(this);
        container.add(addsubBtn);
        subTextArea = new JTextArea(5,40);
        subTextArea.setEditable(false);
        subScroll = new JScrollPane(subTextArea);
        container.add(subScroll);

        saveBtn = new JButton(" Save ");
        saveBtn.setEnabled(false);
        saveBtn.addActionListener(this);
        container.add(saveBtn);

        cancleBtn = new JButton(" Cancle ");
        cancleBtn.setEnabled(false);
        cancleBtn.addActionListener(this);
        container.add(cancleBtn);
    }

    public void actionPerformed(ActionEvent event) {
       if (event.getSource() == addstdBtn) {
          int n = stdCombo.getSelectedIndex();
          stdText.setText(std[n].toString());
          reg.setStudent(std[n]);
          addstdBtn.setEnabled(false);
          addsubBtn.setEnabled(true);
          saveBtn.setEnabled(true);
          cancleBtn.setEnabled(true);
       }
       else if (event.getSource() == addsubBtn) {
          int n = subCombo.getSelectedIndex();
          subTextArea.append(sub[n].toString()+"n");

662305 Information Technology Laboratory II- Chapter 9   หน ้า 6 จาก 13
reg.setSubject(sub[n], count);
             count++;
             if (count == 5 ) addsubBtn.setEnabled(false);
          }
          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(this, output,
                "Registration Data",JOptionPane.INFORMATION_MESSAGE);
             resetBtn();
          }
          else if (event.getSource() == cancleBtn) {
             stdText.setText("");
             subTextArea.setText("");
             count = 0;
             resetBtn();
          }
    }

    public void resetBtn() {
       addstdBtn.setEnabled(true);
       addsubBtn.setEnabled(false);
       saveBtn.setEnabled(false);
       cancleBtn.setEnabled(false);
       stdText.setText("");
       subTextArea.setText("");
    }

    public void init( ) {
         Lab9_1 lab9_1 = new Lab9_1();
    }
}
ผลลัพธ์




662305 Information Technology Laboratory II- Chapter 9       หน ้า 7 จาก 13
การทดลองที่ 9-2
                                   SubjectNew
           -   SubjectCode : String
           -   SubjectName : String
           -   SubjectCredit : int
           -   static count : int
           +   SubjectNew()
           +   SubjectNew(String Code, String Name, int Credit)
           +   SubjectNew( SubjectNew sub)
           +   setSubjectCode(String Code) : void
           +   setSubjectName(String Name) : void
           +   setSubjectCredit(int Credit): void
           +   getSubjectCode() : String
           +   getSubjectName() : String
           +   getSubjectCredit() : int
           #   finalize() : void
           +   static getCount() : int
           +   toString() : String




// File Name : SubjectNew.java
public class SubjectNew {
    private String SubjectCode;
    private String SubjectName;
    private int SubjectCredit;
    private static int count = 0;

      /** Creates a new instance of Subject */
      public SubjectNew() {
          setSubjectCode("");
          setSubjectName("");
          setSubjectCredit(0);
          count++;
      }


662305 Information Technology Laboratory II- Chapter 9        หน ้า 8 จาก 13
public SubjectNew(String Code, String Name, int Credit) {
          setSubjectCode(Code);
          setSubjectName(Name);
          setSubjectCredit(Credit);
          count++;
      }

      public SubjectNew( SubjectNew sub) {
          setSubjectCode(sub.getSubjectCode());
          setSubjectName(sub.getSubjectName());
          setSubjectCredit(sub.getSubjectCredit());
          count++;
      }
      public void setSubjectCode(String Code) {
          SubjectCode = Code;
      }

      public void setSubjectName(String Name) {
          SubjectName = Name;
      }

      public void setSubjectCredit(int Credit) {
          SubjectCredit = Credit;
      }

      public String getSubjectCode() {
          return (SubjectCode);
      }

      public String getSubjectName() {
          return(SubjectName);
      }

      public int getSubjectCredit() {
          return(SubjectCredit);
      }

      protected void finalize() {
          count--;
      }

      public static int getCount() {
          return(count);
      }

      public String toString() {
          String str = "";
          str = getSubjectCode()+" "+getSubjectName()+" ";
          str += getSubjectCredit();
          return(str);
      }
}



662305 Information Technology Laboratory II- Chapter 9       หน ้า 9 จาก 13
// File Name : Lab9_2.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab9_2 extends JApplet implements ActionListener {
   SubjectNew sub[];
   Container container;
   JComboBox stdCombo, subCombo;
   JLabel codeLabel, nameLabel,creditLabel;
   JButton addBtn, editBtn, deleteBtn, showBtn, clearBtn;
   JTextField codeText, nameText, creditText, statusText;
   JTextArea subTextArea;
   JScrollPane subScroll;

    /** Creates a new instance of Lab9_2 */
    public Lab9_2() {
       initGui();
       sub = new SubjectNew[10];
       statusText.setText("Number Object : "+ SubjectNew.getCount());
    }

    public Lab9_2(int max) {
       initGui();
       sub = new SubjectNew[max];
       statusText.setText("Number Object : "+ SubjectNew.getCount());
    }

    public void initGui() {
       container = getContentPane();
       container.setLayout(new FlowLayout());

         codeLabel = new JLabel(" Subject Code : ");
         container.add(codeLabel);
         codeText = new JTextField(10);
         container.add(codeText);
         container.add(new JLabel("                                  "));

         nameLabel = new JLabel(" Subject Name : ");
         container.add(nameLabel);
         nameText = new JTextField(20);
         container.add(nameText);

         creditLabel = new JLabel("Subject Credit : ");
         container.add(creditLabel);
         creditText = new JTextField(5);
         container.add(creditText);
         container.add(new JLabel("                                  "));

         addBtn = new JButton("Add");
         addBtn.addActionListener(this);
         container.add(addBtn);

        editBtn = new JButton("Edit");
        editBtn.addActionListener(this);
        container.add(editBtn);
662305 Information Technology Laboratory II- Chapter 9     หน ้า 10 จาก 13
deleteBtn = new JButton("Delete");
        deleteBtn.addActionListener(this);
        container.add(deleteBtn);

        showBtn = new JButton("Show");
        showBtn.addActionListener(this);
        container.add(showBtn);

        clearBtn = new JButton("Clear");
        clearBtn.addActionListener(this);
        container.add(clearBtn);
        subTextArea = new JTextArea(8,25);
        subTextArea.setEditable(false);
        subScroll = new JScrollPane(subTextArea);
        container.add(subScroll);

        statusText = new JTextField(30);
        statusText.setEnabled(false);
        container.add(statusText);
    }

    public void actionPerformed(ActionEvent event) {
       if (event.getSource() == addBtn) {
          if (SubjectNew.getCount() == sub.length)
          {
             JOptionPane.showMessageDialog( this,
                 "Array full , can not add",
                          "Message", JOptionPane.INFORMATION_MESSAGE);
             return;
          }
          int pos = CheckArrayEmpty();
          int n = Integer.parseInt(creditText.getText());
          sub[ pos ] = new SubjectNew( codeText.getText(),
                                  nameText.getText(), n);
          subTextArea.setText( readString( sub ) );
          JOptionPane.showMessageDialog(this, "Add Subject already",
                          "Message", JOptionPane.INFORMATION_MESSAGE);
          clearTextField();
       }
       else if (event.getSource() == editBtn) {
          String s = codeText.getText();
          int n = searchSubject( sub, s);
          if (n >= 0 ) {
             sub[n].setSubjectName(nameText.getText());
             sub[n].setSubjectCredit( Integer.parseInt(
                           creditText.getText() ) );
             subTextArea.setText( readString(sub) );
             JOptionPane.showMessageDialog(this,
                    "Edit Subject already",
                   "Message", JOptionPane.INFORMATION_MESSAGE);
             clearTextField();
          }
          else {
             JOptionPane.showMessageDialog(this,
                    "can not found subject code",
                    "Error Message", JOptionPane.ERROR_MESSAGE);
662305 Information Technology Laboratory II- Chapter 9      หน ้า 11 จาก 13
}
        }
        else if (event.getSource() == deleteBtn) {
           String s = codeText.getText();
           int n = searchSubject(sub, s);
           if (n >= 0 ) {
              nameText.setText( sub[n].getSubjectName() );
              creditText.setText( sub[n].getSubjectCredit() + "" );
              int ans = JOptionPane.showConfirmDialog(this,
                   "Delete subject ",
                    "Confirm", JOptionPane.YES_NO_OPTION);
              // 0 - Yes, 1 - No
              if (ans == 0) {
                  sub[n] = null;
                  System.gc();
                  subTextArea.setText( readString(sub) );
              }
              clearTextField();
           }
           else {
              JOptionPane.showMessageDialog(this,
                     "can not found subject code",
                      "Error Message", JOptionPane.ERROR_MESSAGE);
           }
        }
        else if (event.getSource() == showBtn) {
           String s = codeText.getText();
           int n = searchSubject(sub, s);
           if (n >= 0 ) {
              nameText.setText( sub[n].getSubjectName() );
              creditText.setText( sub[n].getSubjectCredit() + "" );
           }
           else {
              JOptionPane.showMessageDialog(this,
                  "can not found subject code",
                  "Error Message", JOptionPane.ERROR_MESSAGE);
           }
        }
        else if (event.getSource() == clearBtn) {
           clearTextField();
        }
        statusText.setText("Number Object : "+ SubjectNew.getCount());
    }

    public int CheckArrayEmpty() {
       for(int n = 0 ; n < sub.length ; n++)
          if (sub[n] == null) return( n );
       return( -1 );
    }

    public void clearTextField() {
       codeText.setText("");
       nameText.setText("");
       creditText.setText("");
    }


662305 Information Technology Laboratory II- Chapter 9      หน ้า 12 จาก 13
public int searchSubject(SubjectNew sub[], String s) {
       for(int n= 0 ; n < sub.length ; n++) {
          if ( sub[n] != null )
             if ( s.equals(sub[n].getSubjectCode()) )
                return(n);
       }
       return(-1);
    }

    public String readString(SubjectNew sub[]) {
       String str="";
       for(int n = 0 ; n < sub.length ; n++) {
          if (sub[n] != null)
             str += sub[n].toString() + "n";
       }
       return(str);
    }

    public void init( ) {
         Lab9_2 lab9_2 = new Lab9_2(15);
     }

}
ผลลัพธ์




662305 Information Technology Laboratory II- Chapter 9       หน ้า 13 จาก 13

Contenu connexe

Tendances

Hidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard LibraryHidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard Librarydoughellmann
 
Basic crud operation
Basic crud operationBasic crud operation
Basic crud operationzarigatongy
 
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196Mahmoud Samir Fayed
 
c++ boost and STL
c++  boost and STLc++  boost and STL
c++ boost and STLCOMAQA.BY
 
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181Mahmoud Samir Fayed
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionOUM SAOKOSAL
 
Qtp Imp Script Examples
Qtp Imp Script ExamplesQtp Imp Script Examples
Qtp Imp Script ExamplesUser1test
 
The Ring programming language version 1.6 book - Part 38 of 189
The Ring programming language version 1.6 book - Part 38 of 189The Ring programming language version 1.6 book - Part 38 of 189
The Ring programming language version 1.6 book - Part 38 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31Mahmoud Samir Fayed
 
Zend Framework 1 + Doctrine 2
Zend Framework 1 + Doctrine 2Zend Framework 1 + Doctrine 2
Zend Framework 1 + Doctrine 2Ralph Schindler
 
What makes a good bug report?
What makes a good bug report?What makes a good bug report?
What makes a good bug report?Rahul Premraj
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCOUM SAOKOSAL
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210Mahmoud Samir Fayed
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRick Copeland
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31Mahmoud Samir Fayed
 

Tendances (20)

Hidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard LibraryHidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard Library
 
Basic crud operation
Basic crud operationBasic crud operation
Basic crud operation
 
The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196The Ring programming language version 1.7 book - Part 48 of 196
The Ring programming language version 1.7 book - Part 48 of 196
 
c++ boost and STL
c++  boost and STLc++  boost and STL
c++ boost and STL
 
The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210The Ring programming language version 1.9 book - Part 46 of 210
The Ring programming language version 1.9 book - Part 46 of 210
 
The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212The Ring programming language version 1.10 book - Part 39 of 212
The Ring programming language version 1.10 book - Part 39 of 212
 
The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181The Ring programming language version 1.5.2 book - Part 32 of 181
The Ring programming language version 1.5.2 book - Part 32 of 181
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
 
Qtp Imp Script Examples
Qtp Imp Script ExamplesQtp Imp Script Examples
Qtp Imp Script Examples
 
The Ring programming language version 1.6 book - Part 38 of 189
The Ring programming language version 1.6 book - Part 38 of 189The Ring programming language version 1.6 book - Part 38 of 189
The Ring programming language version 1.6 book - Part 38 of 189
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
 
Qtp best tutorial
Qtp best tutorialQtp best tutorial
Qtp best tutorial
 
Chapter ii(oop)
Chapter ii(oop)Chapter ii(oop)
Chapter ii(oop)
 
Zend Framework 1 + Doctrine 2
Zend Framework 1 + Doctrine 2Zend Framework 1 + Doctrine 2
Zend Framework 1 + Doctrine 2
 
What makes a good bug report?
What makes a good bug report?What makes a good bug report?
What makes a good bug report?
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210The Ring programming language version 1.9 book - Part 53 of 210
The Ring programming language version 1.9 book - Part 53 of 210
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
 
4 gouping object
4 gouping object4 gouping object
4 gouping object
 
The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31The Ring programming language version 1.4.1 book - Part 13 of 31
The Ring programming language version 1.4.1 book - Part 13 of 31
 

En vedette (19)

Lowering the drinking age
Lowering the drinking ageLowering the drinking age
Lowering the drinking age
 
Lowering the drinking age
Lowering the drinking ageLowering the drinking age
Lowering the drinking age
 
Presentacion nomina y pss
Presentacion nomina y pssPresentacion nomina y pss
Presentacion nomina y pss
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
 
El comite sindical 2001
El comite sindical 2001El comite sindical 2001
El comite sindical 2001
 
Control structure
Control structureControl structure
Control structure
 
Things_you_dont_see_every_day
Things_you_dont_see_every_dayThings_you_dont_see_every_day
Things_you_dont_see_every_day
 
Chapter 1 PowerPoint
Chapter 1 PowerPointChapter 1 PowerPoint
Chapter 1 PowerPoint
 
662305 10
662305 10662305 10
662305 10
 
Things_you_dont_see_every_day
Things_you_dont_see_every_dayThings_you_dont_see_every_day
Things_you_dont_see_every_day
 
Instructional tech best_practices
Instructional tech best_practicesInstructional tech best_practices
Instructional tech best_practices
 
Set putty to use numeric keyboard in pico
Set putty to use numeric keyboard in picoSet putty to use numeric keyboard in pico
Set putty to use numeric keyboard in pico
 
Karma 2012
Karma 2012Karma 2012
Karma 2012
 
CENTER OF THE BIBLE
CENTER OF THE BIBLECENTER OF THE BIBLE
CENTER OF THE BIBLE
 
662305 11
662305 11662305 11
662305 11
 
Narendra murkumbi
Narendra murkumbiNarendra murkumbi
Narendra murkumbi
 
Derivatives in India
Derivatives in IndiaDerivatives in India
Derivatives in India
 
Investment banking
Investment bankingInvestment banking
Investment banking
 
Oppression and Management
Oppression and ManagementOppression and Management
Oppression and Management
 

Similaire à 662305 09

Fee managment system
Fee managment systemFee managment system
Fee managment systemfairy9912
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptSaadAsim11
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo....NET Conf UY
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedPawanMM
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfirshadkumar3
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Codemotion
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdfaplolomedicalstoremr
 
Java весна 2013 лекция 2
Java весна 2013 лекция 2Java весна 2013 лекция 2
Java весна 2013 лекция 2Technopark
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.pptinstaface
 
Java programs
Java programsJava programs
Java programsjojeph
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancjiJakub Marchwicki
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기Arawn Park
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyYasuharu Nakano
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 
Create methods to_insert
Create methods to_insertCreate methods to_insert
Create methods to_insertNayanBakhadyo
 
20160616技術會議
20160616技術會議20160616技術會議
20160616技術會議Jason Kuan
 

Similaire à 662305 09 (20)

Fee managment system
Fee managment systemFee managment system
Fee managment system
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
 
Hello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdfHello. Im currently working on the last section to my assignment a.pdf
Hello. Im currently working on the last section to my assignment a.pdf
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
 
@author public class Person{   String sname, .pdf
  @author   public class Person{   String sname, .pdf  @author   public class Person{   String sname, .pdf
@author public class Person{   String sname, .pdf
 
Java весна 2013 лекция 2
Java весна 2013 лекция 2Java весна 2013 лекция 2
Java весна 2013 лекция 2
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Java programs
Java programsJava programs
Java programs
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
JavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovyJavaSE7 Launch Event: Java7xGroovy
JavaSE7 Launch Event: Java7xGroovy
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Create methods to_insert
Create methods to_insertCreate methods to_insert
Create methods to_insert
 
Dotnet 18
Dotnet 18Dotnet 18
Dotnet 18
 
20160616技術會議
20160616技術會議20160616技術會議
20160616技術會議
 

Plus de Nitigan Nakjuatong (20)

วิธีการกำหนดสิทธิให้กับ Directory
วิธีการกำหนดสิทธิให้กับ Directoryวิธีการกำหนดสิทธิให้กับ Directory
วิธีการกำหนดสิทธิให้กับ Directory
 
Applet 7 image_j_panel
Applet 7 image_j_panelApplet 7 image_j_panel
Applet 7 image_j_panel
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
Applet 5 class_inheritance
Applet 5 class_inheritanceApplet 5 class_inheritance
Applet 5 class_inheritance
 
Applet 7 image_j_panel
Applet 7 image_j_panelApplet 7 image_j_panel
Applet 7 image_j_panel
 
Applet 6 mouse_keyboard
Applet 6 mouse_keyboardApplet 6 mouse_keyboard
Applet 6 mouse_keyboard
 
Applet 5 class_inheritance
Applet 5 class_inheritanceApplet 5 class_inheritance
Applet 5 class_inheritance
 
Applet 4 class_composition
Applet 4 class_compositionApplet 4 class_composition
Applet 4 class_composition
 
Applet 3 design_class_composition
Applet 3 design_class_compositionApplet 3 design_class_composition
Applet 3 design_class_composition
 
662305 08
662305 08662305 08
662305 08
 
Applet 2 container and action_listener
Applet 2 container and action_listenerApplet 2 container and action_listener
Applet 2 container and action_listener
 
662305 Lab7new
662305 Lab7new662305 Lab7new
662305 Lab7new
 
New Assingment3 array2D
New Assingment3 array2DNew Assingment3 array2D
New Assingment3 array2D
 
Assingment3 array2 d
Assingment3 array2 dAssingment3 array2 d
Assingment3 array2 d
 
Lab 6 new
Lab 6 newLab 6 new
Lab 6 new
 
Array2D
Array2DArray2D
Array2D
 
Array
ArrayArray
Array
 
Method part2
Method part2Method part2
Method part2
 
Method JAVA
Method JAVAMethod JAVA
Method JAVA
 
Putty basic setting
Putty basic settingPutty basic setting
Putty basic setting
 

Dernier

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 

Dernier (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

662305 09

  • 1. บทที่ 9 การออกแบบ Class แบบ Composition และใช้ งาน Garbage Collection การออกแบบและสร้าง Class แบบ Composition เพื่อเก็บข้อมูลการลงทะเบียน โดยมีการออกแบบ Class ตาม Class Diagram ดงน้ ี ั Subject - SubjectCode : String - SubjectName : String - SubjectCredit : int + Subject() + Subject(String Code, String Name, int Credit) + setSubjectCode(String Code) : void + setSubjectName(String Name) : void + setSubjectCredit(int Credit): void + getSubjectCode() : String + getSubjectName() : String + getSubjectCredit() : int + toString() : String Student - StudentCode : String - StudentName : String - StudentSurName : String + Student() + Student(String Code, String Name, String SurName) + setStudentCode(String Code) : void + setStudentName(String Name) : void + setStudentSurName(String SurName): void + getStudentCode() : String + getStudentName() : String + getStudentSurName() : String + toString() : String Register - std : Student - sub[] : Subject - max : int - count : int = -1 + Register() + Register(Student std , int n) - createSubject(int n) : void + setStudent(Student std) : void + setSubject(Subject sub) : void + setStudentSurName(String SurName): void + setSubject(Subject sub, int n) : void + getStudent() : String + getSubject(int n) : String 662305 Information Technology Laboratory II- Chapter 9 หน ้า 1 จาก 13
  • 2. Subject Register Student การทดลองที่ 9-1 // File Name : Subject.java public class Subject { private String SubjectCode; private String SubjectName; private int SubjectCredit; /** Creates a new instance of Subject */ public Subject() { setSubjectCode(""); setSubjectName(""); setSubjectCredit(0); } public Subject(String Code, String Name, int Credit) { setSubjectCode(Code); setSubjectName(Name); setSubjectCredit(Credit); } public void setSubjectCode(String Code) { SubjectCode = Code; } public void setSubjectName(String Name) { SubjectName = Name; } public void setSubjectCredit(int Credit) { SubjectCredit = Credit; } public String getSubjectCode() { return (SubjectCode); } public String getSubjectName() { return(SubjectName); } public int getSubjectCredit() { return(SubjectCredit); } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 2 จาก 13
  • 3. public String toString() { String str = ""; str = getSubjectCode()+" "+getSubjectName()+" "; str += getSubjectCredit(); return(str); } } // File Name : Student.java public class Student { String StudentCode; String StudentName; String StudentSurName; /** Creates a new instance of Student */ public Student() { setStudentCode(""); setStudentName(""); setStudentSurName(""); } public Student(String Code, String Name, String SurName) { setStudentCode(Code); setStudentName(Name); setStudentSurName(SurName); } public void setStudentCode(String Code) { StudentCode = Code; } public void setStudentName(String Name) { StudentName = Name; } public void setStudentSurName(String SurName) { StudentSurName = SurName; } public String getStudentCode() { return(StudentCode); } public String getStudentName() { return(StudentName); } public String getStudentSurName() { return(StudentSurName); } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 3 จาก 13
  • 4. public String toString() { String str = ""; str = getStudentCode()+" "+getStudentName()+" "; str += getStudentSurName(); return(str); } } // File Name : Register.java public class Register { private Student std; private Subject sub[]; private int max, count = -1; /** Creates a new instance of Register */ public Register() { std = new Student(); max = 0; } public Register(Student std, int n) { this.std = std; max = n; createSubject(max); } private void createSubject(int n) { sub = new Subject[n]; } 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; } public String getStudent() { return(std.toString()); } public String getSubject(int n) { return(sub[n].toString()); } } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 4 จาก 13
  • 5. // File Name : Lab9_1.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab9_1 extends JApplet implements ActionListener { Student std[]; Subject sub[]; String studentStr[], subjectStr[]; Container container; JComboBox stdCombo, subCombo; JLabel stdLabel, subLabel; JButton addstdBtn, addsubBtn, saveBtn, clearBtn, cancleBtn; JTextField stdText; JTextArea subTextArea; JScrollPane subScroll; Register reg; int count = 0; /** Creates a new instance of Lab9_1 */ public Lab9_1() { initStudent(); initSubject(); initGui(); reg = new Register(new Student(), 5); } public void initStudent() { std = new Student[3]; studentStr = new String[3]; std[0] = new Student("5066260010","AAAAA","BBBBBBBBBBB"); std[1] = new Student("5066260024","DDDDD","GGGGGGGGGG"); std[2] = new Student("5066260035","HHHHH","KKKKKKKKKKK"); studentStr[0] = std[0].toString(); studentStr[1] = std[1].toString(); studentStr[2] = std[2].toString(); } public void initSubject() { sub = new Subject[5]; subjectStr = new String[5]; sub[0] = new Subject("662305","IT Laboratory II", 1); sub[1] = new Subject("662309","Data Structure", 3); sub[2] = new Subject("662310","Database System", 3); sub[3] = new Subject("662317","Data Communication", 3); sub[4] = new Subject("662327", "Advanced Computer Programming", 3); subjectStr[0] = sub[0].toString(); subjectStr[1] = sub[1].toString(); subjectStr[2] = sub[2].toString(); subjectStr[3] = sub[3].toString(); subjectStr[4] = sub[4].toString(); } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 5 จาก 13
  • 6. public void initGui() { container = getContentPane(); container.setLayout(new FlowLayout()); stdLabel = new JLabel("Select Student : "); container.add(stdLabel); stdCombo = new JComboBox( studentStr ); stdCombo.setMaximumRowCount(3); container.add(stdCombo); addstdBtn = new JButton("Add Student"); addstdBtn.addActionListener(this); container.add(addstdBtn); stdText = new JTextField(40); stdText.setEditable(false); container.add(stdText); subLabel = new JLabel("Select Subject : "); container.add(subLabel); subCombo = new JComboBox( subjectStr ); subCombo.setMaximumRowCount(5); container.add(subCombo); addsubBtn = new JButton("Add Subject"); addsubBtn.setEnabled(false); addsubBtn.addActionListener(this); container.add(addsubBtn); subTextArea = new JTextArea(5,40); subTextArea.setEditable(false); subScroll = new JScrollPane(subTextArea); container.add(subScroll); saveBtn = new JButton(" Save "); saveBtn.setEnabled(false); saveBtn.addActionListener(this); container.add(saveBtn); cancleBtn = new JButton(" Cancle "); cancleBtn.setEnabled(false); cancleBtn.addActionListener(this); container.add(cancleBtn); } public void actionPerformed(ActionEvent event) { if (event.getSource() == addstdBtn) { int n = stdCombo.getSelectedIndex(); stdText.setText(std[n].toString()); reg.setStudent(std[n]); addstdBtn.setEnabled(false); addsubBtn.setEnabled(true); saveBtn.setEnabled(true); cancleBtn.setEnabled(true); } else if (event.getSource() == addsubBtn) { int n = subCombo.getSelectedIndex(); subTextArea.append(sub[n].toString()+"n"); 662305 Information Technology Laboratory II- Chapter 9 หน ้า 6 จาก 13
  • 7. reg.setSubject(sub[n], count); count++; if (count == 5 ) addsubBtn.setEnabled(false); } 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(this, output, "Registration Data",JOptionPane.INFORMATION_MESSAGE); resetBtn(); } else if (event.getSource() == cancleBtn) { stdText.setText(""); subTextArea.setText(""); count = 0; resetBtn(); } } public void resetBtn() { addstdBtn.setEnabled(true); addsubBtn.setEnabled(false); saveBtn.setEnabled(false); cancleBtn.setEnabled(false); stdText.setText(""); subTextArea.setText(""); } public void init( ) { Lab9_1 lab9_1 = new Lab9_1(); } } ผลลัพธ์ 662305 Information Technology Laboratory II- Chapter 9 หน ้า 7 จาก 13
  • 8. การทดลองที่ 9-2 SubjectNew - SubjectCode : String - SubjectName : String - SubjectCredit : int - static count : int + SubjectNew() + SubjectNew(String Code, String Name, int Credit) + SubjectNew( SubjectNew sub) + setSubjectCode(String Code) : void + setSubjectName(String Name) : void + setSubjectCredit(int Credit): void + getSubjectCode() : String + getSubjectName() : String + getSubjectCredit() : int # finalize() : void + static getCount() : int + toString() : String // File Name : SubjectNew.java public class SubjectNew { private String SubjectCode; private String SubjectName; private int SubjectCredit; private static int count = 0; /** Creates a new instance of Subject */ public SubjectNew() { setSubjectCode(""); setSubjectName(""); setSubjectCredit(0); count++; } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 8 จาก 13
  • 9. public SubjectNew(String Code, String Name, int Credit) { setSubjectCode(Code); setSubjectName(Name); setSubjectCredit(Credit); count++; } public SubjectNew( SubjectNew sub) { setSubjectCode(sub.getSubjectCode()); setSubjectName(sub.getSubjectName()); setSubjectCredit(sub.getSubjectCredit()); count++; } public void setSubjectCode(String Code) { SubjectCode = Code; } public void setSubjectName(String Name) { SubjectName = Name; } public void setSubjectCredit(int Credit) { SubjectCredit = Credit; } public String getSubjectCode() { return (SubjectCode); } public String getSubjectName() { return(SubjectName); } public int getSubjectCredit() { return(SubjectCredit); } protected void finalize() { count--; } public static int getCount() { return(count); } public String toString() { String str = ""; str = getSubjectCode()+" "+getSubjectName()+" "; str += getSubjectCredit(); return(str); } } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 9 จาก 13
  • 10. // File Name : Lab9_2.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Lab9_2 extends JApplet implements ActionListener { SubjectNew sub[]; Container container; JComboBox stdCombo, subCombo; JLabel codeLabel, nameLabel,creditLabel; JButton addBtn, editBtn, deleteBtn, showBtn, clearBtn; JTextField codeText, nameText, creditText, statusText; JTextArea subTextArea; JScrollPane subScroll; /** Creates a new instance of Lab9_2 */ public Lab9_2() { initGui(); sub = new SubjectNew[10]; statusText.setText("Number Object : "+ SubjectNew.getCount()); } public Lab9_2(int max) { initGui(); sub = new SubjectNew[max]; statusText.setText("Number Object : "+ SubjectNew.getCount()); } public void initGui() { container = getContentPane(); container.setLayout(new FlowLayout()); codeLabel = new JLabel(" Subject Code : "); container.add(codeLabel); codeText = new JTextField(10); container.add(codeText); container.add(new JLabel(" ")); nameLabel = new JLabel(" Subject Name : "); container.add(nameLabel); nameText = new JTextField(20); container.add(nameText); creditLabel = new JLabel("Subject Credit : "); container.add(creditLabel); creditText = new JTextField(5); container.add(creditText); container.add(new JLabel(" ")); addBtn = new JButton("Add"); addBtn.addActionListener(this); container.add(addBtn); editBtn = new JButton("Edit"); editBtn.addActionListener(this); container.add(editBtn); 662305 Information Technology Laboratory II- Chapter 9 หน ้า 10 จาก 13
  • 11. deleteBtn = new JButton("Delete"); deleteBtn.addActionListener(this); container.add(deleteBtn); showBtn = new JButton("Show"); showBtn.addActionListener(this); container.add(showBtn); clearBtn = new JButton("Clear"); clearBtn.addActionListener(this); container.add(clearBtn); subTextArea = new JTextArea(8,25); subTextArea.setEditable(false); subScroll = new JScrollPane(subTextArea); container.add(subScroll); statusText = new JTextField(30); statusText.setEnabled(false); container.add(statusText); } public void actionPerformed(ActionEvent event) { if (event.getSource() == addBtn) { if (SubjectNew.getCount() == sub.length) { JOptionPane.showMessageDialog( this, "Array full , can not add", "Message", JOptionPane.INFORMATION_MESSAGE); return; } int pos = CheckArrayEmpty(); int n = Integer.parseInt(creditText.getText()); sub[ pos ] = new SubjectNew( codeText.getText(), nameText.getText(), n); subTextArea.setText( readString( sub ) ); JOptionPane.showMessageDialog(this, "Add Subject already", "Message", JOptionPane.INFORMATION_MESSAGE); clearTextField(); } else if (event.getSource() == editBtn) { String s = codeText.getText(); int n = searchSubject( sub, s); if (n >= 0 ) { sub[n].setSubjectName(nameText.getText()); sub[n].setSubjectCredit( Integer.parseInt( creditText.getText() ) ); subTextArea.setText( readString(sub) ); JOptionPane.showMessageDialog(this, "Edit Subject already", "Message", JOptionPane.INFORMATION_MESSAGE); clearTextField(); } else { JOptionPane.showMessageDialog(this, "can not found subject code", "Error Message", JOptionPane.ERROR_MESSAGE); 662305 Information Technology Laboratory II- Chapter 9 หน ้า 11 จาก 13
  • 12. } } else if (event.getSource() == deleteBtn) { String s = codeText.getText(); int n = searchSubject(sub, s); if (n >= 0 ) { nameText.setText( sub[n].getSubjectName() ); creditText.setText( sub[n].getSubjectCredit() + "" ); int ans = JOptionPane.showConfirmDialog(this, "Delete subject ", "Confirm", JOptionPane.YES_NO_OPTION); // 0 - Yes, 1 - No if (ans == 0) { sub[n] = null; System.gc(); subTextArea.setText( readString(sub) ); } clearTextField(); } else { JOptionPane.showMessageDialog(this, "can not found subject code", "Error Message", JOptionPane.ERROR_MESSAGE); } } else if (event.getSource() == showBtn) { String s = codeText.getText(); int n = searchSubject(sub, s); if (n >= 0 ) { nameText.setText( sub[n].getSubjectName() ); creditText.setText( sub[n].getSubjectCredit() + "" ); } else { JOptionPane.showMessageDialog(this, "can not found subject code", "Error Message", JOptionPane.ERROR_MESSAGE); } } else if (event.getSource() == clearBtn) { clearTextField(); } statusText.setText("Number Object : "+ SubjectNew.getCount()); } public int CheckArrayEmpty() { for(int n = 0 ; n < sub.length ; n++) if (sub[n] == null) return( n ); return( -1 ); } public void clearTextField() { codeText.setText(""); nameText.setText(""); creditText.setText(""); } 662305 Information Technology Laboratory II- Chapter 9 หน ้า 12 จาก 13
  • 13. public int searchSubject(SubjectNew sub[], String s) { for(int n= 0 ; n < sub.length ; n++) { if ( sub[n] != null ) if ( s.equals(sub[n].getSubjectCode()) ) return(n); } return(-1); } public String readString(SubjectNew sub[]) { String str=""; for(int n = 0 ; n < sub.length ; n++) { if (sub[n] != null) str += sub[n].toString() + "n"; } return(str); } public void init( ) { Lab9_2 lab9_2 = new Lab9_2(15); } } ผลลัพธ์ 662305 Information Technology Laboratory II- Chapter 9 หน ้า 13 จาก 13