【发布时间】:2014-10-09 11:13:29
【问题描述】:
我的课程作业要求我创建驱动程序来测试我的课程。显然,驱动程序只是一个具有测试单独类的 main 方法的类。分开是指驱动程序将测试不在驱动程序类中的类。如何在我的驱动程序中使用一个类?我要导入这个类吗?如果是这样,我如何导入我自己的课程之一?驱动程序的实现方式应该与类协定和主方法在同一个类中的方式完全相同。我对在同一个类中实现它们不感兴趣,因为它们必须是分开的。
如何在这个 TestBST 类中使用 BST 类? BST<String> bst = new BST<String>(tempHold); 行不行。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
/*
* This class implements a binary search tree. I have created addition methods and a main method
* in order to test the program. This program includes methods for counting the nodes on all levels
* of the tree. Getting tree height, ace values, node balance level, and balancing the tree.
* @param <E>
*/
public class TestBST {
public static void main(String[] args) {
System.out.println("David Jennings CMSC350 Project 3");
File input = new File("BSTINPUT.txt");
try {
Scanner reader = new Scanner(input);
ArrayList<String> valuePasser = new ArrayList<String>();
String[] tempStorage;
while (reader.hasNext()) {
String line = reader.nextLine();
tempStorage = (line.split(";"));
for (int i = 0; i < tempStorage.length; i++) {
valuePasser.add(tempStorage[i]);
}
}
String[] tempHold = new String[valuePasser.size()];
for (int i = 0; i < valuePasser.size(); i++) {
tempHold[i] = valuePasser.get(i);
}
BST<String> bst = new BST<String>(tempHold);
int actionChoice = 12;
do {
try {
actionChoice = Integer.parseInt(JOptionPane.showInputDialog("Please choose action: \n "
+ "(0) Exit program\n (1) In-order tree traversal\n (2) Pre-order tree traversal\n (3) CalculateACE\n"
+ "(4) CalculateMinAce\n (5) CalculateMaxACE\n (6) NumberOfNodesAllLevels\n (7) TreeHeight\n (8) NodeBalanceLevel\n "
+ "(9) NeedsBalancing\n (10) BalanceBST\n (11) insert value\n"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please only exit program by using input of 0 \nSorry, program only takes integer values between 0 and 10. Please restart program");
System.exit(1);
}
if (actionChoice < 0 || actionChoice > 11) {
JOptionPane.showMessageDialog(null, "Please only exit program by using input of 0 \nSorry, program only takes integer values between 0 and 10. please restart program");
System.exit(1);
}
if (actionChoice == 1) {
System.out.println(" In-order tree values: ");
bst.inorder();
System.out.println(" ");
}
if (actionChoice == 2) {
System.out.println("pre-order tree values: ");
bst.preorder();
System.out.println(" ");
}
if (actionChoice == 3) {
System.out.println("Tree ACE value : " + bst.calculateAce());
System.out.println(" ");
}
if (actionChoice == 4) {
System.out.println("Tree minACE value : " + bst.calculateMinAce());
System.out.println(" ");
}
if (actionChoice == 5) {
System.out.println("Tree maxACE value : " + bst.calculateMaxAce());
System.out.println(" ");
}
if (actionChoice == 6) {
System.out.println(" The number of nodes at all levels of the tree are:");
for (int i = 0; i < bst.treeHeight(); i++) {
System.out.println("Number of nodes at level: " + i);
System.out.println(bst.numberOfNodesAtLevel(i));
}
System.out.println(" ");
}
if (actionChoice == 7) {
System.out.println(" Current tree height: " + bst.treeHeight());
System.out.println(" ");
}
if (actionChoice == 8) {
System.out.println(" Node Balance Level: " + bst.nodeBalanceLevel());
System.out.println(" ");
}
if (actionChoice == 9) {
System.out.println(" Tree needs balancing?: " + bst.needsBalancing());
System.out.println(" ");
}
if (actionChoice == 10) {
bst.balanceBST();
System.out.println(" Balancing BST: " +"\n new balance level:" + bst.nodeBalanceLevel());
System.out.println(" ");
}
if (actionChoice == 11) {
bst.insert(JOptionPane.showInputDialog("Input integer to be added to tree: "));
System.out.println(" ");
}
} while (actionChoice != 0);
} catch (FileNotFoundException e) {
System.out.println("File not found. Please connect BSTINPUT.txt file and restart program.");
}
}
}
【问题讨论】:
-
对我来说听起来像是单元测试
-
看起来您缺少 BST 类的依赖项。如果您已经下载了代码或库,请确保这些 jar 或类在您的类路径中,并且我看到 BST 甚至没有被导入。所以确保 jar/class 在你的类路径中。