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...
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...
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....
[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 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);...
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....
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT