Question

In: Computer Science

I'm pretty new to JUnit testing. Can someone demonstrate how to test one of the tree...


I'm pretty new to JUnit testing. Can someone demonstrate how to test one of the tree traversals and the add() and addrecursive() methods?

public class BinaryTree {

private MorseNode<Character> root = new MorseNode<Character>();
private MorseNode<Character> left = null;
private MorseNode<Character> right = null;


public MorseNode<Character> getRoot() {
return root;
}


public void printPostorder(MorseNode<Character> node) {
if(node == null) {return;}

//System.out.print(node.getLetter());
//traverse left subtree
printPostorder(node.getLeft());
//traverse right subtree
printPostorder(node.getRight());
//node
if(node.getLetter() != null) {
System.out.print(node.getLetter() + " ");}

}

public void printPreorder(MorseNode<Character> node) {
if(node == null) {return;}
//System.out.print(node.getLetter());

//node
if(node.getLetter() != null) {
System.out.print(node.getLetter() + " ");}
//traverse left
printPreorder(node.getLeft());
//traverse right
printPreorder(node.getRight());
}


public void printInorder(MorseNode<Character> node) {
if(node == null) {return;}

//System.out.print(node.getLetter());
//traverse left subtree
printInorder(node.getLeft());
//node
if(node.getLetter() != null) {
System.out.print(node.getLetter() + " ");}
//traverse right subtree
printInorder(node.getRight());


}
void printInorder() {
System.out.println("Inorder Traversal: ");
printInorder(root);
System.out.println();
}

void printPreorder() {
System.out.println("PreOrder Traversal: ");
printPreorder(root);
System.out.println();
}

void printPostorder() {
System.out.println("PostOrder Traversal: ");
printPostorder(root);
System.out.println();
}


public void add(String str, char letter) {
//System.out.println(str + "sssssss " + letter );
if (str.charAt(0) == ".".charAt(0)) {
//`System.out.println(str + letter );
  root.setLeft(addRecursive(root.getLeft(), str.substring(1), letter));
}
else if (str.charAt(0) == "-".charAt(0))
root.setRight(addRecursive(root.getRight(), str.substring(1), letter));

}

public MorseNode<Character> addRecursive(MorseNode<Character> current, String str, char letter) {
  if (str.length() == 0) {
     return new MorseNode<Character>(letter);
  }

  if ( Character.compare(str.charAt(0), ".".charAt(0)) == 0) {
  current.setLeft(addRecursive(current.getLeft(), str.substring(1), letter));
  } else if (Character.compare(str.charAt(0), "-".charAt(0)) == 0) {
  current.setRight(addRecursive(current.getRight(), str.substring(1), letter));
  }

  

  return current;
}



}

import static org.junit.Assert.assertEquals;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class BinaryTreeTest {

@Test
void testInorder() {
BinaryTree object = new BinaryTree();
char a = 'a',b='c',c='c';
String as = ".",bs = ".-",cs="..";
object.add(as, a);
object.add(bs, b);
object.add(cs, c);

String inorder = "c a b";

}

@Test
void testPostorder() {

}

@Test
void testPreorder() {

}

@Test
void testadd() {

}

}


public class MorseNode<T> {


private T letter; //letter of node
private MorseNode<T> left; //left child
private MorseNode<T> right; //right child

public MorseNode() {


}

public MorseNode(T letter) {
this.letter=letter;

}
//GETS letter in node
public T getLetter() {
return letter;
}

//SETS letter in node
public void setLetter(T letter) {
this.letter = letter;
}

//GETS left child node
public MorseNode<T> getLeft() {
return left;
}

//GETS right child node
public MorseNode<T> getRight() {
return right;
}

//SETS left child node
public void setLeft(MorseNode<T> left) {
this.left = left;
}

//SETS right child node
public void setRight(MorseNode<T> right) {
this.right = right;
}

//print node
public String toString() {
String str = letter.toString();
return  str;
}
}

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

