A ResultSet object will hold the results of a web search. Each result, or "hit" (web page that matches terms we searched for), will be represented by a Hit object. Each Hit object records the title and web address of one web page that matched our search criteria. A ResultSet should maintain a collection of these Hit objects.
when i run the driver SearchUI to give me ten results of a search term, it gives me 10 results, 0 through 9 and each says null (this is incorrect)..what is wrong in my ResultSet code?
this is my ResultSet class: public class ResultSet { private Hit[]hit,sortedresults; private int numhits,i,j;
public ResultSet(int maxsize) { hit= new Hit[maxsize]; numhits=0; }
public boolean addResult(Hit result) { if (numhits==hit.length) { return false; } hit[numhits]=result; numhits++; return true; }
public int getSize() { return numhits; }
public Hit getResult(int i) { if (hit.length<i+1) { return null; } return hit[i]; }
public boolean getSortedResults(int first, int last, boolean bytitle, Hit[] sortedresults) { if (bytitle) { for (int sortedsize=0; sortedsize<numhits-1; sortedsize++) { int earliestpos=sortedsize; for (int pos=earliestpos+1; pos<numhits; pos++) { if (hit[pos].compareByTitleTo(hit[earliestpos])<0) { earliestpos=pos; } } Hit a=hit[sortedsize]; hit[sortedsize]=hit[earliestpos]; hit[earliestpos]=a; sortedresults=new Hit[last-first+1]; j=0; for (int i=first; i<last+1; i++) { sortedresults[j]=hit[i]; j++; } return true; } } else { for (int sortedsize=0; sortedsize<numhits-1; sortedsize++) { int earliestpos=sortedsize; for (int pos=earliestpos+1; pos<numhits; pos++) { if (hit[pos].compareByAddressTo(hit[earliestpos])<0) { earliestpos=pos; } } Hit a=hit[sortedsize]; hit[sortedsize]=hit[earliestpos]; hit[earliestpos]=a; sortedresults=new Hit[last-first+1]; j=0; for (int i=first; i<last+1; i++) { sortedresults[j]=hit[i]; j++; } return true; } }
return false; }
}
and this is the Hit class: public class Hit { private static int comparisons = 0;
public static int getComparisons() { return comparisons; }
public static void resetComparisons() { comparisons = 0; }
Please format your code using the java tags as shown on the right side of the input field (Formatting Your Post).
It's really hard to read code like that.
The method getSortedResults has an error.
You pass an array to the method, inside this method you create a new array but this way the result is lost once the method is finished. You cave 2 possibilities: 1. Fill the original array 2. return the new array instead of the boolean value.