» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
רישום תוכנית עבור תוכנית רישום
דף ראשי  מתקדמים  וקטורים, Hashtables ורשימות  תוכנית רשימות לדודמא  רישום תוכנית עבור תוכנית רישום גרסה להדפסה

רישום תוכנית עבור תוכנית רישום

 

להלן רישום מלא של התוכנית.

 

/*File Enum01.java Copyright 1997, R.G.Baldwin
This program illustrates the Enumeration interface.

The program defines a class named MyDataStruct that creates
a simple data structure consisting of an array of String
data.

The class provides a method that returns an object of a 
class that implements the Enumeration interface (an 
enumeration object ).

The two methods of the Enumeration interface class can 
then be invoked on the enumeration object  to iterate 
through the original data structure fetching each data 
element in the structure.

The two methods of the Enumeration interface are:
  public boolean hasMoreElements()
  public Object nextElement()

The first method can be used to determine if there are any
elements in the data structure that haven't already been
fetched.  The second method can be used to fetch the next
element.

The controlling class instantiates an object of type
MyDataStruct, invokes the getEnum() method to get an
enumeration object , and then uses that object to interate
through the data structure fetching and displaying each
of the elements in the structure.

The program was tested using JDK 1.1.3 under Win 95.

Running this program produces the following output:
  
zero
one
two
three
**********************************************************/
import java.util.*;
//=======================================================//

//The following class is used by the MyDataStruct class
// to instantiate an object that implements the Enumeration
// interface.
class MyEnumerator implements Enumeration{
  int count;//store iteration counter here
  int length;//store array length here
  Object[] dataArray;//store reference to data array here
  //-----------------------------------------------------//
  
  //Constructor
  MyEnumerator(int count,int length,Object[] dataArray){
    this.count = count;
    this.length = length;
    this.dataArray = dataArray;
  }//end constructor
  //-----------------------------------------------------//
  
  //This method defines one of the methods that are
  // declared in the Enumeration interface.
  public boolean hasMoreElements(){
    return (count < length);
  }//end hasMoreElements
  //-----------------------------------------------------//
  
  //This method defines the other method that is declared
  // in the Enumeration interface.
  public Object nextElement(){
    return dataArray[count++];
  }//end nextElement
  
}//end class MyEnumerator
//=======================================================//

//This class can be used to instantiate a simple data
// structure object that has the ability to provide an
// enumeration object  to a using program.
class MyDataStruct{
  String[] data;
  //-----------------------------------------------------//
  
  MyDataStruct(){//constructor
    //Hard code the data for illustration purposes only
    data = new String[4];
    data[0] = "zero";
    data[1] = "one";
    data[2] = "two";
    data[3] = "three";
  }//end constructor 
  //-----------------------------------------------------//
  
  //This method will return an enumeration object  to a
  // using program.
  Enumeration getEnum(){
    return new MyEnumerator(0,data.length,data);
  }//end getEnum()
  //-----------------------------------------------------//
}//end class MyDataStruct
//=======================================================//

class Enum01{//controlling class
  public static void main(String[] args){
    //Instantiate an object of type MyDataStruct
    MyDataStruct myDataStruct = new MyDataStruct();
    
    //Get an enumeration object  that describes the object
    // of type MyDataStruct
    Enumeration myEnumeration = myDataStruct.getEnum();
    
    //Use the enumeration object  to iterate and display
    // each element in the object of type MyDataStruct.
    while(myEnumeration.hasMoreElements()){
      System.out.println(myEnumeration.nextElement());
    }//end while loop
  }//end main()
}//end controlling class
//=======================================================//

 

 01-12-03 / 21:50  עודכן ,  13-10-03 / 19:06  נוצר ע"י רונית רייכמן  בתאריך 
 תוכנית רשימות לדודמא - הקודםהבא - מחלקת וקטור + מחלקת Hashtable 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 6