Created
March 13, 2012 05:21
-
-
Save jimmykurian/2026970 to your computer and use it in GitHub Desktop.
A modified version of the DataSet class that accepts Comparable objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code. Really helpful. But i think, we can use default constructor without declaring max and min value.