473,728 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Derivative method for class polynomial

3 New Member
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 6494
JosAH
11,448 Recognized Expert MVP
What is an UnorderedArrayL ist? 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
9599
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, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
10
2134
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 an argument. Feel free to point me into the bowels of google if this has been discussed to death already. super(self).method() seems like super could just do the right thing...
11
3623
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: virtual void do_bar() {} };
9
5972
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 #define Polynomial_h class Polynomial
1
3648
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
14433
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 the class and this works but I would also like to know of other ways to do this. Also are there any performance implacations of using sealed?
15
12788
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" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
4
1720
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
1390
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') .... @classmethod .... def f(cls): .... print('class method')
0
8891
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9409
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8110
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6012
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2642
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2159
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.