Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Binary Tree java test class.

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Binary Tree java test class.

    So, managed to get my Binary Search Tree to work properly:

    package SystemandDesign.BinaryTree;
     
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.List;
    import java.io.ObjectInputStream;
    import java.util.Scanner;
     
    public class BinaryTree{
     
      private int data;
      private List<Integer> list;
      private BinaryTree root;
      private BinaryTree leftSubtree;
      private BinaryTree rightSubtree;
      private ObjectInputStream input;
     
      public BinaryTree(int value){
     
        this.data = value;
        this.leftSubtree = null;
        this.rightSubtree = null;
        this.root = null;
     
      }
     
      public int getData(){
     
        return this.data;
     
      }
     
      public void insert(int value){
     
        if(root == null){//starts a new Binary tree
          root = new BinaryTree(value);
        }
     
        if(value < data){//if the data being inserted is less than the original data, it will be inserted to the left side.
     
          if(leftSubtree == null){
            leftSubtree = new BinaryTree(value);
          }
          else{
            leftSubtree.insert(value);
          }
     
        }
        else if(value > data){//right side of binary tree
     
          if(rightSubtree == null){
            rightSubtree = new BinaryTree(value);
          }
          else{
            rightSubtree.insert(value);
          }
     
        }
     
     
      }
     
      public void inOrder(BinaryTree leaf){
     
        if(leaf != null){
          inOrder(leaf.leftSubtree);
          System.out.print(leaf.data+"");
          inOrder(leaf.rightSubtree);
     
     
      }
     
    }
     
      public static void main(String[] args){
        BinaryTree tree = new BinaryTree(9);
        tree.openFile();
        tree.readFile();
        tree.closeFile();
      }
     
      public void openFile(){
        try{
        input = new ObjectInputStream(new FileInputStream("data.ser"));
      }catch(IOException ioException){
        System.err.println("Error opening file.");
      }
      }
      public void readFile(){ 
      try{
        Scanner in = new Scanner(new FileInputStream("data.ser"));
      }catch(IOException e){
        System.err.println("Error class not found.");
      }
      }
      public void closeFile(){
        try{
          if(input != null)
            input.close();
        }catch(IOException ioException){
          System.err.println("Error closing file.");
          System.exit(1);
        }
      }
    }

    Now I need to create a test class for it

    package SystemandDesign.BinaryTree;
     
    import junit.framework.TestCase;
     
    public class BinaryTreeTest extends TestCase{
     
      BinaryTree d1;
      BinaryTree d2;
      BinaryTree d3;
      BinaryTree d4;
      BinaryTree d5;
      BinaryTree d6;
      BinaryTree d7;
      BinaryTree d8;
      private BinaryTree leftSubtree;
      private BinaryTree rightSubtree;
     
      public void setUp(){
     
        d1 = new BinaryTree(2);
        d2 = new BinaryTree(5);
        d3 = new BinaryTree(9);
        d4 = new BinaryTree(10);
        d5 = new BinaryTree(12);
        d6 = new BinaryTree(19);
        d7 = new BinaryTree(3);
        d8 = new BinaryTree(4);
      }
      public void testInsert(){
        assertEquals(d1 == d2);
     
     
      }
      public void testinOrder(){
     
     
     
      }
      }

    No matter how many times I keep rewritting the testInsert method I get a chock full of errors.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,490
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Binary Tree java test class.

    Post the errors.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Tree java test class.

    1 error and 4 warnings found:
    -------------
    *** Error ***
    -------------
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary TreeTest.java [line: 30]
    Error: The method assertEquals(boolean, boolean) in the type junit.framework.Assert is not applicable for the arguments (boolean)
    --------------
    ** Warnings **
    --------------
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 12]
    Warning: The field SystemandDesign.BinaryTree.BinaryTree.list is never read locally
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 91]
    Warning: The local variable in is never read
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary TreeTest.java [line: 15]
    Warning: The field SystemandDesign.BinaryTree.BinaryTreeTest.leftSubt ree is never read locally
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary TreeTest.java [line: 16]
    Warning: The field SystemandDesign.BinaryTree.BinaryTreeTest.rightSub tree is never read locally

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,490
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: Binary Tree java test class.

    This is a JUnit question, which is fine, but you should understand that.

    What don't you understand about this error or how to fix it? (Read it carefully, look at your code, THINK about how the error message describes the problem with your code, determine a fix.):

    Error: The method assertEquals(boolean, boolean) in the type junit.framework.Assert is not applicable for the arguments (boolean)

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Binary Tree java test class.

    Quote Originally Posted by GregBrannon View Post
    This is a JUnit question, which is fine, but you should understand that.

    What don't you understand about this error or how to fix it? (Read it carefully, look at your code, THINK about how the error message describes the problem with your code, determine a fix.):

    Error: The method assertEquals(boolean, boolean) in the type junit.framework.Assert is not applicable for the arguments (boolean)
    I realize it is taking the value that I am entering as a boolean, its just for this BinaryTree class, I have the faintest clue on how to insert a value(the testcase, not the actual class) into the insert method when BinaryTree(int value) is used to insert a new value to be put as either a root, rightSubtree or leftSubtree. tried using assertEquals(d1.insert()); or assertEquals(d1.insert(rightSubtree)); and even assertEquals(d1.equals(d2));

Similar Threads

  1. (Binary Tree) Family tree - help with my addChild method
    By Pip_Squeak in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 26th, 2014, 07:52 AM
  2. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 04:28 PM
  3. Java Binary Tree (Beginners)?
    By IAmHere in forum Algorithms & Recursion
    Replies: 6
    Last Post: June 19th, 2011, 10:28 AM
  4. Java Binary Tree
    By comwizzz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 02:15 PM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM