473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Derivative method for class polynomial

Hi guys I have run in to a littl bit of trouble. I am writing a class called polynomial
in which i need a derivative method I have everything, just dont know how to start the derivative method. Any help will be appriciated here is the code:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class Polynomial extends UnorderedArrayList
  5. {
  6.         Scanner sc = new Scanner(System.in);
  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.         DoubleElement x = new DoubleElement();
  162.  
  163.         System.out.println("The degree of this polynomial is: "
  164.                          + (length - 1));
  165.         for(int i = 0; i < length; i++)
  166.         {
  167.             System.out.print("Enter the coefficient of x^"
  168.                              + i + ": ");
  169.  
  170.             double y;
  171.             x.setNum(y = sc.nextDouble());
  172.  
  173.             list[i] = x.getCopy();
  174.         }
  175.     }
  176.  
  177.           //Method to return the string containing the polynomial
  178.     public String toString()
  179.     {
  180.         int i;
  181.         int firstNonzeroCoeff = 0;
  182.         DoubleElement x = new DoubleElement();
  183.         String str = "";
  184.  
  185.         for(i = 0; i < length; i++)
  186.         {
  187.             x.makeCopy((DoubleElement) list[i]);
  188.  
  189.             if(x.getNum() != 0.0)
  190.             {
  191.                firstNonzeroCoeff = i;
  192.                break;
  193.             }
  194.         }
  195.  
  196.         if(firstNonzeroCoeff < length)
  197.         {
  198.            if(firstNonzeroCoeff == 0)
  199.               str = list[firstNonzeroCoeff] + " ";
  200.            else
  201.               str = list[firstNonzeroCoeff] + "x^"
  202.                        + firstNonzeroCoeff + " ";
  203.  
  204.            for(i = firstNonzeroCoeff + 1; i < length; i++)
  205.            {
  206.                x.makeCopy((DoubleElement) list[i]);
  207.  
  208.                if(x.getNum() != 0.0)
  209.                   if(x.getNum() > 0.0)
  210.                      str += "+ " + x.getNum() + "x^" + i + " ";
  211.                   else
  212.                      str += "- " + -x.getNum() + "x^" + i + " ";
  213.            }
  214.         }
  215.  
  216.         return str;
  217.     }
  218.  
  219.         //Method to determine the smaller of x and y
  220.         //Postcondition: The smaller of x and y is returned
  221.     public int min(int x, int y)
  222.     {
  223.         if(x <= y)
  224.            return x;
  225.         else
  226.            return y;
  227.     }
  228.  
  229.         //Method to determine the larger of x and y
  230.         //Postcondition: The larger of x and y is returned
  231.     public int max(int x, int y)
  232.     {
  233.         if(x >= y)
  234.            return x;
  235.         else
  236.            return y;
  237.     }
  238. }
  239.  
Oct 8 '08 #1
1 6453
JosAH
11,448 Expert 8TB
What is an UnorderedArrayList? Did you write your Polynomial code yourself?

kind regards,

Jos
Oct 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
10
by: Chris Green | last post by:
Good day, I've done a bit of searching in the language reference and a couple pages referring the behavior of super() but I can't find any discussion of why super needs the name of the class as...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
9
by: strotee76 | last post by:
What do I need to do to setTerm() for it to work properly? If you notice any other glaring issues, then please let me know. With this header file: ======================= #ifndef Polynomial_h...
1
by: Rubén Campos | last post by:
I've trying to implement polynomials of arbitrary order as a C++ template, as shown here: template <unsigned long int N> class Polynomial { public: Polynomial (); ~Polynomial ();
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
2
by: Sylvain Ferriol | last post by:
hello can you explain why python does not see difference between instance method and class method, having the same name example .... def f(self): .... print('instance method')...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.