Wednesday, May 30, 2012

Programming : Polynomial with Honor method


 * This program gets coefficient and degree from a user
 * and calculate it with Horner's method with an user input
/* The output looks like this

 * 1st:
Enter the degree of the polynomial 2
Enter the coefficient of the polynomial 1
Enter the coefficient of the polynomial 2
Enter the coefficient of the polynomial 5
Input the value: -2
The result is 5
e.g : (X^2 + 2X + 5), plug in with -2 , then (-2)^2 +2(-2) + 5 => 5
*/


/**
 * @author      Hun Wie (Theo)<skyhuni82@hotmail.com>
 * @date        2011.0224
 */

package honor;
import java.util.*;

/**
 * This program gets coefficient and degree from a user
 * and calculate it with Horner's method with an user input
 *
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        {      
            Scanner in = new Scanner (System.in);
            int degree = -1;
            boolean isCorrectDegree = false; // Assume an error
            while (!isCorrectDegree) { // loop while the entered value > 1
            try {
                  System.out.print("Enter the degree of the polynomial ");
                  degree  = in.nextInt();
                  if ( degree >= 0)
                  {
                    isCorrectDegree = true; // integer has been entered
                  }
                  else
                  {
                      System.out.print("You entered degree <0 ");
                      isCorrectDegree = false;
                  }
                }
            catch (Exception e)
                {
                  System.out.println("You entered wrong input");
                  in.nextLine();
                }
             }//end of while
         
         

         
       
            int [] array = new int [degree + 1];
     
            for ( int i = array.length -1 ; i >= 0; i--)
            {
             
                boolean isCorrectCoefficient = false; // Assume an error
                while (!isCorrectCoefficient) { // loop while the entered value > 1
                try {
                      System.out.print("Enter the coefficient of the polynomial ");
                      array [i] = in.nextInt();
                      isCorrectCoefficient = true; // integer has been entered

                    }
                catch (Exception e)
                    {
                      System.out.println("You entered wrong input");
                      in.nextLine();
                    }
                 }//end of while
            }// enf of for
            Polynomial p = new Polynomial(array);
            // test the Polynomial with input of x
            System.out.print("Input the value: ");
            int x = in.nextInt();
            int result = p.evaluate(x);
            System.out.println("The result is " + result);
        }
    }
 
   

}



//This is Polynomial Class


package honor;

/**
 *
 * @author Theo
 */
  public class Polynomial{
        int [] array;
        int order;
    // setter
    public void setArray(int[] array) {
        this.array = array;
    }
    // getter
    public int[] getArray() {
        return array;
    }

   Polynomial( int [] array )
   {        this.array = array;
            order = array.length - 1;
   }
   //This is Horner's method.
   int evaluate(int  x)
    {
       int result = array[order];
       for (int i = order - 1 ; i >= 0 ; --i )
       {
            result = result * x + array[i];

        }
        return result;
    }
}



No comments:

Post a Comment