A Working (but useless) Program

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:

The answer is given below.

A Working (but useless) Program

With the blanks filled in the program can be compiled and run. It doesn't do much, but the basic user interaction is complete. One of the aspects of the complete program is finished. Since it is finished you should test it. If something is wrong you can discover the problem and fix it before proceeding.

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
    String response = "y";         // "y" or "n"

    while ( response.equals( "y" ) )    
    {
       // Get a value for x.

       // Evaluate the polynomial.

       // Print out the result.

       // Ask the user if the program should continue.
       // The user's answer is "response".
       System.out.println("continue (y or n)?");

       response = scan.nextLine();      
    }

  }
}

Here is an example dialog:

continue (y or n)?
y
continue (y or n)?
y
continue (y or n)?
n

As with all loops, there were three things to get right:

  1. Initializing the loop.
  2. Testing if the loop should continue.
  3. Setting up for the next test.

In order for this loop to work correctly, the initial value of response was initialized to "y".

QUESTION 18:

Why is this loop a sentinel-controlled loop?