Forums Register Login

A little help please...should be easy

+Pie Number of slices to send: Send
Hi, I need a little push in the right direction. I have to write a polynomial class that adds, subtracts and multiplies polynomials. This is my code so far for the class. My tester is below. It says the constructor is undefined but I don't know how to define it. Also I have to use arrays and have no idea how to start off...any help would be appreciated. I don't need code, maybe just a few examples pertaining to my problem. I just need a push in the right direction.




[ October 09, 2008: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
In your Polynomial class you have the following constructor defined:

public Polynomial(int[] coefficients)

In your PolynomialTester class you use the following constructor:

new Polynomial();

Hmmm... I wonder what coefficients the tester class wants to use.
+Pie Number of slices to send: Send
hi dude,
i think the problem is there is no zero argument constructor.Since you already defined an argumented constructor comppiler will not add the zero argument constructor as in the case of no constructors.


hope you got it
+Pie Number of slices to send: Send
can I ask this...Does the code for the polynomial make sense so far? In my head I can't see the logic of how to write the add method. What does it mean...



...like what is it asking for? In the (Polynomial op2) is that creating a new polynomial or what? I'm not used to seeing that.

Oh, so what you're trying to say is that I need a zero Polynomial() too? What could be included if you don't mind me asking?
[ October 09, 2008: Message edited by: Jamess Davis ]
+Pie Number of slices to send: Send
Is it logical to create a polynomial with no coefficients? If no, then you oughtn't to have a zero-args constructor.
Yes it is logical to pass Polynomial op2 as a parameter; that has to be a polynomial you created elsewhere.
The problem is that in the main method you are calling a polynomial constructor with no arguments; that constructor doesn't exist, so you have to change that invocation with parameters to match, eg new Polynomial(new int[]{1, 4, 6, 6, 4, 1}).
[ October 10, 2008: Message edited by: Campbell Ritchie ]
+Pie Number of slices to send: Send
Thanks Campbell that helped out a little bit. My problem now is in my polynomial class. I don't understand how you're supposed to pass the Polynomial method as a parameter and use it in the actual equation.

Should I create an array within the add() method and work from there? Is the code in the constructor correct? Man i'm so confused...
+Pie Number of slices to send: Send
 

Originally posted by Jamess Davis:
Thanks Campbell that helped out a little bit. You're welcome

Is the code in the constructor correct? Afraid not, no



The idea of a constructor is to set up the initial state of the object. What you are doing is taking the array passed, then adding 1 to each of the members. What you want for a polynomial is simply to record an array of numbers, like 1, 4, 6, 4, 1. You can set up a new array the same size as the array passed, then copy all the values into the new array. Remember to use the length parameter of the old array.

[There is a quicker way to do this, but I am intentionally keeping quiet about it.]

Suggest you get one bit working, then try the next bit. Remember you eat an elephant one mouthful at a time!
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1177 times.
Similar Threads
Using interface as a type
ArrayList - how to store objects of certain kind
graph plotting
int can not be dereferenced error
Help with "Missing return Statement" error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:23:01.