Evaluating the Polynomial

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

See below.

Evaluating the Polynomial

Here is the program with the part for reading x filled in:

import java.io.*;

class EvalPoly
{
  public static void main (String[] args )  
  {
    Scanner scan = new Scanner ( System.in );

    double x;                      // a value to use with the polynomial
    double result;                 // result of evaluating the polynomial at x
    String response = "y";         // "y" or "n"

    while ( response.equals( "y" ) )    
    {
       // Get a value for x.
       System.out.println("Enter a value for x:") ;
       x = scan.nextDouble();

       // Evaluate the polynomial.
       result = ;   

       ;

       // Ask the user if the program should continue.
       System.out.println("continue (y or n)?");
       response = scan.nextLine();      
    }

  }
}

Remember what the problem is: You are interested in the value of the polynomial

7x3- 3x2 + 4x - 12

for various values of x. Only two blanks remain to complete the program.

QUESTION 20:

Complete the program by filling in the blanks.