2014-10-09 73 views
-3

我的課程工作需要我創建驅動程序來測試我的課程。顯然,驅動程序只是一個具有測試單獨類的主要方法的類。分開來說,我的意思是驅動程序將測試一個不在驅動程序類中的類。我如何在我的驅動程序中使用一個類?我是否可以導入這門課?如果是這樣,我如何導入我自己的一個類?駕駛員程序的實施方式應與班級合同和主要方法屬於同一班級的方式完全相同。我沒有興趣在同一個班上實施他們,因爲他們必須是分開的。創建驅動程序

如何在此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."); 
     } 

    } 
} 
+1

這聽起來像是一個單元測試對我來說 – 2014-10-09 11:15:10

+0

看起來像你錯過了你的BST類的依賴。如果你已經下載了你的代碼或庫,請確保這些jar或類是在你的課程路徑上,我發現BST甚至沒有被導入。所以請確保jar/class在您的類路徑中。 – SMA 2014-10-09 11:39:53

回答

0

這是提示你

  1. 你只需要創建一個main方法的類..
  2. 這將是你的驅動程序類
  3. 然後創建另一個類,寫一些功能有
  4. 然後從第一個類的主要方法創建第二個類的對象引用..

這是需要什麼做

另一種方法是使用TestNG的是一些測試引擎來測試類

+0

是的,但它不會那樣工作。 – user3602515 2014-10-09 11:26:07

+0

所以你必須做什麼?..澄清你的問題PLZ – kirti 2014-10-09 11:27:34

+0

檢查更新的代碼。 – user3602515 2014-10-09 11:32:59

0

有你可以爲如做同樣的各種方式通過主要方法編寫的驅動程序的類或使用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(); 
    } 
} 
相關問題