创建驱动程序
我的课程工作需要我创建驱动程序来测试我的课程。显然,驱动程序只是一个具有测试单独类的主要方法的类。分开来说,我的意思是驱动程序将测试一个不在驱动程序类中的类。我如何在我的驱动程序中使用一个类?我是否可以导入这门课?如果是这样,我如何导入我自己的一个类?驾驶员程序的实施方式应与班级合同和主要方法属于同一班级的方式完全相同。我没有兴趣在同一个班上实施他们,因为他们必须是分开的。创建驱动程序
如何在此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.");
}
}
}
这是提示你
- 你只需要创建一个main方法的类..
- 这将是你的驱动程序类
- 然后创建另一个类,写一些功能有
- 然后从第一个类的主要方法创建第二个类的对象引用..
这是需要什么做
另一种方法是使用TestNG的是一些测试引擎来测试类
是的,但它不会那样工作。 – user3602515 2014-10-09 11:26:07
所以你必须做什么?..澄清你的问题PLZ – kirti 2014-10-09 11:27:34
检查更新的代码。 – user3602515 2014-10-09 11:32:59
有你可以为如做同样的各种方式通过主要方法编写的驱动程序的类或使用JUnit/TestNG的等
样品与主要写作测试如下:
让我们假设你有一个名为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();
}
}
这听起来像是一个单元测试对我来说 – 2014-10-09 11:15:10
看起来像你错过了你的BST类的依赖。如果你已经下载了你的代码或库,请确保这些jar或类是在你的课程路径上,我发现BST甚至没有被导入。所以请确保jar/class在您的类路径中。 – SMA 2014-10-09 11:39:53