public class MorseNodeDriver {

public static void main(String[] args) throws FileNotFoundException {

BinaryTree theTree = new BinaryTree();

File codeFile = new File("morse.txt");
Scanner myReader = new Scanner(codeFile);


while (myReader.hasNextLine() ) {



String data = myReader.nextLine();
// System.out.println(data);
int len = data.length();
//if (len==2)

theTree.add(data.substring(1), data.charAt(0));


}

theTree.printInorder();
theTree.printPostorder();
theTree.printPreorder();
//System.out.println(theTree.toString());

File translateFile = new File("translate.txt");
Scanner read = new Scanner(translateFile);

System.out.println("\n\n____________Translations__________\n");

while(read.hasNextLine()) {

String morsecode = read.nextLine();
//System.out.println(morsecode);

String[] array = morsecode.split(" ");

for(int x = 0; x< array.length; x++) {
//System.out.println(array[x]);
if(array[x].contentEquals("")) {
System.out.print(" ");
}
else if(array[x].contentEquals(".")) { //e
System.out.print(theTree.getRoot().getLeft());

}
else if(array[x].contentEquals("..")) { //i
System.out.print(theTree.getRoot().getLeft().getLeft());

}
else if(array[x].contentEquals("...")) { //s
System.out.print(theTree.getRoot().getLeft().getLeft().getLeft());

}
else if(array[x].contentEquals("....")) { //h
System.out.print(theTree.getRoot().getLeft().getLeft().getLeft().getLeft());

}
else if(array[x].contentEquals("-")) { //t
System.out.print(theTree.getRoot().getRight());

}
else if(array[x].contentEquals("--")) { //m
System.out.print(theTree.getRoot().getRight().getRight());

}
else if(array[x].contentEquals("---")) { //o
System.out.print(theTree.getRoot().getRight().getRight().getRight());

}
else if(array[x].contentEquals(".-")) { //a
System.out.print(theTree.getRoot().getLeft().getRight());

}
else if(array[x].contentEquals("..-")) { //u
System.out.print(theTree.getRoot().getLeft().getLeft().getRight());

}
else if(array[x].contentEquals("...-")) { //v
System.out.print(theTree.getRoot().getLeft().getLeft().getLeft().getRight());

}
else if(array[x].contentEquals("-.")) { //n
System.out.print(theTree.getRoot().getRight().getLeft());

}
else if(array[x].contentEquals("--.")) { //g
System.out.print(theTree.getRoot().getRight().getRight().getLeft());

}

else if(array[x].contentEquals("-.-")) { //k
System.out.print(theTree.getRoot().getRight().getLeft().getRight());

}
else if(array[x].contentEquals("-..-")) { //x
System.out.print(theTree.getRoot().getRight().getLeft().getLeft().getRight());

}
else if(array[x].contentEquals(".-.")) { //r
System.out.print(theTree.getRoot().getLeft().getRight().getLeft());

}
else if(array[x].contentEquals("-..")) { //d
System.out.print(theTree.getRoot().getRight().getLeft().getLeft());

}
else if(array[x].contentEquals("-.--")) { //y
System.out.print(theTree.getRoot().getRight().getLeft().getRight().getRight());

}

//

else if(array[x].contentEquals(".-..")) { //l
System.out.print(theTree.getRoot().getLeft().getRight().getLeft().getLeft());

}
else if(array[x].contentEquals("..-.")) { //f
System.out.print(theTree.getRoot().getLeft().getLeft().getRight().getLeft());

}
else if(array[x].contentEquals("-...")) { //b
System.out.print(theTree.getRoot().getRight().getLeft().getLeft().getLeft());

}
else if(array[x].contentEquals("-.-.")) { //c
System.out.print(theTree.getRoot().getRight().getLeft().getRight().getLeft());

}
else if(array[x].contentEquals(".---")) { //j
System.out.print(theTree.getRoot().getLeft().getRight().getRight().getRight());

}
else if(array[x].contentEquals(".--.")) { //p
System.out.print(theTree.getRoot().getLeft().getRight().getRight().getLeft());

}
else if(array[x].contentEquals("--.-")) { //q
System.out.print(theTree.getRoot().getRight().getRight().getLeft().getRight());

}
else if(array[x].contentEquals(".--")) { //w
System.out.print(theTree.getRoot().getLeft().getRight().getRight());

}
else if(array[x].contentEquals("--..")) { //z
System.out.print(theTree.getRoot().getRight().getRight().getLeft().getLeft());

}




}
System.out.println();

}

//System.out.println(theTree.getRoot().getLeft().getLeft().getLeft().getLeft());
}

}

Solutions

Expert Solution

////////////////////////////////// Any problem , add in comment ////////////////////////////////////////

///////////////////////// System.out.println chnage to  System.out.print("\n"); /////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////


