Page 1 of 3 123 LastLast
Results 1 to 20 of 55
  1. #1
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

    Default 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.


    HAVE STARTED TO DO ON MY OWN. IF ANY MODERATOR SAYS I DIDNT MAKE ANY EFFORT.

    not really sure if it's working as I cant test it out.

    Anyone can help me check it out if the codes are so far so good?

    Java Code:
    import java.util.List;
    import java.util.ArrayList;
    
    public class SetImpl<T> implements Set<T>{
    
        private class Node<T>{
            public T val;
            public Node<T> next;
        }
        
        private Node<T> root = null;
        private Node<T> head = null;
        private Node<T> tail = null;
        
        public void add(T val){
            
            if(member(val) != true){
                if (head == null) {
                    head = tail = new Node < T > ();
                    head.val = val;
                    head.next = tail;
                    tail = head;
                } else {
                    tail.next = new Node < T > ();
                    tail = tail.next;
                    tail.val = val;
                }
            }
        }
    
        // delete element from the list - may be multiple copies.
        public void delete(T val){
            if (head == null) {
                head = head.next;
            }
            Node temp = head;
                while (temp.next != head) {
                    temp = temp.next;
                }
                temp.next = temp.next.next; 
        }
    
    
        // membership test of list
        public boolean member(T val){
            Node tmp = head;
            while(tmp != null) {
                tmp = tmp.next;
                if(tmp.val == val){
                    return true;
                }
            }
            
            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){
            
        }
        
        public static void main(String args[]){
            
        }
    }
    Last edited by JosAH; 07-03-2013 at 03:54 PM. Reason: fixed the [code] ... [/code] tags

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

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

    In the add( ... ) method: why keep a tail pointer for singly linked list; better add a new element to the head of the list; in the delete( ... ) method: if the head is null it will throw a NPE. in te member( ... ) method: you're skipping the first element in the list without testing it.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    Don't really understand the logic. Is this actually set or list or linkedlist? Do you have any example codes to take reference from?

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

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

    Quote Originally Posted by Malv View Post
    Don't really understand the logic. Is this actually set or list or linkedlist? Do you have any example codes to take reference from?
    According to your copied topic title this should be an implementation for a set; according to your code you're using a singly linked list for it; you should design and write the code; it is your assignment after all ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,681
    Rep Power
    5

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

    Quote Originally Posted by Malv View Post
    not really sure if it's working as I cant test it out.
    I would like to know: why not? Testing is the easiest part of the whole deal! You call the code and you see if it works or not. Add System.out.println() statements to your code to print out what it is doing. You can do easy tests by putting String objects in your list so you can easily print those out.

  6. #6
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    I'm still clueless as of whether set or linkedlist codes i should use. if i were to add in public static void main(String args[]) , i do not know which object i should create so that object can use the add,member, delete method from this class.

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,681
    Rep Power
    5

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

    In other words you haven't learned yet how to work with objects. That is pretty vital knowledge, you need to drop everything right now and study up on that first. Otherwise it will be impossible

    a) for you to complete this assignment
    b) anyone to help you; anything told to you, you won't understand

    Sorry, they don't teach you in school that programming is hard.

    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

  8. #8
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    why do I encounter errors when i try to implement codes like this?

    public static void main(String args[]){
    Set<String> s = new Set<String>();
    }

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,681
    Rep Power
    5

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

    Because likely you are trying to create an object out of java.util.Set - but it is an interface, you cannot create an object out of it. Your class implementing that interface is called SetImpl, try using that.

    Java Code:
    Set<String> s = new SetImpl<String>();

    And in the future post the actual error you get; saying "it doesn't work" is really unhelpful.

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

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

    Quote Originally Posted by Malv View Post
    why do I encounter errors when i try to implement codes like this?

    public static void main(String args[]){
    Set<String> s = new Set<String>();
    }
    And now you want us to guess what errors?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    And now you want us to guess what errors?

    Jos
    ok have managed to figure out add and member method on my own with the help of the mods and other members. now im facing problem with the toList method. does it means to take the content of the set and push it to a list?

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

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

    Quote Originally Posted by Malv View Post
    ok have managed to figure out add and member method on my own with the help of the mods and other members. now im facing problem with the toList method. does it means to take the content of the set and push it to a list?
    The method name suggests so ... but we can't read your assignment text ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    The method name suggests so ... but we can't read your assignment text ...

    Jos
    Write the toList method of SetImpl.java. toList returns a List containing the elements of the set. The elements of the list can be in any order but the result of toList must not contain any duplicates.

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

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

    Quote Originally Posted by Malv View Post
    Write the toList method of SetImpl.java. toList returns a List containing the elements of the set. The elements of the list can be in any order but the result of toList must not contain any duplicates.
    List is an interface so any implementation of that interface will do fine. (e.g. an ArrayList or a LinkedList, but if your class also imlements that interface you can simply return that object as well)

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    List is an interface so any implementation of that interface will do fine. (e.g. an ArrayList or a LinkedList, but if your class also imlements that interface you can simply return that object as well)

    Jos
    public List<T> toList(){
    ArrayList<T> res;
    return res;
    }

    this is the skeleton code which they gave me. I know how to create an arraylist but this code seems to me that i will have to fill in a list after the res. but im not sure of the requirement of this method. does anybody has any idea?

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

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

    Uh, excuse me but why are you double posting the same question to this forum? I have been trying to help you
    here--> toList Method of Setlmpl

    I will let the moderators resolve this.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  17. #17
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    i've goggled online regarding converting set to list and i've done this

    Java Code:
    // converts to a list
    public List<T> toList(){
    Set<String> s = new SetImpl<String>();
    ArrayList<T> res = new ArrayList<T>();
    res.addAll(s);
    return res;
    }
    but it looks wrong and it's definitely wrong.
    can somebody correct my mistake?

    and there's this union method. I do understand how union method works. it takes in 2 set and only keep the unique elements in the set. but the skeleton code only takes in a set

    Java Code:
    // does simple set union
        public void union(Set<T> s){
    
        }

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

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

    Stick to that toList() method and don't even mention that union( ... ) method now; you can only addAll( ... ) to a List if the parameter of that method implements the Collection interface; does your SetImpl class implements that interface? If not you have to add every element of your SetImpl object to the ArrayList, one by one; also there is no need to create yet another (empty) SetImpl object for this; simply add the elements of the current SetImpl object (this) to the ArrayList.

    Another remark: don't start multiple threads for the same question/problem; I noticed that the other thread is closed now; stay in this thread for your problem.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Malv is offline Member
    Join Date
    Jun 2013
    Posts
    62
    Rep Power
    0

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

    Quote Originally Posted by JosAH View Post
    Stick to that toList() method and don't even mention that union( ... ) method now; you can only addAll( ... ) to a List if the parameter of that method implements the Collection interface; does your SetImpl class implements that interface? If not you have to add every element of your SetImpl object to the ArrayList, one by one; also there is no need to create yet another (empty) SetImpl object for this; simply add the elements of the current SetImpl object (this) to the ArrayList.

    Another remark: don't start multiple threads for the same question/problem; I noticed that the other thread is closed now; stay in this thread for your problem.

    Jos
    Hi Morning Jos

    I've understood what you say and come up with these codes

    Java Code:
    public List<T> toList(){
            ArrayList<T> res = new ArrayList();
            
            while(root != null){
                res.add((T) root);
            }
            return res;
        }
    Is the algorithm something like that? adding all the value of the set into the arraylist.

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

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

    Yes, it's 'something like that', except for:

    - You want to create an ArrayList<T> instead of a raw Arralist in line 2;
    - Your ArrayList can contain types T, not Node<T> objects; add root.val instead of root in line 5;
    - You're not changing root anywhere in that while-loop, so if it starts it is never going to end.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 07-01-2013, 09:07 AM
  2. 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
  3. Replies: 3
    Last Post: 04-14-2011, 06:59 AM
  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
  •