Friday, 15 February 2013

java - Modify DataSet to accept Comparable objects -



java - Modify DataSet to accept Comparable objects -

i need modify dataset take comparable objects. tester not compile , not know how print out compareto method. should using arraylist tester? ahead of time!

public interface comparable { /** compares object another. @param other object compared @return negative integer, zero, or positive integer if object less than, equal to, or greater than, other */ int compareto(object other); } public class datasetcomparable { private double sum; private object maximum; private object minimum; private int count; private comparable comparer; /** constructs empty info set given measurer. @param ameasurer measurer used measure info values */ public datasetcomparable(comparable acomparer) { sum = 0; count = 0; maximum = null; minimum = null; comparer= acomparer; } /** adds info value info set. @param x info value */ public void add(object x) { sum = sum + comparer.compareto(x); if (count == 0 || comparer.compareto(maximum) < comparer.compareto(x)) maximum = x; if (count == 0 || comparer.compareto(minimum) > comparer.compareto(x)) minimum=x; count++; } /** gets largest of added data. @return maximum or 0 if no info has been added */ public object getmaximum() { homecoming maximum; } /**gets smallest of added data. *@return minimum or 0 if no info has been added **/ public object getminimum() { homecoming minimum; } } public class string implements comparable { private string input; private int holder; public string(string aninput){ input= aninput; holder=0; } public string getcomparer(){ homecoming input; } public string getstring(){ homecoming input; } public int compareto(object other){ string temp= (string) other; if(input.compareto(temp)<0){ holder=-1; } else if (input.compareto(temp)== 0) { holder= 0; } else{ holder= 1; } homecoming holder; } } public class stringtester{ public static void main (string [] args){ comparable c = new string(); datasetcomparable info = new datasetcomparable(c); data.add(new string("jimmy")); data.add(new string("amy")); data.add(new string("melissa")); data.add(new string("melissa")); string max = (string) data.getmaximum(); string min = (string) data.getminimum(); system.out.println("maximum string = " + max); system.out.println("minimum string = " + min); } }

your code includes this:

public class string implements comparable { ... }

do realize there standard java library class called string gets imported default every class? if implement own class called string going confusing compilation error messages.

i recommend alter name of class else; e.g. stringholder.

note, technically could define class called string. rules java uses disambiguate names of classes not designed use-case ... , end having refer java.lang.string qualified name wherever utilize it. , other people reading / modifying code find awkward / annoying.

it best treat names of classes in java.lang bundle "reserved", , don't define classes same (unqualified) name.

java comparable

No comments:

Post a Comment