// adds a new droid to droid collections public void Add(Droid NDroid) { DCollection[DCLength] = NDroid; //increment length and highest value DCLength++; highestValue++; }
//CampareTo inheriting from IComparable public int CompareTo(object obj) { Droid otherD = (Droid)obj; decimal TCost = TotalCost; decimal otherTCost = otherD.TotalCost; return(TCost.CompareTo(otherTCost)); }
//**************END METHOD THAT OVERRIDES TOSTRING****************// public int CompareTo(object obj) //CompareTo method which must be implemented when inheriting from IComparable. { //This method will compare the total costs between two droids which will aid the merge sort. Droid otherDroid = (Droid)obj; decimal thisTotalCost = this.TotalCost; decimal otherTotalCost = otherDroid.TotalCost; return(thisTotalCost.CompareTo(otherTotalCost)); }
//Method for comparing droids total cost public int CompareTo(object obj) { //Downcasts passed in object as a Droid Droid compareDroid = (Droid)obj; //Calculates currently selected Droid's Total Cost this.CalculateTotalCost(); //Finds passed in Droid's Total Cost compareDroid.CalculateTotalCost(); //Returns a value to determine whether or not the passed in Droid has a lower, higher, or equal Total Cost return(this.totalCost.CompareTo(compareDroid.totalCost)); }
//This here CompareTo method is here to implement the IComparable interface. It does pretty much what it says on the tin, comparing the total costs //of two different droids. I had some trouble for awhile here because the droids don't have their costs calculated automatically when they are added //to the array(They get calculated in the Print String method in Droid Collection), so I ended up comparing alot of zeroes. public int CompareTo(object obj) { if (obj == null) { return(1); } Droid otherDroid = obj as Droid; if (otherDroid != null) { return(this.totalCost.CompareTo(otherDroid.totalCost)); } else { throw new ArgumentException("Droid Doesn't have a cost."); } }
//***************END METHOD TO CREATE NEW DROID COLLECTION ARRAY***************// //*******************METHOD TO ADD NEW DROID TO COLLECTION**************// public void AddNewDroid(Droid newDroid) { droidCollection[droidCollectionLength] = newDroid; droidCollectionLength++; highestIndex++; //increment int by one with each droid added to droid collection }
//Compares the droid with another public int CompareTo(object obj) { Droid tempDroid = (Droid)obj; return(this.totalCost.CompareTo(tempDroid.totalCost)); }
// Public method to compare droids based on their total costs: public override int CompareTo(Droid otherDroid) { return(this.TotalCost.CompareTo(otherDroid.TotalCost)); }
//***************END METHOD TO CREATE NEW DROID COLLECTION ARRAY***************// //*******************METHOD TO ADD NEW DROID TO COLLECTION**************// public void AddNewDroid(Droid newDroid) { droidCollection[droidCollectionLength] = newDroid; droidCollectionLength++; highestIndex++; //increment int by one with each droid added to droid collection }
public virtual int CompareTo(object obj) { //Method to return the difference between this droid's total cost and the passed droid's total cost. Positive means this is more expensive. Droid otherDroid = (Droid)obj; return(this.totalCost.CompareTo(otherDroid.totalCost)); }
public void SortbyTotalCost() { MergeSort mergeSort = new MergeSort(); Droid[] droids = new Droid[LengthofCollection]; mergeSort.sort(droids); }