Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question How to create an instance of an object of a class ruled by an interface?

    Good evening!

    I'm learning Java and I have some difficulties constructing parts ofaa code:

    I have build an instance c1 of Concret named compListe with s1 and an element instance of Atome valued 14 with the following (and incomplete) code:

    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){
            this.name=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() == (object.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
        super(name);
        setElements(elements);
      }
     
      ????????? 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;
    }
     
    ///////////////////////////       CONCRET        ///////////////
     
    package java_td.td6_composite2;
    import java.util.*;
        public class Concret extends Composite{
        private double coeff;
        private final static double DEFAULT=1;
        public Concret(String name, double coeff) {
        super(name, new TreeSet());
        this.coeff=coeff;
      }
     
      public Concret(String name) {
        this(name, DEFAULT);
      }
     
      public double value(){
        double res=maximum().value();
        return maximum().value()*coeff;
      }
     
      ??????? Valuable maximum(){
        return   ?????????????????                          C7
      }
     
      public String toString(){
        return "\t"+ super.toString()+ "\t coeff: "+coeff+ "\t maximum " +
                                            maximum();
      }
    }// Fin CONCRET

    I tried

    Java Code:
    ComposantManagement c1 = new Concret (compListe")
    c1.add(s1);
    c1.add(new Atome (parent, 14));
    But the actual answer was:

    Java Code:
    ComposantManagement c1 = new Concret (compListe")
    c1.add(s1);
    c1.add(new Atome ((Named)parent, 14));
    Why? Is it a casting case?
    Should it have been:

    Java Code:
    ComposantManagement c1 = new Concret (compListe")
    c1.add(s1);
    c1.add(new Atome (parent.name(), 14));

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to create an instance of an object of a class ruled by an interface?

    Your code has lots of syntax errors. You have unmatched quotes ("), and some of your comments start with */ instead of /*.
    Also, I find your spurious ????????????? scattered throughout the code makes it very hard to read. Please submit a small
    program that demonstrates the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Default Re: How to create an instance of an object of a class ruled by an interface?

    Actually, this not mine but the one of my teacher, who hid part of his code as a bone for thought...

    To simplify, my question is: when we have an interface Named

    Java Code:
    interface Named{
        String name();
    }
    why don't we do:

    Java Code:
    ComposantManagement c1 = new Concret (compListe")
    c1.add(s1);
    c1.add(new Atome (parent, 14));
    or:

    Java Code:
    ComposantManagement c1 = new Concret (compListe")
    c1.add(s1);
    c1.add(new Atome (parent.name(), 14));

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to create an instance of an object of a class ruled by an interface?

    In one case you are passing the parent object (which I presume implements Named). In the other case, you are invoking the method name() which returns
    a String (which is wrong because the constructor expects an object).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to create an instance of an object of a class ruled by an interface?

    A constructor is never part of an interface, so your question is moot; simply design the constructor(s) you need.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 2
    Last Post: 03-07-2015, 06:24 PM
  2. Replies: 1
    Last Post: 07-08-2012, 06:23 PM
  3. Why don't create new object instance ?
    By macylapka in forum New To Java
    Replies: 5
    Last Post: 09-29-2011, 02:47 PM
  4. create an object of interface
    By yma16 in forum New To Java
    Replies: 6
    Last Post: 04-16-2011, 05:28 AM
  5. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 09:13 PM

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
  •