Hey all!
I am having trouble debugging my program, and after exhausting all of my options i decided to come to you guys.
The assignment requires us to create the methods for the tes tin junit.
I have most of it working, but have a few problems, mainly with my toString() method.
I could really use some help please, any insight or clue as to why this is not working will be greatly appreciated.
Thanks in advance! :)
Here is my code:
1) this is the term class where i need to create the methods
package poly;
public class Term
{
final private int coef;
final private int expo;
private static Term zero, unit;
static { try { zero = new Term(0,0); // the number zero
unit = new Term(1,0); // the number one
}
catch (Exception e) {
// constructor will not throw an exception here anyway
}
}
/**
*
* @param c The coefficient of the new term
* @param e The exponent of the new term (must be non-negative)
* @throws NegativeExponent
*/
public Term(int c, int e) throws NegativeExponent {
if (e <= 0) throw new NegativeExponent();
coef = c;
expo = (coef == 0) ? 1 : e;
}
final public static Term Zero = zero;
final public static Term Unit = unit;
/**
* Tests to see if the term represents a constant value
* @return true if the term has a zero coefficient
*/
public boolean isConstant() {
return false; // unimplemented - default value false is only a placeholder
}
/**
* Tests to see if the term is equal to zero
* @return true if the term is equal to zero
*/
public boolean isZero() {
return false; // unimplemented - default value false is only a placeholder
}
/**
* Tests to see if the term is negative
* @return true if the coefficient is less than
*/
public boolean isNegative() {
return false; // unimplemented - default value false is only a placeholder
}
/**
* Represents a term as a string in a standard form: specification by example:
* (An underscore represents any value)
*
* (-7,0) => "-7"
* (-7,1) => "-7x"
* (-7,2) => "-7x^2"
*
* (-1,0) => "-1"
* (-1,1) => "-x"
* (-1,2) => "-x^2"
*
* (0, _) => "0"
*
* (1,0) => "1"
* (1,1) => "x"
* (1,2) => "x^2"
*
* (7,0) => "7"
* (7,1) => "7x"
* (7,2) => "7x^2"
*/
@Override
public String toString() {
return null; // unimplemented - default value null is only a placeholder
}
@Override
public boolean equals(Object other) {
return false; // unimplemented - default value false is only a placeholder
}
/**
* Negates a term
* @return a new term with the original coefficient negated
* @throws CoefficientOverflow
*/
public Term negate() throws CoefficientOverflow {
return null; // unimplemented - default value null is only a placeholder
}
/**
* Multiplies a term by a scalar amount
* @param m The value to multiply by
* @return a new Term whose coefficient is the original coefficient multiplied by scale
* @throws CoefficientOverflow
*/
public Term scale(int m) throws CoefficientOverflow {
return null; // unimplemented - default value null is only a placeholder
}
/**
* Multiplies two terms together
* @param that represents the term to be multiplied by
* @return a new Term whose coefficient is the product of the two terms, and whose exponent is the sum of the two terms
* @throws CoefficientOverflow
* @throws ExponentOverflow
*/
public Term times(Term that) throws CoefficientOverflow, ExponentOverflow {
return null; // unimplemented - default value null is only a placeholder
}
/**
* Adds two terms together
* Precondition: The two exponents must be equal for this to be valid
* @param thats represent the term to be added
* @return a new Term whose coefficient is the sum of the two original coefficients
* @throws CoefficientOverflow
* @throws IncompatibleTerms
*/
public Term add(Term that) throws CoefficientOverflow, IncompatibleTerms {
return null; // unimplemented - default value null is only a placeholder
}
}
2)this is the class test that i am stuck on
package poly;
import static org.junit.Assert.*;
// version 3
import org.junit.Test;
public class ConEqTest
{
private int min = Integer.MIN_VALUE;
private int max = Integer.MAX_VALUE;
/* Testing the constructor
*
* Values for coefficient:
* min
* -7
* 0
* 7
* max
* Values for exponent:
* -1
* 0
* 9
* max
* Combinations for test cases:
* con1 (min,9) ok
* con2 (-7,9) ok
* con3 (0,9) ok
* con4 (7,9) ok
* con5 (max,9) ok
* con6 (7, -1) NegativeExponent
* con7 (7,0) ok
* con8 (7,9) ok
* con9 (7,max) ok
*/
@Test public void con1() throws TError { new Term(min,9); }
@Test public void con2() throws TError { new Term(-7,9); }
@Test public void con3() throws TError { new Term(0,9); }
@Test public void con4() throws TError { new Term(7,9); }
@Test public void con5() throws TError { new Term(max,9); }
@Test (expected = NegativeExponent.class) public void con6() throws TError { new Term(7,-1); }
@Test public void con7() throws TError { new Term(7,0); }
@Test public void con8() throws TError { new Term(7,9); }
@Test public void con9() throws TError { new Term(7,max); }
/* Testing equality
* Future tests will need to use assertEquals between Terms. Therefore it is essential to have
* confidence in the equals method() before any tests using assertEquals() are run.
*
* Valid term valid coefficient categories: less than zero, zero, greater than zero
* Valid term valid exponent categories: zero, greater than zero
* Invalid term category: exponent less than zero, null object, non-Term objects
* Equality category: equivalent terms, diff coefficients, diff exponents
*
* The receiving object (this) must be a valid term if the constructor itself is working properly.
* The parameter object (that) must be a valid term if it is a Term object
* Nevertheless we will incorporate some tests in which we attempt to construct invalid Term objects
*
* Valid terms that are equivalent:
* eq01 (-10,0) (-10,0) => true
* eq02 (0,0) (0,0) => true
* eq03 (0,0) (0,2) => true
* eq04 (10,0) (10,0) => true
* eq05 (-10,2) (-10,2) => true
* eq06 (0,2) (0,0) => true
* eq07 (0,2) (0,2) => true
* eq08 (10,2) (10,2) => true
*
* Valid terms that are not equivalent
* eq09 (-10,0) (0,0) => false
* eq10 (-10,0) (10,0) => false
* eq11 (0,0) (-10,0) => false
* eq12 (0,0) (10,0) => false
* eq13 (10,0) (-10,0) => false
* eq14 (10,0) (0,0) => false
* eq15 (-10,0) (-10,2) => false
* eq16 (10,0) (10,2) => false
*
* Invalid objects
* eq17 (10,2) null => false
* eq18 (10,2) "junk" => false
* eq19 (10,2) (10,-2) => NegativeExponent
* eq20 (0,0) null => false
*
* Testing Zero
* eq21 Test.Zero (0,1) => true
* eq22 Test.Zero (0,0) => true
*/
@Test
public void eq01() throws TError { assertTrue(new Term(-10,0).equals(new Term(-10,0))); }
@Test
public void eq02() throws TError { assertTrue(new Term(0,0).equals(new Term(0,0))); }
@Test
public void eq03() throws TError { assertTrue(new Term(0,0).equals(new Term(0,2))); }
@Test
public void eq04() throws TError { assertTrue(new Term(10,0).equals(new Term(10,0))); }
@Test
public void eq05() throws TError { assertTrue(new Term(-10,2).equals(new Term(-10,2))); }
@Test
public void eq06() throws TError { assertTrue(new Term(0,2).equals(new Term(0,0))); }
@Test
public void eq07() throws TError { assertTrue(new Term(0,2).equals(new Term(0,2))); }
@Test
public void eq08() throws TError { assertTrue(new Term(10,2).equals(new Term(10,2))); }
@Test
public void eq09() throws TError { assertFalse(new Term(-10,0).equals(new Term(0,0))); }
@Test
public void eq10() throws TError { assertFalse(new Term(-10,0).equals(new Term(10,0))); }
@Test
public void eq11() throws TError { assertFalse(new Term(0,0).equals(new Term(-10,0))); }
@Test
public void eq12() throws TError { assertFalse(new Term(0,0).equals(new Term(10,0))); }
@Test
public void eq13() throws TError { assertFalse(new Term(10,0).equals(new Term(-10,0))); }
@Test
public void eq14() throws TError { assertFalse(new Term(10,0).equals(new Term(0,0))); }
@Test
public void eq15() throws TError { assertFalse(new Term(-10,0).equals(new Term(-10,2))); }
@Test
public void eq16() throws TError { assertFalse(new Term(10,0).equals(new Term(10,2))); }
@Test
public void eq17() throws TError { assertFalse(new Term(10,2).equals(null)); }
@Test
public void eq18() throws TError { assertFalse(new Term(10,2).equals("junk")); }
@Test(expected = NegativeExponent.class)
public void eq19() throws TError { assertFalse(new Term(10,2).equals(new Term(10,-2))); }
@Test
public void eq20() throws TError { assertFalse(new Term(0,0).equals(null)); }
@Test
public void eq21() throws TError { assertEquals(Term.Zero, new Term(0,1)); }
@Test
public void eq22() throws TError { assertEquals(Term.Zero, new Term(0,0)); }
/* Check the predicate functions
*
* zero1 isZero(zero) => true
* zero2 isZero(unit) => false
* zero3 isZero((0,5)) => true
* zero4 isZero((5,0)) => false
*/
@Test
public void zero1() throws TError { assertTrue(Term.Zero.isZero()); }
@Test
public void zero2() throws TError { assertFalse(Term.Unit.isZero()); }
@Test
public void zero3() throws TError { assertTrue(new Term(0,5).isZero()); }
@Test
public void zero4() throws TError { assertFalse(new Term(5,0).isZero()); }
/*
* neg1 isNegative(zero) => false
* neg2 isNegative(unit) => false
* neg3 isNegative(-1,0) => true
* neg4 isNegative(min,2) => true
* neg5 isNegative(max,2) => false
*/
@Test
public void neg1() throws TError { assertFalse(Term.Zero.isNegative()); }
@Test
public void neg2() throws TError { assertFalse(Term.Unit.isNegative()); }
@Test
public void neg3() throws TError { assertTrue(new Term(-1,2).isNegative()); }
@Test
public void neg4() throws TError { assertTrue(new Term(Integer.MIN_VALUE,2).isNegative()); }
@Test
public void neg5() throws TError { assertFalse(new Term(Integer.MAX_VALUE,2).isNegative()); }
/*
* const1 isConstant(zero) => true
* const2 isConstant(unit) => true
* const3 isConstant(0,5) => true
* const4 isConstant(5,1) => false
*/
@Test
public void const1() throws TError { assertTrue(Term.Zero.isConstant()); }
@Test
public void const2() throws TError { assertTrue(Term.Unit.isConstant()); }
@Test
public void const3() throws TError { assertTrue(new Term(0,5).isConstant()); }
@Test
public void const4() throws TError { assertFalse(new Term(5,1).isConstant()); }
}
3) this is the toostring test class i am stuck on
package poly;
import static org.junit.Assert.*;
import org.junit.Test;
// Version 3
// Updated test ts03 to include the correct string assertEquals("-7x^2", new Term(-7,2).toString() );
public class ToStringTest
{
/* Testing the toString method
*
* ts01 (-7,0) => "-7"
* ts02 (-7,1) => "-7x"
* ts03 (-7,2) => "-7x^2"
*
* ts04 (-1,0) => "-1"
* ts05 (-1,1) => "-x"
* ts06 (-1,2) => "-x^2"
*
* ts07 (0, 0) => "0"
* ts08 (0, 1) => "0"
* ts09 (0, 2) => "0"
*
* ts10 (1,0) => "1"
* ts11 (1,1) => "x"
* ts12 (1,2) => "x^2"
*
* ts13 (7,0) => "7"
* ts14 (7,1) => "7x"
* ts15 (7,2) => "7x^2"
*/
@Test
public void ts01() throws TError { assertEquals("-7", new Term(-7,0).toString() ); }
@Test
public void ts02() throws TError { assertEquals("-7x", new Term(-7,1).toString() ); }
@Test
public void ts03() throws TError { assertEquals("-7x^2", new Term(-7,2).toString() ); }
@Test
public void ts04() throws TError { assertEquals("-1", new Term(-1,0).toString() ); }
@Test
public void ts05() throws TError { assertEquals("-x", new Term(-1,1).toString() ); }
@Test
public void ts06() throws TError { assertEquals("-x^2", new Term(-1,2).toString() ); }
@Test
public void ts07() throws TError { assertEquals("0", new Term(0,0).toString() ); }
@Test
public void ts08() throws TError { assertEquals("0", new Term(0,1).toString() ); }
@Test
public void ts09() throws TError { assertEquals("0", new Term(0,2).toString() ); }
@Test
public void ts10() throws TError { assertEquals("1", new Term(1,0).toString() ); }
@Test
public void ts11() throws TError { assertEquals("x", new Term(1,1).toString() ); }
@Test
public void ts12() throws TError { assertEquals("
^2", new Term(1,2).toString() ); }
@Test
public void ts13() throws TError { assertEquals("7", new Term(7,0).toString() ); }
@Test
public void ts14() throws TError { assertEquals("7x", new Term(7,1).toString() ); }
@Test
public void ts15() throws TError { assertEquals("7x^2", new Term(7,2).toString() ); }
}
3)finally the other class i am stuck on ..
package poly;
// Version 3
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class NegateScaleTest
{
private int min = Integer.MIN_VALUE;
private int max = Integer.MAX_VALUE;
/* Testing the negate method
*
* Fix a standard exponent (use 2)
*
* neg1 (min,2) => Overflow
* neg2 (-7,2) => (7,2)
* neg3 (0,2) => (0,2)
* neg4 (7,2) => (-7,2)
* neg5 (max,2) => (-max,2)
*/
@Test (expected = CoefficientOverflow.class)
public void negate1() throws TError { new Term(min,2).negate(); }
@Test
public void negate2() throws TError { assertEquals(new Term(7,2), new Term(-7,2).negate()); }
@Test
public void negate3() throws TError { assertEquals(Term.Zero, new Term(0,2).negate()); }
@Test
public void negate4() throws TError { assertEquals(new Term(-7,2), new Term(7,2).negate()); }
@Test
public void negate5() throws TError { assertEquals(new Term(-max,2), new Term(max,2).negate()); }
/* Testing the scalar multiply method
*
* Fix a standard exponent (use 2)
*
* Input Domain Modelling:
* Choose coefficient values:
* min
* -7
* -1
* 0
* 1
* 7 <- typical value
* max
*
* Choose multiplier values:
* min
* -10
* -1
* 0
* 1
* 10 <- typical value
* max
*
* scale01 (7,2) * min => Overflow
* scale02 (7,2) * -10 => (-70,2)
* scale03 (7,2) * -1 => (-7,2)
* scale04 (7,2) * 0 => 0
* scale05 (7,2) * 1 => (7,2)
* scale06 (7,2) * 10 => (70,2)
* scale07 (7,2) * max => Overflow
*
* scale08 (min,2) * 10 -> Overflow
* scale09 (-7,2) * 10 -> (-70,2)
* scale10 (-1,2) * 10 -> (-10,2)
* scale11 (0,2) * 10 -> 0
* scale12 (1,2) * 10 -> (10,2)
* scale13 (7,2) * 10 -> (70,2)
* scale14 (max,2) * 10 -> Overflow
*/
@Test (expected = CoefficientOverflow.class)
public void scale01() throws TError { new Term(7,2).scale(min); }
@Test
public void scale02() throws TError { assertEquals(new Term(-70,2), new Term(7,2).scale(-10)); }
@Test
public void scale03() throws TError { assertEquals(new Term(-7,2), new Term(7,2).scale(-1)); }
@Test
public void scale04() throws TError { assertEquals(Term.Zero, new Term(7,2).scale(0)); }
@Test
public void scale05() throws TError { assertEquals(new Term(7,2), new Term(7,2).scale(1)); }
@Test
public void scale06() throws TError { assertEquals(new Term(70,2), new Term(7,2).scale(10)); }
@Test (expected = CoefficientOverflow.class)
public void scale07() throws TError { new Term(7,2).scale(max); }
@Test (expected = CoefficientOverflow.class)
public void scale08() throws TError { new Term(min,2).scale(10); }
@Test
public void scale09() throws TError { assertEquals(new Term(-70,2), new Term(-7,2).scale(10)); }
@Test
public void scale10() throws TError { assertEquals(new Term(-10,2), new Term(-1,2).scale(10)); }
@Test
public void scale11() throws TError { assertEquals(Term.Zero, new Term(0,2).scale(10)); }
@Test
public void scale12() throws TError { assertEquals(new Term(10,2), new Term(1,2).scale(10)); }
@Test
public void scale13() throws TError { assertEquals(new Term(70,2), new Term(7,2).scale(10)); }
@Test (expected = CoefficientOverflow.class)
public void scale14() throws TError { new Term(max,2).scale(10); }
/*
* Output Domain Modellng:
* Output coefficient values:
* min
* min+1
* -10
* -1
* 0
* 1
* 10
* max-1
* max
*
* scale15 (min,2) * 1 => (min,2)
* scale16 (min/2,2) * 2 => (min,2)
* scale17 (min+1,2) * 1 => (min+1,2)
* scale18 (2,2) * -5 => (-10,2)
* scale19 (1,2) * -1 => (-1,2)
* scale20 (0,2) * min => 0 ( experiential test - "error guessing")
* scale21 (0,2) * max => 0 ( ditto )
* scale22 (0,2) * 0 => 0 ( ditto )
* scale23 (0,2) * 1 => 0 ( ditto )
* scale24 (1,2) * 1 => (1,2)
* scale25 ((max-1)/14) * 14 => (max-1,2)
* scale26 (max,2) * 1 => (max,2)
*/
@Test
public void scale15() throws TError { assertEquals(new Term(min,2), new Term(min,2).scale(1)); }
@Test
public void scale16() throws TError { assertEquals(new Term(min,2), new Term(min/2,2).scale(2)); }
@Test
public void scale17() throws TError { assertEquals(new Term(min+1,2), new Term(min+1,2).scale(1)); }
@Test
public void scale18() throws TError { assertEquals(new Term(-10,2), new Term(2,2).scale(-5)); }
@Test
public void scale19() throws TError { assertEquals(new Term(-1,2), new Term(1,2).scale(-1)); }
@Test
public void scale20() throws TError { assertEquals(Term.Zero, new Term(0,2).scale(min)); }
@Test
public void scale21() throws TError { assertEquals(Term.Zero, new Term(0,2).scale(max)); }
@Test
public void scale22() throws TError { assertEquals(Term.Zero, new Term(0,2).scale(0)); }
@Test
public void scale23() throws TError { assertEquals(Term.Zero, new Term(0,2).scale(1)); }
@Test
public void scale24() throws TError { assertEquals(new Term(1,2), new Term(1,2).scale(1)); }
@Test
public void scale25() throws TError { assertEquals(new Term(max-1,2), new Term((max-1)/14,2).scale(14)); }
@Test
public void scale26() throws TError { assertEquals(new Term(max,2), new Term(max,2).scale(1)); }
}
Thank you In advance for Everyones Help
:)