Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 55
  1. #21
    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

    ok finally cracked my brain into half and gotten this

    Java Code:
    // converts to a list
        public List<T> toList(){
            ArrayList<T> res = new ArrayList<T>();
            
            if(root != null){ // check if there's a first item
                res.add(root.val); //add the first item into the list 
                while(root.next != null){ // while there's still other items
                    root = root.next; // go to the next node
                    res.add(root.val); // add it in
                }
            }
            
            return res;
        }
    Any idea if it's correct? I think there's minor mistakes.


    I added a check inside

    Java Code:
    public static void main(String args[]){
             Set lst = new SetImpl();
             lst.add("1");
             lst.add("2");
             System.out.println(lst.toList());
        }
    and its output is [2, 1]

    It seems to be in the wrong sequence. May I know which part of the code did i implement wrongly?
    Last edited by Malv; 07-07-2013 at 11:06 AM.

  2. #22
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    Why all that clumsy 'mechanical' testing; have a look at this:

    Java Code:
    for (Node<T> root= head; root != null; root= root.next)
       res.add(root.val);
    W.r.t. you other question: it depends where a node is added to your list: the head or the tail.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #23
    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

    my add method is as stated:

    Java Code:
    public void add(T val){
            Node<T> temp = new Node();
            if(member(val) == false){
                if(root != null){
                    temp.val = val;
                    temp.next = root.next;
                    root.next = temp;
                }else{
                    temp.val = val;
                    temp.next = root;
                    root = temp;
                }
                
            }
        }
    I think that line 6 is weird but i do not know what to replace it.

    Now is printing out [1,3,2]

    I cant really see what im adding to the tail rather than the head
    Last edited by Malv; 07-07-2013 at 01:48 PM.

  4. #24
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    You're just juggling with those 'next' pointers; to add a node to the head of the list (always) you can do this:

    Java Code:
    Node<T> temp= new Node<T>();
    temp.val= val;
    
    temp.next= root;
    root= temp;
    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #25
    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

    how about adding on to the head? as in the list already has 1 value, to add on the second value what do you do? I have the same add a node to head of list just like you but the subsequent adding of nodes is screwing up.

  6. #26
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    Take a piece of paper and draw little blocks for the nodes and little arrows for the 'next' elements in the nodes and see what your algorithm does; it does [i]not/i] add nodes to the front of the list if the list already contains one or more nodes.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    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 do understand that but i do not know which part of my code is affecting this. Will you please help me fix this?

    Java Code:
    public void add(T val){
            Node<T> temp = new Node<T>();
            if(member(val) == false){
                if(root == null){
                    temp.val = val;
                    temp.next = root;
                    root = temp;
                }else{
                    temp.val = val;
                    temp.next = root.next;
                    root.next = temp;
                }
                
            }
        }

  8. #28
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    Quote Originally Posted by Malv View Post
    i do understand that but i do not know which part of my code is affecting this. Will you please help me fix this?
    See my reply #24; I was spoonfeeding you.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #29
    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 already had that code before you spoonfed me fyi. thanks but now im displaying [4,3,2,1]

    could it be the toList method?
    Java Code:
    public List<T> toList(){
            ArrayList<T> res = new ArrayList<T>();
            
                for (Node<T> head= root; root != null; root= root.next){
                    res.add(root.val);
                }  
     
            
            return res;
        }

  10. #30
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    If you're adding 1, 2, 3 and 4 to your SetImpl (in that order), the output is fine (if you're using my algorithm); always show the latest version of your code so we know what you're talking about. Remarks such as 'I already had that code' are 'deus ex machina' at best and don't help us to help you.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #31
    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

    ok toList method is still not working. do not know what requirement i neednt fulfilled but its okay

    next on , union method

    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){
     
        }
    how do i have to starT?

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

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

    Quote Originally Posted by Malv View Post
    ok toList method is still not working. do not know what requirement i neednt fulfilled but its okay

    next on , union method
    No, what isn't working with that toList() method?

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #33
    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

    how do I tell you what's wrong when I do not know what's wrong, the system is not taking it as correct answer.

  14. #34
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    Quote Originally Posted by Malv View Post
    how do I tell you what's wrong when I do not know what's wrong, the system is not taking it as correct answer.
    You seem to be able to judge that something is wrong; how? why? were you expecting different output? You got [4,3,2,1] (at least you wrote so); did you put in those numbers in the order 12,3,4? Did you use my implementation of the add( ... ) method? Programming is about giving perfect information; not what you are doing ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #35
    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

    this is my assignment, and its going to due in 2 hours time. and this is only what I have. I have an automark system to mark my prac for me. so i do know if its correct anot. although the system marks my add and member method as correct, but i just realised my member method is not working properly. it only can read the head node. still left with union delete and toList. I do not know what to do anymore.

    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;
    
        public static void main(String args[]){
             Set lst = new SetImpl();
             lst.add("3");
             lst.add("8");
             lst.add("9");
    
             System.out.println(lst.member("8"));
             System.out.println(lst.toList());
    
             
        }
    
        public void add(T val){
            Node<T> temp = new Node<T>();
            if(member(val) == false){
                temp.val = val;
                temp.next = root;
                root = temp;  
            }
        }
    
        // delete element from the list - may be multiple copies.
        public void delete(T val){
            
            if(member(val) == true){
                /*if(root.val == val){
                    root = root.next;
                }*/
                
                    System.out.println("1"+val);
                    root = root.next;
                    if(root.val == val){
                        System.out.println("2");
                        Node<T> temp = root;
                        temp = temp.next;
                        temp.next = temp.next.next;
                        System.out.println("3");
                    }
                    
                
            }
        }
    
        // membership test of list
        public boolean member(T val){
            boolean check = false;
            if(root == null){
                return false;
            }else{
                if(root.val == val){
                    return true;
                }
                
                while(check != false){
                    root = root.next;
                    if(root.val == val){
                        return true;
                    }
                }
    
            }
            return false;
        }
    
        // converts to a list
        public List<T> toList(){
            ArrayList<T> res = new ArrayList<T>();
            
            for (Node<T> head= root; root != null; root= root.next){
                if(!res.contains(root.val)){
                    res.add(root.val);
                }
            }
            return res;
        }
    
        // does simple set union
        public void union(Set<T> s){
           Set<T> uSet3 = new SetImpl<T>();
    	        //s.addAll(s);
    	            
    	         
    
    
        }
    
    
    
    }

  16. #36
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    The member( ... ) method is clumsy and incorrect (you initialized check to false so that while loop never runs); why not simply do this:

    Java Code:
    for (Node<T> t= root; t != null; t= t.next) // walk over the entire list
       if (t.val.equals(val)) return true; // found the element?
    return false; // nothing found
    If your SetImpl doesn't contain duplicates, the List copy won't contain any duplicates either ...

    Jos
    Last edited by JosAH; 07-07-2013 at 04:16 PM.
    cenosillicaphobia: the fear for an empty beer glass

  17. #37
    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

    what about my toList method?

  18. #38
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

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

    Quote Originally Posted by Malv View Post
    what about my toList method?
    As I wrote in my previous reply: there is no need to check if a list already contains an element (your SetImpl object contains unique objects only)

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #39
    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

    ok, understood, do you know how to do union method?

    this is what i have so far
    Java Code:
    // does simple set union
        public void union(Set<T> s){
           Set<T> uSet3 = new SetImpl<T>();
    	        //uSet3.addAll(s);
    	            
    	         
    
    
        }
    i do not know if the addAll is in union method or what. I only know 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

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

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

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

    If your SetImpl<T> class wants to implement the Set<T> interface, it should implement all of the methods defined in that interfacce (your class doesn't). That method doesn't return anything so I assume that 'this' (the current SetImpl<T> object should represent the union of 'this' and that other set (the parameter); all you have to do is add each element of that other Set<T> to 'this', i.e. the current SetImpl<T> object. That still doesn't make your SetImpl<T> object a Set<T> though ...

    The union( ... ) method is just a couple of lines:

    Java Code:
    for (T val : s)
       add(val);
    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 3 FirstFirst 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
  •