
I have the Array class that contains an object and I need to make one that is similar but using a Linked List counting an Object, and both use the interface...
Can some one please help me out as I cant find any thing on the internet, even if its a example of something similar, it just I cant find any help on the matter...
Current coding listed below of the storage and interface classes
Interface
package sets_and_data_structures; public interface CompetitionInterface { public void addCompetitor(Competitor comp); public void addResult(int compNum, int result); public int[] listCompNum(); public void startListingByResult(); public void startListingByCompNumber(); public boolean hasNext(); public boolean hasNextResult(); public Competitor next(); }
Arrary Class
package sets_and_data_structures; import java.io.*; public class ArrayCompetition implements Serializable, CompetitionInterface { private Competitor list[]; private int numOfComp; private int nextPointer = -1; public ArrayCompetition() { list = new Competitor[100]; numOfComp=0; } public void addCompetitor(Competitor comp) { list[numOfComp] = comp; System.out.println(numOfComp+comp.toString()); numOfComp++; nextPointer = -1; // to prevent continuing iteration after an add with restarting } public void addResult(int compNum, int result) { for (int i=0;i<numOfComp;i++) { if (list[i].getCompNumber()==compNum) { list[i].setResult(result); } } } public int[] listCompNum() { sortByCompNumber(); int numList[] = new int[numOfComp]; for (int i=0;i<numOfComp;i++) { System.out.println("listing "+i); numList[i]=list[i].getCompNumber(); System.out.println("listed "+i); } return numList; } private void sortByResult() { int arrayLen = numOfComp; for (int pass=0; pass<arrayLen-1;pass++) { for (int i=0; i<arrayLen -pass -1; i++) { if (list[i+1].hasResult() && (!list[i].hasResult() || (list[i+1].hasResult() && list[i].getResult()>list[i+1].getResult()) ) ) { Competitor t = list[i]; list[i] = list[i+1]; list[i+1] = t; } } } }//sortByResult private void sortByCompNumber() { int arrayLen = numOfComp; for (int pass=0; pass<arrayLen-1;pass++) { for (int i=0; i<arrayLen -pass -1; i++) { if (list[i].getCompNumber()>list[i+1].getCompNumber()) { Competitor t = list[i]; list[i] = list[i+1]; list[i+1] = t; } } } }//sortByCompNumber public void startListingByResult() { if (numOfComp>0) { sortByResult(); nextPointer = 0; } } public void startListingByCompNumber() { if (numOfComp>0) { sortByCompNumber(); nextPointer = 0; } } public boolean hasNext() { return nextPointer!=-1; } public boolean hasNextResult() { return nextPointer!=-1 && list[nextPointer].hasResult(); } public Competitor next() { if (nextPointer!=-1) { Competitor comp = list[nextPointer]; nextPointer++; if (nextPointer==numOfComp) { nextPointer = -1; } return comp; } else { return null; } } public void listCompetitors() { System.out.println("Competition: number of Competitors is "+numOfComp); for (int i=0;i<numOfComp;i++) { System.out.println(list[i].toString()); } } } // end of class ArrayCompetition
Linked List Class
package sets_and_data_structures; import java.util.LinkedList; public class LLCompetition implements CompetitionInterface { Competitor comp; LinkedList competitors = new LinkedList(); public LLCompetition() { } public void addCompetitor(Competitor comp) { competitors.add(comp); } public void addResult(int compNum, int result) { } public int[] listCompNum() { return null; } public void startListingByResult() { } public void startListingByCompNumber() { } public boolean hasNext() { return false; } public boolean hasNextResult() { return false; } public Competitor next() { return null; } }
Edited by o0MattE0o, 31 May 2005 - 03:50 PM.