Skip to content

Instantly share code, notes, and snippets.

@jimmykurian
Created March 13, 2012 05:21
Show Gist options
  • Save jimmykurian/2026970 to your computer and use it in GitHub Desktop.
Save jimmykurian/2026970 to your computer and use it in GitHub Desktop.
A modified version of the DataSet class that accepts Comparable objects.
//DataSet.java - Jimmy Kurian
public class DataSet
{
private Comparable minimum;
private Comparable maximum;
public DataSet()
{
maximum = null;
minimum = null;
}
public void add(Comparable x)
{
if(minimum == null || x.compareTo(minimum) < 0)
minimum = x;
if(maximum == null || x.compareTo(maximum) > 0)
maximum = x;
}
public Comparable getMaximum()
{
return maximum;
}
public Comparable getMinimum()
{
return minimum;
}
}
//DataSetTester.java - Jimmy Kurian
public class DataSetTester
{
public static void main(String[] args)
{
DataSet data = new DataSet();
data.add(new String("Jimmy"));
data.add(new String("Anil"));
data.add(new String("Melissa"));
data.add(new String("Victor"));
String max = (String) data.getMaximum();
String min = (String) data.getMinimum();
System.out.println("Maximum String = " + max);
System.out.println("Minimum String = " + min);
}
}
Copy link

ghost commented Jan 19, 2015

Thanks for the code. Really helpful. But i think, we can use default constructor without declaring max and min value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment