go to previous page   go to home page   go to next page hear noise highlighting

Answer:

The complete program is given below.


Complete Program

Translating the polynomial

7x3- 3x2 + 4x - 12

into Java is easy. The term x3 means x times itself three times, so that is what the Java statement does.

The next statement writes out the answer. The + operator concatenates the strings. Remember that doubles are converted to strings automatically when you use + between a double and a string.


import java.util.Scanner;

public 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 =7*x*x*x - 3*x*x + 4*x - 12;

       // Print out the result.
       System.out.println("The value of the polynomial at x = " +
           x + " is: " + result + "\n"  ) ;

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

  }
}


QUESTION 21:

What happens in this program if the user types "yes" in response to the prompt continue (y or n)?


go to previous page   go to home page   go to next page