Assignment 2

Say that you are interested in the value of the quadradic:

3X2 -8X + 4

for several values of X.

Write a program that has a double precision variable X. Assign a value to it. Write statement that computes a value for the quadradic and stores the result in another double precision variable. Finally write out the result, something like: At X = 4.0 the value is 20.0. Run the program with several values for X (edit the program for each value of X) and examine the result. Use values with decimal points, large values, small values, negative values, and zero. Solve the equation with paper and pencil (use the quadradic formula.) The quadradic should evaluate to zero at X = 2.0 and at X = 2/3. Try these values for X. Are the results exactly correct?

class Quadradic
{
   public static void main ( String[] args )
   {
    double x;
    double y;
    x = 2/3;
    y = 3 * x * x - 8 * x + 4;
    System.out.println("If x = " + x + " then the quadradic = " + y );
    x = 2.0;
    y = 3 * x * x - 8 * x + 4;
    System.out.println("If x = " + x + " then the quadradic = " + y );
    x = 4.0;
    System.out.println("If x = " + x + " then the quadradic = " + y );

    // 2/3 quadradic should be 0, but it doesn't because of interger math
    // integer math says that 3 cannot go a whole amount into 2, therefore
    // in integer match 2 divided by 3 equals 0
    // 3 * 0 * 0 - 8 * 0 + 4 = 4
    // 2.0 and 4.0 however do resolve to their expected quadradic of 0 and 20

   }
}