Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2015
    Rep Power

    Question How to use the method iterator()?

    Good evening!

    I'm still learning java and tonight I'm stumbling over the way my teacher used the iterator method of the interface Collection.

    Indeed, my teacher defined such a following value method of a class of elements HashConcret:

    Java Code:
    public double value(){
        double sum=0;
        int =1;
        Iterator it =iterator();
        for (i=1,(it.hasNext()),i++){
            sum +=((valuable)it.hasNext()).value();
        return sum/i;
    I don't see any elements being passed as arguments, entering it the iterator... to have their value being moved up to the sum!
    So does it actually work? And if it does, how?

    Class Composant is abstract and defined the characteristics of a named given composant. It is characterized by a name and realize two interfaces Named and Valuable defined below. Two composants are equals if they have same name and same value.

    Class Atome is a composant characterized by a name and a double value. An atome is equal to another atome if they have same value. The name of an atome is not given at creation. it is formed by its parent's name. followed by "_" and its value.

    Abstract class Composite is defined by the characteristics of a complex composant having a collection of named elements of type Atome or Coposite. The class composite forbidden dupplicates. It realizes the interface ComposantManagement. Two objects are equals if they have same name and same elements.

    Class HashConcret is a subclass of Composite which doesn't allow to have duplications and which elements aren't sorted out. The value of an instance of HashConcret is the mean of the value of its elements.

    this description corresponds to the following code: HashConcret is at the bottom!

    Java Code:
    /////////////////////////////       ATOME        ////////////////
    package java_td.td6_composite2;
    public class Atome extends Composant{
        private double value;
        public Atome(Named parent, double value){
            this.value = value;
      public double value(){
            return value;
      public boolean equals(Object object){
        System.out.println("equals Atome");
      return ??????????;                                    C1
      public String toString(){
           return super.toString()+"\n";
    }//Fin Atome
    //////////////////////        COMPOSANT         //////////////
    package java_td.td6_composite2;
    public abstract class Composant implements Named, Valuable, Comparable{
      private String name;
        public Composant(String name){
        public String name(){
            return name;
    /*                 Interface Valuable                                
        public abstract double value();
    /**********           **********************************************/
    /*                Redefined methods                                   /****************************************************************/
        public boolean equals(Object object){
            boolean result=false;
            System.out.println("equals Objet Composant entrée");
            if (???){
            System.out.println("equals Objet Composant cas Named");
            Named temp = (Named) object;
            result= equals(temp);System.out.println("equals res. interm. "+result);
           if (???????){
            System.out.println("equals Objet Composant cas Value");
            result = ????????????????????                       C2
         return result;
        }//end equals(Object object)
        ?????? boolean equals(Named object){
            System.out.println("equals Named Composant");
        return name() == (;
        ?????? boolean equals(Valuable object){
            System.out.println("equals Valuable Composant");
        return ????????????????;
         */ Compares this object to another Object. This function behaves
         * of the <code>compareTo(String)</code> function in taking into account
         * the String represention of the both objects. */
      public int compareTo (Object object) {
           if (object instanceof Valuable){
           return compareTo(((Valuable)object));
        return 1;
        }//end compareTo
        ???????????  int compareTo(Valuable object){
        return        ???????????????????;                      C3
        public int hashCode(){
        return toString().hashCode();
        public String toString(){
        return " name : "+name() +"\t value : "+value();
        }//end toString()
    } // Fin COMPOSANT
    interface Named{
        String name();
    interface Valuable{
        double value();
    ///////////////////////         COMPOSITE       ////////////////////////
    package java_td.td6_composite2;
    import java.util.*;
    public abstract class Composite extends Composant implements ComposantManagement{
        private Set elements;
        double truc=0;
         */Initializes a newly created <code>AbstractComposite</code> object
         * with the name passed as argument.*/
      ????????? Composite( String name, Set elements){              C4
      ????????? void setElements(Set  elements){
        this.elements = elements;
      /*        Interface ComposantManagement                          
      /*                 Element Management                                 ******************************************************************/
      protected Set getElements(){
        return elements;
        */returns true if the evaluation composite object has an element
          that is the same as the value argument.Otherwise returns false./
      public boolean hasElement(Object object) {
        return getElements().contains(object);
      public Iterator iterator(){
            return ??????????????? ;                            C5
      public boolean add(Object value){
        if (value instanceof Named){return getElements().add(value);
        return false;
      }//end add
      public boolean remove(Object element){
        return getElements().remove(element);
        public int size(){
           return getElements().size();
      }//end size()
    public Object get(Object element){
            Iterator it=iterator();
            Object result=null;
    ??????????????????????????                          C6
        return result;
        public String toString(){
        return super.toString()+"  elements: \n"+ getElements()+"\n";
    }// Fin COMPOSITE
    /*        Interface ComposantManagement                          
    /*                 Element Management                            *****************************************************************/
    interface ComposantManagement extends Valuable{
       //retourne l'élément de l'ensemble qui est égaux ‡ l'objet passé en argument
       Object get(Object object);
       //retourne vrai si object est élément de l'ensemble sinon retourne faux
       boolean hasElement(Object object) ;
       //retourne un objet de type Iterator
       Iterator iterator();
       //ajoute un élément dans l'ensemble
       boolean add(Object value);
       double truc=1;
    //////////////////////////      HASHCONCRET       //////////////////
    package java_td.td6_composite2;
    import java.util.*;
        public class HashConcret extends Composite{
            public HashConcret(String name) {
            super(name, new HashSet());
        public double value(){
            *****************                               C8
    I want to understand the method value() of class Composite.

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    MA, USA
    Rep Power

    Default Re: How to use the method iterator()?

    Concerning the last question: Are you asking about Line 42 above? There must have been some sort of copy-paste mistake in your code. You will find this syntax only in an interface (method without method body in {..}).

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: How to use the method iterator()?

    Well, the iterator() method currently has no code in it.
    I am going to assume it is supposed to return the iterator of the Set in Composite.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. cannot find symbol - method iterator()
    By D.Calladine in forum New To Java
    Replies: 1
    Last Post: 11-30-2010, 01:13 PM
  2. How to use linkedlist 'add' method without iterator
    By plexus0208 in forum New To Java
    Replies: 1
    Last Post: 11-25-2010, 09:27 PM
  3. Replies: 0
    Last Post: 05-18-2010, 01:53 AM
  4. iterator method help..
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-08-2009, 04:45 AM
  5. [SOLVED] Writing an Iterator method in a custom list
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 06:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts