The Java Program: TPK.java

  1 // TPK.java -- Knuth's TPK program in Java
  2 
  3 import java.util.Scanner;                 // Input class
  4 import static java.lang.Math.*;           // Standard math library
  5 
  6 public class TPK {
  7 
  8    // f(x) = sqrt(|x|) + 5*x**3
  9    static double f (double x) {
 10       return sqrt (abs(x)) + 5.0 * pow(x,3.0);
 11    }
 12 
 13    public static void main (String args[])  {
 14       final double A[] = new double[11];
 15       final Scanner scan = new Scanner (System.in);
 16 
 17       // Read in the values of the array "A"; one per line
 18       for (int i=0; i<A.length; i++) {
 19          A[i] = scan.nextDouble ();
 20       }
 21 
 22       // In reverse order, apply "f" to each element of "A" and print
 23       for (int i=A.length-1; i>=0; i--) {
 24          final double y = f (A[i]);
 25          if (y > 400.0) {
 26             System.out.format ("%d  TOO LARGE%n", i);
 27          } else {
 28             System.out.format ("%d  %f%n", i, y);
 29          }
 30       }
 31    }
 32 }