472,365 Members | 1,963 Online

# Derivative method for class polynomial 3
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.
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 6334 JosAH
11,448 Expert 8TB
What is an UnorderedArrayList? Did you write your Polynomial code yourself?

kind regards,

Jos
Oct 8 '08 #2

 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 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')... 2 by: Kemmylinns12 | last post by: Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies... 0 by: antdb | last post by: Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and... 0 by: Arjunsri | last post by: I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection... 1 by: Matthew3360 | last post by: Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a... 0 by: Oralloy | last post by: Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++... 0 by: Carina712 | last post by: Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important... 0 by: BLUEPANDA | last post by: At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also... 2 by: Ricardo de Mila | last post by: Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what... 1 by: ezappsrUS | last post by: Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...