Results 1 to 13 of 13
Thread: facade and collections
- 03-28-2012, 04:46 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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; } }
-
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...
-
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.
- 03-28-2012, 03:07 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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
- 03-28-2012, 03:33 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 03-29-2012, 02:48 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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); } }
- 03-29-2012, 03:01 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Re: facade and collections
Writing messages to the PrintWriter instance is as easy asJava 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); }
Java Code:print.println("Some message here");
- 03-29-2012, 03:50 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
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; } }
-
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.
- 03-29-2012, 10:48 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: facade and collections
so what should i return? just true?
-
Re: facade and collections
- 03-31-2012, 10:28 PM #12
Member
- Join Date
- Sep 2010
- Posts
- 83
- Rep Power
- 0
Re: facade and collections
ah, i see....ok. tks for the help
-
Similar Threads
-
Help collections
By nikosv in forum New To JavaReplies: 8Last Post: 12-13-2010, 05:29 PM -
Collections
By Cbani in forum New To JavaReplies: 3Last Post: 02-16-2010, 02:46 PM -
facade pattern
By hannes in forum AWT / SwingReplies: 6Last Post: 02-09-2010, 11:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks