In: Computer Science
Write a method (in Java) that will return output as stated below:
toThePowerOf(int): Applies exponentiation to the existing PolyTerm. For example, 2.4x^3 to the power of 3 should return 13.824x^9 while 2.4x^3 to the power of -3 should return 0.0723x^-9
Respective JUnit test for this question:
public void testToThePowerOf() {
       assertEquals(1,
t1.toThePowerOf(3).coefficient, 0.0001);
       assertEquals(3,
t1.toThePowerOf(3).exponent);
       assertEquals(13.824,
t2.toThePowerOf(3).coefficient, 0.0001);
       assertEquals(9,
t2.toThePowerOf(3).exponent);
       assertEquals(0.0723,
t2.toThePowerOf(-3).coefficient, 0.0001);
       assertEquals(-9,
t2.toThePowerOf(-3).exponent);
       assertEquals(-0.2962,
t3.toThePowerOf(-3).coefficient, 0.0001);
       assertEquals(0,
t3.toThePowerOf(-3).exponent);
       assertEquals(46.656,
t4.toThePowerOf(3).coefficient, 0.0001);
       assertEquals(-6,
t4.toThePowerOf(3).exponent);
       assertEquals(0.0016,
t4.toThePowerOf(-5).coefficient, 0.0001);
       assertEquals(10,
t4.toThePowerOf(-5).exponent);
       currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
   }
Main Function:
public class PolyTermTest {
   public static int score = 0;
   public static String result = "";
   public static String currentMethodName = null;
   ArrayList methodsPassed = new ArrayList();
  
   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
   }
Hi, I have added the function implementation along with relevant comments to help you understand better. I ran the function with the above test cases mentioned. It is passing all of the tests. Let me know if you need more help in understanding. I am attaching the PolyTerm class and PolytermTest class below.
public class PolyTerm {
    public double coefficient;
    public int exponent;
    public PolyTerm(double coefficient, int exponent) {
        this.coefficient = coefficient;
        this.exponent = exponent;
    }
    // returns the polyterm object after calculating the power
    public PolyTerm toThePowerOf(int value) {
        // the new coefficient will be old coefficient to the power given
        double newCoefficient = power(this.coefficient, value);
        // the new exponent will be old exponent times the power given
        int newExponent = this.exponent * value;
        // returning the updated PolyTerm Object
        return new PolyTerm(newCoefficient, newExponent);
    }
    // calculates the power of a number
    public double power(double number, int power) {
        double result = 1;
        if (power != 0) {
            int absolutePower = power;
            if (power < 0)
                absolutePower = power * (-1);
            // calculate the power considering power is positive
            for (int i = 1; i <= absolutePower; i++) {
                result *= number;
            }
            // if power is negative, invert
            if (power < 0) {
                result = 1.0 / result;
            }
        } else {
            result = 1;
        }
        return result;
    }
}
PolyTermTest.java
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import testingPOJO.PolyTerm;
public class PolyTermTest {
    public static int score = 0;
    public static String result = "";
    public static String currentMethodName = null;
    ArrayList methodsPassed = new ArrayList();
    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
    }
    @Test
    public void testToThePowerOf() {
        assertEquals(1, t1.toThePowerOf(3).coefficient, 0.0001);
        assertEquals(3, t1.toThePowerOf(3).exponent);
        assertEquals(13.824, t2.toThePowerOf(3).coefficient, 0.0001);
        assertEquals(9, t2.toThePowerOf(3).exponent);
        assertEquals(0.0723, t2.toThePowerOf(-3).coefficient, 0.0001);
        assertEquals(-9, t2.toThePowerOf(-3).exponent);
        assertEquals(-0.2962, t3.toThePowerOf(-3).coefficient, 0.0001);
        assertEquals(0, t3.toThePowerOf(-3).exponent);
        assertEquals(46.656, t4.toThePowerOf(3).coefficient, 0.0001);
        assertEquals(-6, t4.toThePowerOf(3).exponent);
        assertEquals(0.0016, t4.toThePowerOf(-5).coefficient, 0.0001);
        assertEquals(10, t4.toThePowerOf(-5).exponent);
        currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
    }
}