How to refrence a generic class from another class


How to refrence a generic class from another class
I am making a program that is able to sort of list of integers or fractions in either ascending or descending order. I am using a GUI and when I push the button the original list should be added to a binary tree. An inorder traversal should be performed and that list should be displayed in the GUI. I've gotten everything to work, but then I noticed I should have made the search tree class generic. I've changed the class to a generic but I am having issues calling it from the GUI to actually perform the traversal. Any help you can give will be appreciated.
This is the original SearchTree with Node class
public class SearchTree
// creates the root of the tree
Node root;
// String to store the answer while tree is being traversed
private String answer = "";
SearchTree (Object x)
root = new Node(x);
/* recursive method adding integers to the tree,
* placing it depending on if it is smaller or larger
* than the parent node
*/
Node insertInteger(Node node, Integer x)
if (node == null)
return node = new Node(x);
if (x < (Integer) node.parent)
node.left = insertInteger(node.left, x);
else
node.right = insertInteger(node.right, x);
return node;
// recursive method for adding fractions to the tree
Node insertFraction(Node node, Fraction x)
if (node == null)
return node = new Node(x);
if (x.compareTo((Fraction) node.parent) < 1)
node.left = insertFraction(node.left, x);
else
node.right = insertFraction(node.right, x);
return node;
// in order traversal for the ascending order
String inOrder(Node node)
if (node != null)
inOrder(node.left);
answer = answer + node.parent + " ";
inOrder(node.right);
return answer;
// reversed traversal for the descending order
String descendingOrder(Node node)
if (node != null)
descendingOrder(node.right);
answer = answer + node.parent + " ";
descendingOrder(node.left);
return answer;
class Node
Object parent;
Node left;
Node right;
// Constructors
Node(Object theParent)
this(theParent, null, null);
private Node(Object theParent, Node lChild, Node rChild)
parent = theParent;
this.left = lChild;
this.right = rChild;
Generic SearchTree
public class SearchTree<T extends Comparable<T>>
Node root; //root node of the tree
//defines each node of tree
class Node
T value;
Node right,left;
Node(T value)
this.value = value;
// inserts new node in tree
public void insert(T value)
if(isEmpty())
root = new Node(value);
else
insert(root, value);
// insert nodes in there proper places
private void insert(Node node, T value)
if(value.compareTo(node.value) < 0) //if new value less than parent node
if(node.left == null) //if left null then add
node.left = new Node(value);
else
insert(node.left,value); //if left not null then recursive call
else if(value.compareTo(node.value) >= 0) //if new value same or greater than parent node
if(node.right == null) //if right null then add
node.right = new Node(value);
else
insert(node.right,value); //if right not null then recursive call
// returns root value
public T peek()
return root.value;
// makes sure tree is not empty
public boolean isEmpty()
return (root == null)? true : false;
This is the issue
private <T extends Comparable<T>> void intOperation() throws NumberFormatException {
try {
String stringArray = txtOrig.getText().split(" ");
int intArray = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++)
intArray[i] = Integer.parseInt(stringArray[i]);
SearchTree<T> st = new SearchTree<T>();
for (int i = 1; i < intArray.length; i++)
T val = intArray[i];
st.insert(st.root, val);
if (rdbtnAscend.isSelected())
txtSort.setText(st.inOrder(st.root));
else
txtSort.setText(st.descendingOrder(st.root));
/***** This is the original along with the first bracketed try block
SearchTree st = new SearchTree(intArray[0]);
for (int i = 1; i < intArray.length; i++)
st.insertInteger(st.root, intArray[i]);
if (rdbtnAscend.isSelected())
txtSort.setText(st.inOrder(st.root));
else
txtSort.setText(st.descendingOrder(st.root));
*/
This is were I am stuck. How do I call the generic SearchTree class from this try block?
1 Answer
1
You are declaring your class correctly with a placeholder type T
. When you instantiate the class, you are using a placeholder instead of the type that you want the generic class to be.
T
SearchTree<Integer> st = new SearchTree<Integer>();
The generic class will need to instantiated with a concrete type to have all the type checking work correctly.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment