In: Computer Science
Write a method that returns the result when the calling object
is multiplied by a scalar value.
For example, the PolyTerm 2.4x^3 multiplied by -1.2 should return
the PolyTerm object representing -2.88x^3.
Language: Java.
Method name be like: scalarMultiply(double)
Some Outputs:
Test 1: Coefficient =1, Exponent = 1
scalarMultiply(1.2).coefficient return 1.2;
scalarMultiply(1.2).exponent returns 1.
Test 2: Coefficient =2.4, Exponent = 3
scalarMultiply(-1.2).coefficient returns -2.88
scalarMultiply(-1.2).exponent return 3
Test 3: Coefficient =-1.5 Exponent = 0
scalarMultiply(0).coefficient returns 0
scalarMultiply(0).exponent returns 3
The actual question is:
PolyTerm t1, t2, t3, t4;
@BeforeEach
public void setUp() throws Exception {
currentMethodName = null;
t1 = new PolyTerm(1, 1); //x
t2 = new PolyTerm(2.4, 3);
//2.4x^3
t3 = new PolyTerm(-1.5, 0);
//-1.5
t4 = new PolyTerm(3.6, -2);
//3.6x^-2
}
And it's respective JUnit test:
@Test @Order(6) @Graded(marks=8,
description="scalarMultiply(double)")
public void testScalarMultiply() {
assertEquals(1.2,
t1.scalarMultiply(1.2).coefficient, 0.001);
assertEquals(1,
t1.scalarMultiply(1.2).exponent);
assertEquals(-2.88,
t2.scalarMultiply(-1.2).coefficient, 0.001);
assertEquals(3,
t2.scalarMultiply(-1.2).exponent);
assertEquals(0,
t2.scalarMultiply(0).coefficient, 0.001);
assertEquals(3,
t2.scalarMultiply(0).exponent);
assertEquals(-0.36,
t4.scalarMultiply(-0.1).coefficient, 0.001);
assertEquals(-2,
t4.scalarMultiply(-0.1).exponent);
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
Code to paste
public PolyTerm scalarMultiply(double multiplier){
return new
Polyterm(coefficient * multiplier,exponent);
}
Driver program and screen shot