【问题标题】:Creating a driver program创建驱动程序
【发布时间】: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 在你的类路径中。

标签: java import


【解决方案1】:

这里有提示

  1. 您只需要使用 main 方法创建一个类..
  2. 这将是您的驱动程序类
  3. 然后创建另一个类并在那里编写一些功能
  4. 然后从第一个类的 main 方法创建第二个类的对象引用..

这是必须要做的事情

其他方法是使用一些测试引擎的testng来测试类

【讨论】:

  • 那么你必须做什么?..请澄清你的问题
【解决方案2】:

您可以通过多种方式来做同样的事情,例如通过 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();
    }
}

【讨论】:

    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2010-09-25
    • 2011-09-02
    • 2014-02-05
    • 2011-05-12
    • 2017-04-09
    相关资源
    最近更新 更多