Hi I am trying to sort a generic object. When the object is created, it is created with three generic parameters and I want the parameters to be sorted when it is created.
Here is my code:
import java.util.*;
public class SortedTrio<T> extends Trio {
T firstST;
T secondST;
T thirdST;
public SortedTrio (T firstST, T secondST, T thirdST) {
super(firstST, secondST, thirdST);
}
}
and this is where the objects are created
import java.util.*;
public class TestHw2{
public static void main(String[] args) {
SortedTrio<Integer> STobj = new SortedTrio<Integer>(3, 1, 5);
SortedTrio<Integer> STobj1 = new SortedTrio<Integer>(5, 3, 1);
boolean isEqual = STobj.equals(STobj1);
if(isEqual)
System.out.println("Both are equal!");
else
System.out.println("Neither are equal!");
System.out.println(STobj1.toString());
System.out.println(STobj.toString());
}
}
this is my trio class if that's necessary to look at:
import java.util.*;
public class Trio<T> implements Comparable<Trio>
{
private List<T> arrObj = new ArrayList<T>(); //arraylist of generic type objects
public Trio(T first, T second, T third)
{
//add string objects to generic arraylist (which is set as a string arraylist by the main)
arrObj.add(first);
arrObj.add(second);
arrObj.add(third);
}
public T first() { //return first element
return arrObj.get(0);
}
public T second() { //return second element
return arrObj.get(1);
}
public T third() { //return third element
return arrObj.get(2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public T contains(T value) { //not done
return value;
}
public boolean equals(Trio t_object) {
boolean tt = false;
if (this.first() == t_object.first() && this.second() == t_object.second() && this.third() == t_object.third())
tt = true;
return tt;
}
public boolean matches() { //not done
return true;
}
public String toString() {
String strValue = "(" + arrObj.get(0) + ", " + arrObj.get(1) + ", " + arrObj.get(2) + ")";
return strValue;
}
public int compareTo(Trio that)
{
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
Trio<String> triObj = new Trio<String>("abc", "def", "ghi"); //create trio object of string type and give 3 string parameters
//retrieve strings in the object by using generic methods first(), second(), third()
System.out.println("The elements in generic class Trio are: " + triObj.first() +", " + triObj.second() +", " + triObj.third());
}
}
if there is anything that needs explaining, please let me know.
Thank you so much for any help!