I\'m sure this is a very easy fix, but I just can\'t figure it out. I am doing a
ID: 3557636 • Letter: I
Question
I'm sure this is a very easy fix, but I just can't figure it out. I am doing a JUnit test, but my test class isn't working correctly as everytime it says "Baseconversion", it is underlined saying that there is a error that says "Cannot find symbol". I'm not sure what exactly the problem is.
This dropbox link is the link to my project file. If you could, just edit it, upload it, and send me the link, you will get 5 stars. What I want is just for the test file to work properly.
https://www.dropbox.com/sh/n61i0p51xqk3kpx/AACJbt-3UqxNiVNgnaiNdt7ia
Explanation / Answer
BaseConversionTest.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package baseconversiontest;
import baseconversionclient.BaseConversion;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import junit.framework.*;
/**
*
* @author mjsather2000
*/
public class BaseConversionTest {
/**
* JUnit test cases must have a constructor with a string
* parameter, otherwise an error message about TestCase
* being private in the aren't class will be generated
* pby javac. Calling the TestCase constructor is the only
* required action.
*/
public BaseConversionTest(String name)
{
}
/**
* Test the intToBinaryString method by comparing its result to
* the correct string representation.
*/
public void testIntToBinaryString()
{
// Note that this class is for demonstration purposes and
// that this is a woefully inadequate set of tests for this
// method...
assertEquals("0001", BaseConversion.intToBinaryString(1, 4));
}
/**
* Test the log2 method by comparing its output to known correct
* values.
*/
public void testLog2()
{
// This version of the assertEquals method allows you to
// specify a tolerance for equality of floating
// point values.
assertEquals(3.0, BaseConversion.log2(8.0), 0.0000001);
assertEquals(0.0, BaseConversion.log2(1.0), 0.0000001);
assertEquals(4.0, BaseConversion.log2(16.0), 0.0000001);
}
/**
* Test the isEven method by trying it with various values.
*/
public void testIsEven() {
assertTrue(BaseConversion.isEven(2));
assertTrue(BaseConversion.isEven(0));
assertTrue(BaseConversion.isEven(43567342));
assertTrue(!BaseConversion.isEven(1));
assertTrue(!BaseConversion.isEven(989293847));
assertTrue(!BaseConversion.isEven(-1));
}
/**
* Test the isOdd method by trying it with various values.
*/
public void testIsOdd() {
assertTrue(!BaseConversion.isOdd(2));
assertTrue(!BaseConversion.isOdd(0));
assertTrue(!BaseConversion.isOdd(43567342));
assertTrue(BaseConversion.isOdd(1));
assertTrue(BaseConversion.isOdd(989293847));
assertTrue(BaseConversion.isOdd(-1));
}
}
BaseConversion.java
package baseconversionclient;
public class BaseConversion
{
/**
* Multiply by this constant to convert from natural logarithm
* (as supplied by java.lang.Math.log) to log base 2.
*/
public static final double NATLOG_TO_LOG2 = 1.4426950408889634;
/**
* Multiply by this constant to convert from log base 10 to
* log base 2. Not too relevant here, since java.lang.Math.log()
* gives you the natural log, but included for the sake of
* completeness.
*/
public static final double LOG10_TO_LOG2 = 3.3219280948873626;
/**
* Create a string containing the binary translation of a
* non-negative integer. The string contains the binary
* representation right-justified in a field of specified
* width, filled with leading zeroes if necessary.
* @param n The int to translate
* @param width The width of the resulting string.
* @return The string containing the binary representation.
*
* Preconditions: n >= 0 and width > 0
* Postconditions: none
*/
public static String intToBinaryString(int n, int width) {
if (n < 0 || width <= 0) {
return ""; // Base case; no action necessary
} else {
String retStr = intToBinaryString(n/2, width - 1);
if (isEven(n)) {
retStr = retStr + "0";
} else {
retStr = retStr + "1";
}
return retStr;
}
}
/**
* log base 2 function.
* @param x number to calculate log base 2 of.
* @returns log base 2 of x as a double.
*
* Preconditions: x > 0.0
* Postconditions: none
*/
public static double log2 (double x)
{
return NATLOG_TO_LOG2 * Math.log(x);
}
/**
* Print out the binary translation of a non-negative integer.
*
* @param n The number to translate
* @param width The width of the field in which n should
* be printed. The printed value is filled
* to the left with 0.
*
* Preconditions: n >= 0 and width > 0
* Postconditions: none
*/
public static void printBinary(int n, int width) {
if (n < 0 || width <= 0) {
return; // Base case; no action necessary
} else {
printBinary(n/2, width - 1);
if (isEven(n)) {
System.out.print(0);
} else {
System.out.print(1);
}
}
}
/**
* Determine whether an integer is even.
* @param n An integer.
* @return <code>true</code> if n is even, <code>false</code>
* if not.
*
* Pre/postconditions: none
*/
public static boolean isEven(int n) {
return (n % 2 == 0);
}
/**
* Determine whether an integer is odd.
* @param n An integer.
* @return <code>true</code> if n is odd, <code>false</code>
* if not.
*
* Pre/postconditions: none
*/
public static boolean isOdd(int n) {
return (n % 2 != 0);
}
}