public class MorseNode<T> {


private T letter; //letter of node
private MorseNode<T> left; //left child
private MorseNode<T> right; //right child

public MorseNode() {


}

public MorseNode(T letter) {
this.letter=letter;

}
//GETS letter in node
public T getLetter() {
return letter;
}

//SETS letter in node
public void setLetter(T letter) {
this.letter = letter;
}

//GETS left child node
public MorseNode<T> getLeft() {
return left;
}

//GETS right child node
public MorseNode<T> getRight() {
return right;
}

//SETS left child node
public void setLeft(MorseNode<T> left) {
this.left = left;
}

//SETS right child node
public void setRight(MorseNode<T> right) {
this.right = right;
}

//print node
public String toString() {
String str = letter.toString();
return str;
}
}

//////////////////////////////////////////////////////////////////////

public class BinaryTree {

private MorseNode<Character> root = new MorseNode<Character>();
private MorseNode<Character> left = null;
private MorseNode<Character> right = null;


public MorseNode<Character> getRoot() {
return root;
}


public void printPostorder(MorseNode<Character> node) {
if(node == null) {return;}

//System.out.print(node.getLetter());
//traverse left subtree
printPostorder(node.getLeft());
//traverse right subtree
printPostorder(node.getRight());
//node
if(node.getLetter() != null) {
System.out.print(node.getLetter() + " ");}

}

public void printPreorder(MorseNode<Character> node) {
if(node == null) {return;}
//System.out.print(node.getLetter());

//node
if(node.getLetter() != null) {
System.out.print(node.getLetter() + " ");}
//traverse left
printPreorder(node.getLeft());
//traverse right
printPreorder(node.getRight());
}


public void printInorder(MorseNode<Character> node) {
if(node == null) {return;}

//System.out.print(node.getLetter());
//traverse left subtree
printInorder(node.getLeft());
//node
if(node.getLetter() != null) {
System.out.print(node.getLetter() + " ");}
//traverse right subtree
printInorder(node.getRight());


}
void printInorder() {
System.out.print("InOrder Traversal: \n");
printInorder(root);
System.out.print("\n");
}

void printPreorder() {
System.out.print("PreOrder Traversal: \n");
printPreorder(root);
System.out.print("\n");
}

void printPostorder() {
System.out.print("PostOrder Traversal: \n");
printPostorder(root);
System.out.print("\n");
}


public void add(String str, char letter) {
//System.out.println(str + "sssssss " + letter );
if (str.charAt(0) == ".".charAt(0)) {
//`System.out.println(str + letter );
root.setLeft(addRecursive(root.getLeft(), str.substring(1), letter));
}
else if (str.charAt(0) == "-".charAt(0))
root.setRight(addRecursive(root.getRight(), str.substring(1), letter));

}

public MorseNode<Character> addRecursive(MorseNode<Character> current, String str, char letter) {
if (str.length() == 0) {
return new MorseNode<Character>(letter);
}

if ( Character.compare(str.charAt(0), ".".charAt(0)) == 0) {
current.setLeft(addRecursive(current.getLeft(), str.substring(1), letter));
} else if (Character.compare(str.charAt(0), "-".charAt(0)) == 0) {
current.setRight(addRecursive(current.getRight(), str.substring(1), letter));
}

  

return current;
}

}

/////////////////////////////////////////////////////////////////////////// junit testing

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class BinaryTreeTest {

   private final PrintStream standardOut = System.out;
   private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
  
   @BeforeEach
   public void setUp() {
   System.setOut(new PrintStream(outputStreamCaptor));
   }
  
@Test
void testInorder() {
BinaryTree object = new BinaryTree();
char a = 'a',b='b',c='c';
String as = ".",bs = ".-",cs="..";
object.add(as, a);
object.add(bs, b);
object.add(cs, c);

String inorder = "InOrder Traversal: \nc a b \n";

object.printInorder();
assertEquals(inorder, outputStreamCaptor.toString());

}

@Test
void testPostorder() {
   BinaryTree object = new BinaryTree();
   char a = 'a',b='b',c='c';
   String as = ".",bs = ".-",cs="..";
   object.add(as, a);
   object.add(bs, b);
   object.add(cs, c);

   String postorder = "PostOrder Traversal: \nc b a \n";
   object.printPostorder();
   assertEquals(postorder, outputStreamCaptor.toString());
}

@Test
void testPreorder() {
   BinaryTree object = new BinaryTree();
   char a = 'a',b='b',c='c';
   String as = ".",bs = ".-",cs="..";
   object.add(as, a);
   object.add(bs, b);
   object.add(cs, c);

   String preorder = "PreOrder Traversal: \na c b \n";

   object.printPreorder();
   assertEquals(preorder, outputStreamCaptor.toString());
}

@Test
void testadd() {
   BinaryTree object = new BinaryTree();
   char a = 'p',b='q',c='r';
   String as = ".",bs = ".-",cs="..";
   object.add(as, a);  
   object.add(bs, b);
   object.add(cs, c);

   String inorder = "InOrder Traversal: \nr p q \n";
   object.printInorder();
  
   String preorder = "PreOrder Traversal: \np r q \n";
   object.printPreorder();
  
   String postorder = "PostOrder Traversal: \nr q p \n";
   object.printPostorder();
  
  
  
   assertEquals(inorder+preorder+postorder, outputStreamCaptor.toString());
  
}

}

