Question

In: Computer Science

Write a method that returns the result when the calling object is multiplied by a scalar...

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

Solutions

Expert Solution

Code to paste

public PolyTerm scalarMultiply(double multiplier){
        return new Polyterm(coefficient * multiplier,exponent);

}

Driver program and screen shot


Related Solutions

A vector has components (4,2). When the vector is multiplied by the scalar 9, how does...
A vector has components (4,2). When the vector is multiplied by the scalar 9, how does its magnitude and direction change?
Method calling in c# I need to write methods for calling the min and max numbers...
Method calling in c# I need to write methods for calling the min and max numbers using this code; Console.WriteLine("Calling highest method."); Console.WriteLine("Highest number is: {0}", highest(3)); Console.WriteLine("Calling lowest method."); Console.WriteLine("Lowest number is: {0}", lowest(3));
1) What's the result of calling method blitz passing strings "Aquamarine" as the first argument and...
1) What's the result of calling method blitz passing strings "Aquamarine" as the first argument and "Heliotrope" as the second argument? static int blitz(String v, String w) { if (v.length() != w.length()) return 0; int c = 0; for (int i = 0; i < v.length(); i++) if (v.charAt(i) == w.charAt(i)) c++; return c; } a)0 b)1 c)2 d) 3 2)What is NOT an advantage of dynamic arrays compared to static arrays? a)new elements can be added b)elements can be...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with the content from the original array, modified as follows: 1) If the number in the original array is divisible by 3, the corresponding element in the new array should be a string ‘fizz’. 2) If the number in the original array is divisible by 5, the corresponding element in the new array should be a string ‘buzz’. 3) If the number in the original...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
C Program: Write your own stringLength function (without calling a pre-written library function) that returns the...
C Program: Write your own stringLength function (without calling a pre-written library function) that returns the length of the string argument in characters, not including the terminating ‘\0’ character.   Write four versions of a void printString function (i.e., printString1, printString2, etc) each of which prints its string argument character-by-character but using the following techniques: printString1: array indexing printString2: pointer/offset with the array name as a pointer, printString3: pointer indexing, and printString4: pointer/offset with a pointer Write a void function called...
In the chain ratio method of estimating demand, a base number is multiplied by a chain...
In the chain ratio method of estimating demand, a base number is multiplied by a chain of adjusting percentages. The upper limit of market demand is called market potential. The % market that willing and able to buy is available market under the current conditions. ZIVAGO sells HDTV and a recent marketing research survey found the following information: Number of US households: 230,000 Access to HD Internet = 65% of the households. Among the households with access to HD internet,...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
​Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.
Write a recursive method, vowels, that returns the number of vowels in a string. Also, write a program to test your method.(JAVA Code)
Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT