Question

In: Computer Science

Write a method (in Java) that will return output as stated below: toThePowerOf(int): Applies exponentiation to...

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
   }

Solutions

Expert Solution

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();
    }
}

Related Solutions

how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Java Write a method, makeUserName, that is passed two Strings and an int: the first is...
Java Write a method, makeUserName, that is passed two Strings and an int: the first is a first name, the second is a last name, and the last is a random number. The method returns a user name that contains the last character of the first name, the first five characters of the last name (assume there are five or more characters in last name), and the random number. An example: Assume that “John”, “Smith”, 45, are passed when the...
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum...
Just that method --> Java code Write a method “int sumPos(List aList)” to calculate the sum of positive integers in an ADT List aList of integers using ADT List operations. ADT List operations: isEmpty(), size(), add(index, item), remove(index), get(index), and removeAll(). You should not assume how an ADT List is implemented. Using array indexing, head/tail references, or any operation not defined in ADT List is not allowed.
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
5. Write a method to accept five int parameters and return the largest of the five.  ...
5. Write a method to accept five int parameters and return the largest of the five.         Example: largest(45, 13, 65, 19, 23) returns 65. Java Program (METHOD ONLY!!!)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT