Question

***Write a method in Java to Evaluate: ***For exp: object t2 has coefficient = 2.4 and...

***Write a method in Java to Evaluate:

***For exp: object t2 has coefficient = 2.4 and exponent = 3. it represents 2.4 * x^3. Then t2.evaluate(-1.5) is 2.4 * -1.5^3 which is -8.1.

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

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 @Order(2) @Graded(marks=8, description="evaluate(double)")

public void testEvaluate() {

assertEquals(1, t1.evaluate(1), 0.001); ///x=1

assertEquals(0, t1.evaluate(0), 0.001);

assertEquals(-1.5, t1.evaluate(-1.5), 0.001);

assertEquals(2.4, t2.evaluate(1), 0.001);///2.4x^3= 2.4*(1)^3

assertEquals(0, t2.evaluate(0), 0.001);

assertEquals(-8.1, t2.evaluate(-1.5), 0.001);

assertEquals(-1.5, t3.evaluate(1), 0.001);///-1.5x=-1.5*1=-1.5

assertEquals(-1.5, t3.evaluate(0), 0.001);

assertEquals(-1.5, t3.evaluate(-1.5), 0.001);

assertEquals(3.6, t4.evaluate(1), 0.001);

assertNull(t4.evaluate(0));

assertEquals(1.6, t4.evaluate(-1.5), 0.001);

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

2/ write a method in Java

@Test @Order(1) @Graded(marks=8, description="PolyTerm(double, int)")

public void testPolyTerm() {

assertEquals(1, t1.coefficient, 0.001);

assertEquals(1, t1.exponent);

assertEquals(2.4, t2.coefficient, 0.001);

assertEquals(3, t2.exponent);

assertEquals(-1.5, t3.coefficient, 0.001);

assertEquals(0, t3.exponent);

assertEquals(3.6, t4.coefficient, 0.001);

assertEquals(-2, t4.exponent);

currentMethodName = new Throwable().getStackTrace()[0].getMethodName();

}

Homework Answers

Answer #1

Here is the method evaluate :

float evaluate(float value)
{
float a = Math.pow(value,exponent);
float res = a*coefficient;
return res;
}

as per the given example it is clear that t2 is an object of a class where coefficient and exponent are two predefined variables and this method evaluate should be inside the same class so it can easily access the variables exponent and coefficient.

The java.lang.Math.pow() is used to calculate a number raise to the power of some other number. This function accepts two parameters and returns the value of first parameter raised to the second parameter.

*****WITHOUT MATH.POW( )*****

float evaluate(float value)

{

float a=1.0;

while(exponent>0)

{

a=a*value;

exponent--;

}

float res = a*coefficient;
return res;

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they...
Q. Write a method add(PolyTerm): Two PolyTerm objects can be added if and only if they have the same exponent. If the calling object can be added to the parameter object, return their sum, otherwise (if they can't be added), return null. For example 2x^3 + -7.3x^3 = -5.3x^3 while 2x^3 + 8x^2 should return null. (It's on JAVA) The JUnit Test for this question is: public class PolyTermTest {    public static int score = 0;    public static...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) →...
Write a function (java static method) that computes the maximum of two integer. maxOfTwo(1, 2) → 2 maxOfTwo(2, 1) → 2 maxOfTwo(1, 1) → 1 **(to start use): public int maxOfTwo(int a, int b) { And then complete the function (java static method)MaxOfThree so that it returns the maximum (highest) of the three int values passed as the parameters. maxOfThree(1, 2, 3) → 3 maxOfThree(0, 2, 1) → 2 maxOfThree(88, 4, 5) → 88 **(to start use): public int maxOfThree(int...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...
Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below: https://www.thoughtco.com/using-the-arraylist-2034204 import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + ".");...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
JAVA question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
JAVA question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...