Results 1 to 13 of 13
  1. #1
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default facade and collections

    Need some major help....I've been doing pretty well but this question confuses the heck out of me and I don't even know where to start....Assistance would be greatly appreciated......

    Let's write a simple logging facade that works with any collection, so that we can also practice writing generic classes. Write a class LoggingCollection<T> that extends AbstractCollection<T> and can be given any Collection<T> and a PrintWriter instances as its two constructor parameters. In this class, override the Collection methods add, contains, remove, size and iterator so that they just call the corresponding methods of the underlying collection and return whatever these methods return, but your methods additionally write to the given PrintWriter a short description of what was done to the collection (e.g. “Object Hello added to collection”)


    The below code is all I have....I need major help



    Java Code:
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.util.AbstractCollection;
    import java.util.Iterator;
    
    
    public class LoggingCollection<T> extends AbstractCollection<T> {
    
    	public LoggingCollection() {
    		
    	}
    	
    	public Iterator<T> iterator() {
    		
    		return null;
    	}
    
    	
    	public int size() {
    		
    		return 0;
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: facade and collections

    Do what your instructions tell you to do, one at a time.

    So first, give it the constructor that it's asking for as well as two fields that make sense for this constructor. Please give it a try and come on back with your code.

    Remember, one step at a time...

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: facade and collections

    Well, I thought you'd respond by now, ... oh well, I hope that means that you've solved this, as it's time for me to sleep. Best of luck.

  4. #4
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: facade and collections

    Oh, no...haven't soved it yet, but it was time for me to sleep too. I'm at work and can't work on it till the evening anyways. But can you provide a useful link that will help me with this? I can read at work..lol...tks

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: facade and collections

    Well, there's the API for AbstractCollection.
    Other than that, start as Fubarable says.
    You should at least have the constructor sorted by now.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: facade and collections

    tks. I usually don't ask for stuff to be written for me and i asked many questions before and i wanna write the code...it's just this collection stuff and facade confuses the heck out of me.....anyways, i wrote some code. Am i on the right track or completely wrong?

    Java Code:
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.util.AbstractCollection;
    import java.util.Collection;
    import java.util.Iterator;
    
    
    public class LoggingCollection<T> extends AbstractCollection<T> {
    
    	PrintWriter print;
    	Collection<T> coll;
    	
    	public LoggingCollection(Collection<T> col, PrintWriter print) {
    		this.print = print;
    		this.coll = col;
    		
    		
    	}
    	
    	public Iterator<T> iterator() {
    		Iterator<T> iter = coll.iterator();
    
    	    while (iter.hasNext()) {
    	    	System.out.println(iter.next());
    	    }
    
    		return null;
    	}
    
    	
    	public int size() {
    		
    		return coll.size();
    	}
    	public boolean add(T e){
    		return coll.add(e);
    		
    	}
    	
    }

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: facade and collections

    Java Code:
    /*
     *  Make things *private* unless you have some reason not to.
     */
        PrintWriter print;
        Collection<T> coll;
         
        public LoggingCollection(Collection<T> col, PrintWriter print) {
            this.print = print;
            this.coll = col;
             
             
        }
         
        public Iterator<T> iterator() {
            Iterator<T> iter = coll.iterator();
     /*
     * The instructions don't say anything about printing anything to System.out.
     * You might want to consider writing a short message to print saying that the iterator was obtained.
     */
            while (iter.hasNext()) {
                System.out.println(iter.next());
            }
     /*
     * Why return null?
     * The instructions say "call the corresponding methods of the underlying collection 
     * and return whatever these methods return".
     * So you should do that and return what coll returns when its iterator() method
     * is called.
     */
            return null;
        }
     
         
        public int size() {
    /*
     * Yes - this is the right thing to return.
     * But remember the instructions: "your methods additionally write to the given PrintWriter 
     * a short description of what was done to the collection"
     */         
            return coll.size();
        }
        public boolean add(T e){
    /*
     * ditto
     */
            return coll.add(e);
             
        }
    Writing messages to the PrintWriter instance is as easy as

    Java Code:
    print.println("Some message here");

  8. #8
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: facade and collections

    cool.....tks....not as hard as i thought i guess....so would this be good????

    Java Code:
    import java.io.PrintWriter;
    import java.io.Writer;
    import java.util.AbstractCollection;
    import java.util.Collection;
    import java.util.Iterator;
    
    
    abstract class LoggingCollection<T> extends AbstractCollection<T> {
    
    	PrintWriter print;
    	Collection<T> coll;
    	
    	private LoggingCollection(Collection<T> col, PrintWriter print) {
    		this.print = print;
    		this.coll = col;
    	}
    	
    	 public boolean add(T e){
    		
    		print.println("adding " + e + " to the colloection");
    		return coll.add(e);
    		
    	}
    	public boolean contains(Object o) {
    		boolean containsObject = coll.contains(o);
    		if(containsObject) {
    			print.println("collection contains " + o);
    		}
    		else print.println("collection does not contain" + o);
    		return containsObject;
    		
    	}
    	public boolean remove(Object o) {
    		boolean removeObj = coll.remove(o);
    		if(removeObj) {
    			print.println("removed " + o + " from collection");
    		}
    		else print.println("No " + o + " to remove");
    		
    		return removeObj;
    	}
    	 public int size() {
    		print.println("There are" + coll.size() + "in the collection");
    		return coll.size();
    		
    	}
    	public Iterator<T> iterator() {
    		Iterator<T> iter = coll.iterator();
    
    	    while (iter.hasNext()) {
    	    	System.out.println(iter.next());
    	    }
    	    print.println("creating an iterator");
    		return iter;
    	}
    	
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: facade and collections

    It all looks pretty good to me except for the iterator part. It looks as if you'll be returning a "spent" iterator due to that while loop, and that won't be of much use to the requester.

  10. #10
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: facade and collections

    so what should i return? just true?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: facade and collections

    Quote Originally Posted by Danieldcc View Post
    so what should i return? just true?
    No, you should return the Iterator object just as you are doing. Just don't use it in the method.

  12. #12
    Danieldcc is offline Member
    Join Date
    Sep 2010
    Posts
    83
    Rep Power
    0

    Default Re: facade and collections

    ah, i see....ok. tks for the help

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Help collections
    By nikosv in forum New To Java
    Replies: 8
    Last Post: 12-13-2010, 05:29 PM
  2. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 02:46 PM
  3. facade pattern
    By hannes in forum AWT / Swing
    Replies: 6
    Last Post: 02-09-2010, 11:28 PM

Posting Permissions

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