first tests with JUnit

Hi,

working out of the "beginning algorithms" book. In the book, it gives an example that doesn't compile for me in eclipse. It's the line that has the assert command in it; I double checked to make sure it was spelled correctly, etc.

here's the code.

package com.jasonwardenburg.algorithms.ch02;

publicfinalclass PowerCalculator{

publicstaticfinal PowerCalculator INSTANCE =new PowerCalculator();

private PowerCalculator(){

}

publicint calculate(int base,int exponent){

assert exponent >= 0 :"exponent can't be < 0";

int result = 1;

for (int i = 0; i < exponent; ++i){

result *= base;

}

return result;

}

}

thanks,

bp;

[1585 byte] By [badpersona] at [2007-11-27 8:39:59]
# 1

I added a main funtion and it worked just fine.

public final class PowerCalculator {

public static final PowerCalculator INSTANCE = new PowerCalculator();

private PowerCalculator(){

}

public int calculate(int base, int exponent){

assert exponent >= 0 : "exponent can't be < 0";

int result = 1;

for (int i = 0; i < exponent; ++i){

result *= base;

}

return result;

}

public static void main(String[] args)

{

PowerCalculator pc = new PowerCalculator();

System.out.println(pc.calculate(3, 4));

}

}

How doesn't it work for you?

petes1234a at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 2

or better, to test it in a separate class, you have to call instance:

public class TestPowerCalc

{

public static void main(String[] args)

{

PowerCalculator pc = PowerCalculator.INSTANCE;

System.out.println(pc.calculate(1, 4));

System.out.println(pc.calculate(2, 4));

System.out.println(pc.calculate(3, 4));

System.out.println(pc.calculate(4, 4));

}

}

petes1234a at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 3

I tried with that main method, and it still didn't work. I get this error in eclipse:

Severity and DescriptionPathResourceLocationCreation TimeId

Syntax error on token "assert", ( expectedalgorithms/com/jasonwardenburg/algorithms/ch02PowerCalculator.javaline 111182699525000113

could it be that eclipse is pointing to an old version of java?

bp

badpersona at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 4

here's my test class:

package com.jasonwardenburg.algorithms.ch02;

import junit.framework.TestCase;

public class PowerCalculatorTest extends TestCase {

public PowerCalculatorTest(String name) {

super(name);

}

protected void setUp() throws Exception {

super.setUp();

}

protected void tearDown() throws Exception {

super.tearDown();

}

public void testAnythingRaisedToThePowerOfZeroIsOne(){

PowerCalculator calculator = PowerCalculator.INSTANCE;

assertEquals(1, calculator.calculate(0,0));

assertEquals(1, calculator.calculate(1,0));

assertEquals(1, calculator.calculate(27,0));

assertEquals(1, calculator.calculate(143,0));

}

public void testAnythingRaisedToThePowerOfOneIsItself(){

PowerCalculator calculator = PowerCalculator.INSTANCE;

assertEquals(0, calculator.calculate(0,1));

assertEquals(1, calculator.calculate(1,1));

assertEquals(27, calculator.calculate(27,1));

assertEquals(143, calculator.calculate(143,1));

}

public void testAritrary(){

PowerCalculator calculator = PowerCalculator.INSTANCE;

assertEquals(0, calculator.calculate(0,2));

assertEquals(1, calculator.calculate(1,2));

assertEquals(4, calculator.calculate(2,2));

assertEquals(8, calculator.calculate(2,3));

assertEquals(27, calculator.calculate(3,3));

}

}

badpersona at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 5
It has nothing to do with Eclipse and everything to do with which version of java you are compiling with. You should compile with version 1.5 or 1.6.Message was edited by: petes1234
petes1234a at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 6
> It has nothing to do with Eclipse and everything to> do with which version of java you are compiling with.> You should compile with version 1.5 or 1.6.> Message was edited by: > petes1234and perhaps version 1.4,... I'm not sure of this.
petes1234a at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 7
I'm pretty sure I have 1.5this is on my machine:C:\Program Files\Java\jdk1.5.0_08Message was edited by: badperson
badpersona at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 8

> I'm pretty sure I have 1.5

>

> this is on my machine:

> C:\Program Files\Java\jdk1.5.0_08

Being on the machine doesn't mean that you are compiling with this. If you are using Eclipse, go into Window -- Preferences -- Java -- Compilerand see what your settings are.

petes1234a at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 9
ahhh...gotcha.'that worked, it was set to 1.4, I changed to 5.0 thanks a million!!bp
badpersona at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...
# 10
you're welcome. Glad it's working.
petes1234a at 2007-7-12 20:38:14 > top of Java-index,Java Essentials,New To Java...