Polymorphism example in SortResult

This commit is contained in:
2020-05-30 09:41:56 -05:00
parent d11a20536e
commit 136a759e73

View File

@@ -5,11 +5,47 @@ import java.time.Duration;
* This is a class to hold the efficiencty results of a sorter. We will use this
* because we can only have one return value from a method, but want to get more
* than one piece of data from each sorter.
*
* We will write several constructors for this class, as not all sorting algorithms
* will use all of the fields. For example, radix sort does not use comparisons.
* Counting sort does not use swaps.
*
* Writing several constructors for difference situations is an example of
* "polymorphism" -- the quality of object oriented design that lets a single
* interface have multiple implementations.
*
* The methods that call the constructor of SortResult don't know it has lots of
* them, they only know what data they have to give it.
*/
public class SortResult {
private int[] sortedArray;
private long comparisonsUsed;
private long swapsUsed;
private String comparisonsUsed;
private String swapsUsed;
private Duration timeUsed;
public SortResult(int[] a, long c, long s, Duration t){
sortedArray = a;
if(c==0){
comparisonsUsed = "None";
}
else{
comparisonsUsed = Long.toString(c);
}
if(s==0){
swapsUsed = "None";
}
else{
swapsUsed = Long.toString(s);
}
timeUsed = t;
}
public SortResult(int [] a, Duration t){
sortedArray = a;
timeUsed = t;
comparisonsUsed = "Unknown";
swapsUsed = "Unknown";
}
}