How can I modify the inOrder method in order to make it work for any class type,
ID: 3599375 • Letter: H
Question
How can I modify the inOrder method in order to make it work for any class type, for Example Staff.
You need to use WriteOutput to print out an object of type Staff.
This is my inOrder method:
public static void inOrder(BinaryTreeNode t) {
if (t != null)
{
inOrder(t.getLeft());
System.out.println(t.getValue());
inOrder(t.getRight());
}
This is my Staff class
public class Staff extends Employee implements Comparable<Staff>{
private int payGrade;
public Staff() {
super();
payGrade=0;
}
public Staff(int payGrade, String name, double salary, int hireDate, int ID, String department) {
super(name, salary, hireDate, ID, department);
this.payGrade = payGrade;
}
public int getPayGrade() {
return payGrade;
}
public void setPayGrade(int payGrade) {
this.payGrade = payGrade;
}
public void WriteOutput()
{
super.WriteOutput();
System.out.println("Pay Grade: " + payGrade);
}
public int compareTo(Staff other)
{
if (this.getSalary()>other.getSalary()) return 1;
if (this.getSalary()<other.getSalary()) return -1;
return 0;
}
}
Explanation / Answer
Hi friend you can implement like this:
// i assumed the structure of Node
class BinaryTreeNode<E> {
E value;
BinaryTreeNode<E> left;
BinaryTreeNode<E> right;
}
public static void inOrder(BinaryTreeNode<E> t) {
if (t != null)
{
inOrder(t.getLeft());
t.getValue().WriteOutput();
inOrder(t.getRight());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.