Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.03 KB | None | 0 0
  1.  
  2. import java.io.*;
  3.  
  4. public class Polynomial extends UnorderedArrayList
  5. {
  6.  
  7.         //Default constructor
  8.         //Postcondition: An array of the size 100, and
  9.         //             length and maxSize are set to 100
  10.     public Polynomial()
  11.     {
  12.          super();
  13.          length = 100;
  14.     }
  15.  
  16.         //Constructor with parameter
  17.         //Postcondition: An array of the size specified by
  18.         //      the parameter size is created, length and maxSize
  19.         //      are initialized to size
  20.     public Polynomial(int size)
  21.     {
  22.          super(size);
  23.          length = size;
  24.     }
  25.  
  26.         //copy constructor
  27.     public Polynomial(Polynomial right)
  28.     {
  29.          super(right);
  30.     }
  31.  
  32.  
  33.         //Method to copy the coefficients of the polynomial
  34.         //specified by the parameter right
  35.         //Postcondition: The coefficients of the polynomial
  36.         //               specified by right are copied.
  37.     public void copy(Polynomial right)
  38.     {
  39.         super.copyList(right);
  40.     }
  41.  
  42.         //Method to evaluate a polynomial at a given value
  43.         //Postcondition: The polynomial is evaluated at x and
  44.         //               the value is returned
  45.     public double evaluate(double x)
  46.     {
  47.         double value = 0.0;
  48.  
  49.         DoubleElement coeff = new DoubleElement();
  50.  
  51.         for(int i = 0; i < length; i++)
  52.         {
  53.             coeff.makeCopy((DoubleElement) list[i]);
  54.  
  55.             if(coeff.getNum() != 0.0)
  56.                value = value + coeff.getNum() * Math.pow(x,i);
  57.         }
  58.  
  59.         return value;
  60.     }
  61.  
  62.  
  63.         //Method to add two polynomials
  64.         //Postcondition: This polynomial is added with the polynomial
  65.         //          specified by the parameter right. A reference of
  66.         //          the result is returned.
  67.     public Polynomial add(Polynomial right)
  68.     {
  69.         int size = max(length, right.length);
  70.         int i;
  71.         double sumCoeff;
  72.  
  73.         DoubleElement coeffP = new DoubleElement();
  74.         DoubleElement coeffQ = new DoubleElement();
  75.  
  76.         DoubleElement z;
  77.  
  78.         Polynomial temp = new Polynomial(size);
  79.  
  80.         for(i = 0; i < min(length, right.length); i++)
  81.         {
  82.             coeffP.makeCopy((DoubleElement) list[i]);
  83.             coeffQ.makeCopy((DoubleElement) right.list[i]);
  84.  
  85.             sumCoeff = coeffP.getNum() + coeffQ.getNum();
  86.             z = new DoubleElement(sumCoeff);
  87.             temp.list[i] = z;
  88.         }
  89.  
  90.         if(size == length)
  91.              for(i = min(length, right.length); i < length; i++)
  92.                  temp.list[i] = list[i].getCopy();
  93.         else
  94.            for(i = min(length, right.length); i < right.length; i++)
  95.                temp.list[i] = right.list[i].getCopy();
  96.  
  97.         return temp;
  98.     }
  99.  
  100.         //Method to subtract two polynomials
  101.         //Postcondition: The polynomial specified by the
  102.         //          parameter right is subtracted from this
  103.         //          polynomial. A reference of the result is returned.
  104.     public Polynomial subtract(Polynomial right)
  105.     {
  106.         int size = max(length, right.length);
  107.         int i;
  108.         double diffCoeff;
  109.         double coeff;
  110.  
  111.         DoubleElement coeffP = new DoubleElement();
  112.         DoubleElement coeffQ = new DoubleElement();
  113.  
  114.         DoubleElement z;
  115.  
  116.         Polynomial temp = new Polynomial(size);
  117.  
  118.         for(i = 0; i < min(length, right.length); i++)
  119.         {
  120.             coeffP.makeCopy((DoubleElement) list[i]);
  121.             coeffQ.makeCopy((DoubleElement) right.list[i]);
  122.  
  123.             diffCoeff = coeffP.getNum() - coeffQ.getNum();
  124.             z = new DoubleElement(diffCoeff);
  125.             temp.list[i] = z;
  126.         }
  127.  
  128.         if(size == length)
  129.            for(i = min(length, right.length); i < length; i++)
  130.                temp.list[i] = list[i].getCopy();
  131.         else
  132.            for(i = min(length, right.length); i < right.length; i++)
  133.            {
  134.                z = new DoubleElement();
  135.                z.makeCopy((DoubleElement) right.list[i]);
  136.                coeff = z.getNum();
  137.                z.setNum(-coeff);
  138.                temp.list[i] = z;
  139.            }
  140.  
  141.         return temp;
  142.     }
  143.  
  144.         //Method to multiply two polynomials
  145.         //Postcondition: This polynomial is multiplied with the
  146.         //          polynomial specified by the parameter right. A
  147.         //          reference of the result is returned.
  148.     public Polynomial multiply(Polynomial right)
  149.     {
  150.  
  151.         Polynomial temp = new Polynomial();
  152.  
  153.         System.out.println("See Programming Exercise 5 at the end of the chapter.");
  154.  
  155.         return temp;
  156.     }
  157.  
  158.         //Method to read the coefficients of a polynomial
  159.     public void read() throws IOException
  160.     {
  161.         BufferedReader keyboard = new
  162.             BufferedReader(new InputStreamReader(System.in));
  163.  
  164.         DoubleElement x = new DoubleElement();
  165.  
  166.         System.out.println("The degree of this polynomial is: "
  167.                          + (length - 1));
  168.         for(int i = 0; i < length; i++)
  169.         {
  170.             System.out.print("Enter the coefficient of x^"
  171.                              + i + ": ");
  172.             System.out.flush();
  173.  
  174.             x.setNum(Double.parseDouble(keyboard.readLine()));
  175.  
  176.             list[i] = x.getCopy();
  177.         }
  178.     }
  179.  
  180.           //Method to return the string containing the polynomial
  181.     public String toString()
  182.     {
  183.         int i;
  184.         int firstNonzeroCoeff = 0;
  185.         DoubleElement x = new DoubleElement();
  186.         String str = "";
  187.  
  188.         for(i = 0; i < length; i++)
  189.         {
  190.             x.makeCopy((DoubleElement) list[i]);
  191.  
  192.             if(x.getNum() != 0.0)
  193.             {
  194.                firstNonzeroCoeff = i;
  195.                break;
  196.             }
  197.         }
  198.  
  199.         if(firstNonzeroCoeff < length)
  200.         {
  201.            if(firstNonzeroCoeff == 0)
  202.               str = list[firstNonzeroCoeff] + " ";
  203.            else
  204.               str = list[firstNonzeroCoeff] + "x^"
  205.                        + firstNonzeroCoeff + " ";
  206.  
  207.            for(i = firstNonzeroCoeff + 1; i < length; i++)
  208.            {
  209.                x.makeCopy((DoubleElement) list[i]);
  210.  
  211.                if(x.getNum() != 0.0)
  212.                   if(x.getNum() > 0.0)
  213.                      str += "+ " + x.getNum() + "x^" + i + " ";
  214.                   else
  215.                      str += "- " + -x.getNum() + "x^" + i + " ";
  216.            }
  217.         }
  218.  
  219.         return str;
  220.     }
  221.  
  222.         //Method to determine the smaller of x and y
  223.         //Postcondition: The smaller of x and y is returned
  224.     public int min(int x, int y)
  225.     {
  226.         if(x <= y)
  227.            return x;
  228.         else
  229.            return y;
  230.     }
  231.  
  232.         //Method to determine the larger of x and y
  233.         //Postcondition: The larger of x and y is returned
  234.     public int max(int x, int y)
  235.     {
  236.         if(x >= y)
  237.            return x;
  238.         else
  239.            return y;
  240.     }
  241.  
  242.  
  243.   // Write the definition of the method derivative
  244.   // to determine and return the derivative of a polynomial  
  245.     public Polynomial derivative()
  246.     {
  247.        
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement