Results 1 to 1 of 1
Thread: Please help in data structures
- 03-27-2009, 10:41 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 42
- Rep Power
- 0
Please help in data structures
Hello. I hope you guys are familiar with containers and linked lists and searchable containers ... ( How arrays and linked lists work ) .. I am having a problem with this requirement and I am hoping that you guys can help. Ill display the problem here and all the classes along with it for u to get a better look .. ( sorry if this thread seemed very long ).
(a) Write a visitor DuplicatedElementsFinder whose instance returns a MySearchableContainer set of the duplicated elements, of a
MyContainer object it visits, each of which has a frequency greater than three. An empty MySearchableContainer should be
returned if there are no such duplicated elements.
Example: If the visited container contains the objects {5, 5, 5, 20, 8, 1, 8, 20, 8, 3, 20, 8, 8, 20} then the returned container is
{8, 20}
Hint: Use the Association design pattern and two MySearchableContainer objects in your visit method.
(b) Write an appropriate test class to test the DuplicatedElementsFinder visitor.
Java Code:package ics202.lab02; import ics202.*; import ics202.lab01.*; public class MySearchableContainer extends MyContainer implements SearchableContainer { private int findIndex(Comparable target){ int index = 0; while(index < count && !array[index].equals(target)) index++; if(index < count) //then target is found return index; else return -1; } public void withdraw(Comparable target){ int index = findIndex(target); if(index != -1) { //the target exists for (int i = index; i<count-1; i++) array[i] = array[i+1]; // shift each element from index to count -1 count--; } } //returns the reference to the target if found, null otherwise public Comparable find(Comparable target) { // to be implemented by students int index = findIndex(target); if( index == -1){ return null; } return array[index]; } //returns true if target is in the container, false otherwise. public boolean isMember(Comparable target) { // to be implemented by students boolean found = true; int index = findIndex(target); if(index == -1){ found = false; } return found; } }Java Code:package ics202.lab01; import ics202.*; public class MyContainer extends AbstractContainer { private final int SIZE = 100; protected Comparable[] array = new Comparable[SIZE]; public boolean isFull() { return count == SIZE; } public void purge() { for (int i=0; i<count; i++) array[i] = null; count = 0; } public int getSize(){ return SIZE; } public Iterator iterator() { return new Iterator () { private int pos = 0; public boolean hasNext() { return pos < count; } public Object next() { Comparable element = array[pos]; pos++; return element; } }; } public void insert(Comparable object) { if(isFull()) throw new ContainerFullException(); else{ array[count] = object; count++; } } }Java Code:package ics202; public class Association implements Comparable{ protected Comparable key; protected Object value; public Association(Comparable comparable, Object obj){ key = comparable; value = obj; } public Association(Comparable comparable){ this(comparable, null); } public Comparable getKey(){ return key; } public Object getValue(){ return value; } public void setKey(Comparable key){ this.key = key; } public void setValue(Object value){ this.value = value; } public int compareTo(Object obj){ Association association = (Association)obj; return key.compareTo(association.getKey()); } public boolean equals(Object obj){ return compareTo(obj) == 0; } public String toString(){ String s = "{ " + key; if(value != null) s = s + " , " + value; return s + " }"; } }
And this is the class that I need to implements
Here are just in case you need to see classesJava Code:public class DuplicatedElementsFinder extends AbstractVisitor { }
Java Code:package ics202; public abstract class AbstractContainer implements Container, Comparable { protected int count; public int getCount () { return count; } public boolean isEmpty () { return getCount () == 0; } public boolean isFull () { return false; } public void accept(Visitor visitor) { Iterator iterator = iterator(); while ( iterator.hasNext() && !visitor.isDone()) visitor.visit(iterator.next()); } public String toString() { final StringBuffer buffer = new StringBuffer(); AbstractVisitor visitor = new AbstractVisitor() { private boolean comma; public void visit(Object obj) { if(comma) buffer.append(" "); buffer.append(obj); comma = true; } }; accept(visitor); return "" + buffer; } public int compareTo(Object object){ throw new MethodNotImplemented(); } public abstract void purge(); public abstract Iterator iterator(); }Java Code:package ics202; public abstract class AbstractVisitor implements Visitor { public abstract void visit (Object object); public boolean isDone () { return false; } }
lol thank you.
Similar Threads
-
C like data structures in java ?
By ankitmcgill in forum New To JavaReplies: 10Last Post: 11-09-2008, 01:53 PM -
Data Pipeline 2 - Data Transformation Toolkit for Java Released
By dele in forum Java SoftwareReplies: 0Last Post: 10-31-2008, 02:13 PM -
Some questiong about Data Structures in Java
By Javid in forum New To JavaReplies: 5Last Post: 10-10-2008, 07:33 AM -
Sux data structures 1.0.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-15-2008, 06:54 PM -
Data Structures Help...
By jac0117 in forum New To JavaReplies: 1Last Post: 01-12-2008, 07:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks