Page 3 of 3 FirstFirst 123
Results 41 to 55 of 55
  1. #41
    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 does
    That still doesn't make your SetImpl<T> object a Set<T> though ...
    means? how do i make my setImpl<T> Object a Set<T> ? do i have to do that? Sorry I'm urgent now. need to hand in soon

  2. #42
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Malv View Post
    what does means? how do i make my setImpl<T> Object a Set<T> ? do i have to do that? Sorry I'm urgent now. need to hand in soon
    That is what you wrote in your first post:

    Java Code:
    public class SetImpl<T> implements Set<T>{
    So if you want your class to implement the Set<T> interface, it should implement all of its methods.

    Jos

    ps. It's Sunday so I'm not in a hurry ...
    cenosillicaphobia: the fear for an empty beer glass

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

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

    First there are a number of things wrong with 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;
    }
    1. You are creating a new, empty Set (which has nothing to do with the current set you want to return as a list). You are adding an empty set.
    2. It wouldn't work anyway because the addAll method of ArrayList assumes a full implementation of Set (which I do not believe you are doing. I believe you need a toArray method in SetImpl).
    3. What makes you think that some type T is a String (it may not be).
    4. I recommend you assign the ArrayList<T> to a List<T> variable (best practice).

    In summary, you have been provided working examples of how to copy a set to a list. I recommend you use them. If the order is reversed, then reverse it again (for expediency and automatic grading purposes).

    Also, why not make a suggestion to the instructor to either state the input order must be maintained, or to have the automatic grading system simply sort the supplied list before comparing it to the test data.


    Edit: Reply issue. This reply is to the wrong message. Actual message is later. Sorry.

    Regards,
    Jim
    Last edited by jim829; 07-07-2013 at 06:40 PM. Reason: Reply problem
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #44
    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 jim829 View Post
    First there are a number of things wrong with 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;
    }
    1. You are creating a new, empty Set (which has nothing to do with the current set you want to return as a list). You are adding an empty set.
    2. It wouldn't work anyway because the addAll method of ArrayList assumes a full implementation of Set (which I do not believe you are doing. I believe you need a toArray method in SetImpl).
    3. What makes you think that some type T is a String (it may not be).
    4. I recommend you assign the ArrayList<T> to a List<T> variable (best practice).

    In summary, you have been provided working examples of how to copy a set to a list. I recommend you use them. If the order is reversed, then reverse it again (for expediency and automatic grading purposes).

    Also, why not make a suggestion to the instructor to either state the input order must be maintained, or to have the automatic grading system simply sort the supplied list before comparing it to the test data.

    Regards,
    Jim

    Hi Jim,

    Thanks for replying. My toList method has been modified to this :

    Java Code:
    // 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;
        }

  5. #45
    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
    That is what you wrote in your first post:

    Java Code:
    public class SetImpl<T> implements Set<T>{
    So if you want your class to implement the Set<T> interface, it should implement all of its methods.

    Jos

    ps. It's Sunday so I'm not in a hurry ...
    yes it should does implements the set class because i got a set class in my directory. i don't get the code snippet you gave me

    Java Code:
    // does simple set union
        public void union(Set<T> s){
           for(T val : s){
               this.add(val);
           }
        }
    for(T val : s)

    my foreach demands an array not a Set<T>

    What should i change it to?

    Sorry to disturb your lovely sunday afternoon

  6. #46
    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

    guys

    do anyone know why m i getting a nullpointer at line 12?

    Java Code:
    // 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;
                } 
                Node<T> temp = root;
                System.out.println(temp);
                    for (Node<T> t= root; t != null; t= t.next){
                        temp = temp.next; 
                    }
                    temp.next = temp.next.next;
            }

  7. #47
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Malv View Post
    Java Code:
    // 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;
        }
    You don't read what I wrote, do you? If the SetImpl<T> object doesn't contain duplicates, there is no need to test for them in that List copy ...

    Jos

    ps. I've been helping you all day now, but if you simply ignore what I wrote I'm going to leave you to others ...
    cenosillicaphobia: the fear for an empty beer glass

  8. #48
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Malv View Post
    yes it should does implements the set class because i got a set class in my directory. i don't get the code snippet you gave me

    Java Code:
    // does simple set union
        public void union(Set<T> s){
           for(T val : s){
               this.add(val);
           }
        }
    for(T val : s)

    my foreach demands an array not a Set<T>

    What should i change it to?
    Don't change it; it is correct.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #49
    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
    You don't read what I wrote, do you? If the SetImpl<T> object doesn't contain duplicates, there is no need to test for them in that List copy ...

    Jos

    ps. I've been helping you all day now, but if you simply ignore what I wrote I'm going to leave you to others ...
    im not ignoring what you say, im just busy doing up delete and union method by myself because time is running out.

  10. #50
    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
    Don't change it; it is correct.

    Jos
    its not. it has an error

    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - for-each not applicable to expression type
    required: array or java.lang.Iterable
    found: Set<T>
    at SetImpl.union(SetImpl.java:69)
    at SetImpl.main(SetImpl.java:20)
    Java Result: 1

  11. #51
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Malv View Post
    its not. it has an error
    A Set<T> interface extends the Iterable<T> interface; you're talking about another Set<T> interface than I do ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #52
    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
    A Set<T> interface extends the Iterable<T> interface; you're talking about another Set<T> interface than I do ...

    Jos
    has no idea what you're talking about. you said the codes were right but it doesn't compile. is it still correct?

  13. #53
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Malv View Post
    has no idea what you're talking about. you said the codes were right but it doesn't compile. is it still correct?
    Yes; you really don't know what you're doing ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #54
    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
    Yes; you really don't know what you're doing ...

    Jos
    because you don't talk to the point. you just go round and round the bushes.

  15. #55
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,767
    Blog Entries
    7
    Rep Power
    21

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

    Quote Originally Posted by Malv View Post
    because you don't talk to the point. you just go round and round the bushes.
    It is you who has to do this assignment, not me; I am not going to spoonfeed you, you have to do at least some thinking yourself; if you can't do that, well too bad for you ...

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Replies: 4
    Last Post: 07-01-2013, 10: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, 10:07 PM
  3. Replies: 3
    Last Post: 04-14-2011, 07:59 AM
  4. How can we implement IPC in java
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 07: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
  •