我的课程工作要求我创建驱动程序来测试我的课程。显然,驱动程序只是一个带有测试单独类的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.");
}
}
}
答案 0 :(得分:0)
这是给你的提示
这是需要完成的事情
其他方法是使用testng作为测试引擎的一些测试引擎
答案 1 :(得分:0)
有多种方法可以做同样的事情,例如:通过main方法为您的班级编写驱动程序或使用junit / testng等编写测试。
带有main的样本如下:让我们假设您有一个名为Customer的类,其方法如sayHello,getId
public class Customer {
String custId;
public Customer(String custId) {
this.custId = custId;
}
public void sayHello() {
System.out.println("Hello " + custId);
}
public String getId() {
return custId;
}
}
public class CustomerDriver {
public static void main(String args[]) {
Customer customer = new Customer("1234");
customer.sayHello();
}
}