我正在使用 Java 进行作业,并且遇到了一个相当独特的问题。本质上,我的包中有两个文件:一个用于测试的驱动程序文件,然后是实际的类文件。我想知道如何从驱动程序/测试文件中调用或执行两个类深的语句?(即我们有公共类 Hello,在这个类中我们声明了另一个类,称为公共类 Goodbye。我们想在 Goodbye 中执行语句。)
下面是我的类声明和驱动程序文件的代码。我要执行的功能是 InOrder/PostOrder/PreOrder 类。我什至不确定使用“public void main(String args[])”是否是定义此函数的正确方法。如果您对定义函数的方法有其他建议,我愿意接受建议。
驱动文件:
package binTreeImprovedTwo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
/**
* Class TreeTest.
*/
public class TreeTest {
/**
* The main method.
*
* @param default arguments
*/
public static void main (String[] args) {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
BinTree tree = new BinTree();
int value;
String loop = "";
Random randomNumber = new Random();
System.out.println("Bulding binary tree...");
for (int i = 1; i <= 10; i++) {
value = randomNumber.nextInt(100);
System.out.printf("%d ", value);
tree.insertNode(tree.root, value);
}
while(loop != "quit"){
System.out.println("\nPlease select a traveral path:");
System.out.println("(Enter the choice in parenthesis EXACTLY)\n");
System.out.println("(inorder)Invokes InOrder Traversal");
System.out.println("(postorder)Invokes PostOrder Traversal");
System.out.println("(preorder)Invokes PreOrder Traversal");
System.out.println("(quit)Quits the Program");
System.out.println("Your choice: ");
try{
loop = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying to read your choice!\n");
System.exit(1);
}
System.out.println("\n");
if(loop == "inorder"){
InOrder.main(); //the calls for executing the statements
} else if(loop == "postorder"){
PostOrder.main(); // ''
} else if(loop == "preorder"){
PreOrder.main(); // ''
} else if (loop == "quit");
}
}
}
类声明:
package binTreeImprovedTwo;
import binTreeImprovedTwo.BinTree.Node;
/**
* Class BinTree.
*/
public class BinTree {
/** The root. */
public Node root = new Node(1111);
/**
* The Class Node.
*/
static class Node{
/** The left. */
Node left;
/** The right. */
Node right;
/** The data. */
int data;
/**
* Instantiates a new node.
*
* @param newData the new data
*/
public Node(int newData){
left = null;
right = null;
this.data = newData;
}
}
/**
* Bin tree.
*/
public void BinTree(){
}
/**
* Insert node.
*
* @param root is the root
* @param value is the value to be inserted
*/
public void insertNode(Node root, int value){
if(root.data == 1111){
root.data = value;
} else if(value < root.data){
if(root.left != null){
insertNode(root.left, value);
} else {
root.left = new Node(value);
}
} else if(value > root.data){
if(root.right != null){
insertNode(root.right, value);
} else {
root.right = new Node(value);
}
}
}
/**
* Super Class traverse.
*
*/
public class Traverse{
public void left(Node root){
if(root != null){
left(root.left);
}
} //end left function
public void print(){
System.out.println(" " + root.data);
} //end of print function
public void right(Node root){
if(root != null){
right(root.right);
}
} //end right function
} //end of class traverse
public class InOrder extends Traverse{
public void main(String args[]){
super.left(root);
super.print();
super.right(root);
}
} //end of InOrder class
public class PostOrder extends Traverse{
public void main(String args[]){
super.left(root);
super.right(root);
super.print();
}
} //end of PostOrder class
public class PreOrder extends Traverse{
public void main(String args[]){
super.print();
super.left(root);
super.right(root);
}
} //end of PreOrder class
}