////////////////////////////////////////////////////


Related Solutions

I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To...
Triangle Testing You are about to test the implementations of triangleType method with JUnit Testing. To test this Triangle class, you write a JUnit test class named TriangleTest.java. This test class contains multiple test cases in the format of methods. The test runner executes TriangleTest to generate a JUnit test report. In this project, you do not need to write the code for this Triangle class. Instead, you will write JUnit test cases to make sure that the implementation of...
PLEASE can u demonstrate the binomial tree model for this question! And tell me how to...
PLEASE can u demonstrate the binomial tree model for this question! And tell me how to work out the payoffs for the 2 period put!! Thanks Q- The current price of Excel Network Systems stock is £60 per share. In each of the next two years the stock price will either increase by 20% or decrease by 10%. The 3% one year risk free rate of interest will remain constant. Calculate the price of a two year European put option...
Can someone explain how to get the answers step by step? I'm really confused by this...
Can someone explain how to get the answers step by step? I'm really confused by this stats question and I really need it "dummied down." A researcher has gathered information from a random sample of 178 households. For each of the following variables, construct confidence intervals to estimate the population mean. Use the 90% level. A. An average of 2.3 people resides in each household. Standard devisfikn is 0.35 B. There was an average of 2.1 television sets (s= 0.10)...
You will be creating a JUnit Test Class for Gradebook.java, (listing 1.1) and you can download...
You will be creating a JUnit Test Class for Gradebook.java, (listing 1.1) and you can download it from blackboard. Gradebook has two attributes: an array of int called scores to hold scores and scoreSize that indicates how many scores are currently held in the array. This field is initially set to 0. Task #1: Add a getScoreSize() method to the Gradebook class which returns scoresSize; Add a toString() method to the Gradebook class that returns a string with each score...
Have a pretty good idea on how to solve this program, I'm just not very accustomed...
Have a pretty good idea on how to solve this program, I'm just not very accustomed to formatting well in Java for these types of problems. In java, you have full control of what's printed, I just need to get better at it, so I thought I'd post it here to receive an answer to learn from. Thanks for everything guys. This program must use the Exclusive OR Operator ^ in java, it must also use a while loop. The...
I'm completely lost can someone really break it down in English with how to answer each...
I'm completely lost can someone really break it down in English with how to answer each For each question, draw the appropriate picture, with shading. Then show all calculations and write a sentence for your answer. The weight of new-born babies in the US is normally distributed with a mean of 7.5 pounds and a standard deviation of 2 pounds. What percentage of new-born babies in the US weighs more than 7.5 pounds? What is the probability that a new-born...
Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im...
Draw a Koch Snowflake fractal image using MATLAB programming. I'm pretty new to MATLAB and im not too familar with how all the plotting and drawing functions work.
hi so i'm new to c++ i understand the language pretty good however no experience whatsoever...
hi so i'm new to c++ i understand the language pretty good however no experience whatsoever with visual studio here is the assigment Examine, understand, compile and run the FruitJuice Program (all files) and submit it (a copy is made available on LMS) i know what the program does and i get what is doing but i don't even know if i'm opening the thing the right way from visual studio it wont see it as a project at all....
A new test can screen someone with a family history for a particular type of aggressive...
A new test can screen someone with a family history for a particular type of aggressive cancer through genetic testing. Those that inherit this genetic marker, or 'cancer gene', from one or both of their parents will have a much greater risk of getting this cancer. Would you recommend this genetic screening? Why or why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT