Java Snippet: Evaluate a Polynomial

0
30

Description

Learn Java with simple programming examples.

This snippet evaluates the polynomial

Raising a number to a power (e.g., x3) is often represented in other programming languages with the caret symbol (^), meaning to the power of.

In Java, a number is raised to a power by successive multiplications

or with the built-in Math.pow() method.

We will use variables of type double throughout the program snippet because the Math.pow() method requires double rather than int. Raising the number three to the second power (i.e., 3^2) is expressed as

Source Code

  1. Type (or copy) the program shown below into your computer, compile it, and then run it.
  2. Modify the line double x = 3.0; to double x = 5.0; to evaluate the polynomial with a different value of x.
  3. What result do you expect? Use a calculator if you need to.
  4. Compile and run it to see if you were correct.

Compile and Run Screenshot

Screenshot of compiling and running EvaluatePolynomial.java

 

Leave a Reply