Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Implement the add and member methods of the SetImpl.java

  1. #1
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Question Implement the add and member methods of the SetImpl.java

    Hi, Everyone. Anyone can help me how to start this question. I am not very clear about it. Very appreciate.

    The question is:
    Implement the add and member methods of the SetImpl.java. Note that it is strongly recommended that you do not allow duplicates during add - that would make other methods more challenging to implement.

    The following is the java coding about SetImpl.java:
    Java Code:
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Iterator;
    
    public class SetImpl<T> implements Set<T>{
        
    
        // container class for linked list nodes
        private class Node<T>{
            public T val;
            public Node<T> next;
        }
    
        private Node<T> root; // empty set to begin with
    
        // no need for constructor
    
    
        // add new element to the set by checking for membership.. if not
        // then add to the front of the list
        public void add(T val){
        }
    
        // delete element from the list - may be multiple copies.
        public void delete(T val){
         
        }
    
    
        // membership test of list
        public boolean member(T val){
           
            return false;
        }
      
        // converts to a list
        public List<T> toList(){
            ArrayList<T> res;
            return res;
        }
            
        // does simple set union   
        public void union(Set<T> s){
            
        }
    
    }
    Anyone can give me some tips about this question?
    Thanks very much!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,377
    Blog Entries
    7
    Rep Power
    20

    Default Re: Implement the add and member methods of the SetImpl.java

    Your add( ... ) method should first check if the element to add is already a member( ... ) of the collection.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Default Re: Implement the add and member methods of the SetImpl.java

    Hi, Jos

    Thanks for your replaying. I am really weak about this. Could you give me some more tips about this question? Really don't know about the member part.

    Very appreciate. Thanks very much!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Implement the add and member methods of the SetImpl.java

    Write another method that compares the "new" object to all the objects previously added to see if it exists. Have that method return true or false. Then call that method from the add method and use the returned value to decide if to add it or not.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,377
    Blog Entries
    7
    Rep Power
    20

    Default Re: Implement the add and member methods of the SetImpl.java

    Note that you already have an (empty) member( ... ) method; you should provide a sensible implementation for it.

    kind regards,

    Jos
    hjxlpp likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  2. Replies: 3
    Last Post: 04-14-2011, 06:59 AM
  3. Using Java To Implement RSA Algorithm
    By Floetic in forum New To Java
    Replies: 3
    Last Post: 03-31-2008, 11:56 PM
  4. How can we implement IPC in java
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 06:38 AM

Posting Permissions

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