|
jschell wrote: 5. Go to 2.
Go to? You used a 'king go to! Back of the class and write out 100 times "Using GO TO is neither big nor clever and I will not do it again."
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
I also used the English language.
Perhaps you were confusing it with a programming language.
|
|
|
|
|
that was probably a homage to the old days when we all played with our C64's.
regards Torsten
I never finish anyth...
|
|
|
|
|
How do I add a javascript to an adobe livecycle form that will give me an enlarrged pop up image as I hover the mouse over an image.
|
|
|
|
|
Wrong forum, ask in JavaScript next-door.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
I have to do my first assignment for my java class for friday this week. So due in 3 days. I wrote the polynomial class but haven't done the driver for it yet as i don't know if my polynomial class is good. Please check and let me know if i have done correctly according to my assignment details (assignment pdf file can be downloaded here http://www.2shared.com/document/7YITxJL1/A1_online.html[^]). I am missing the numberOfTerms as i can't seem to figure that out.
Please help me out.
Polynomial.java
public class Polynomial
{
private int[] coeff = new int[5];
public Polynomial()
{
coeff = null;
}
public Polynomial(int c0, int c1, int c2, int c3, int c4)
{
this.coeff[0] = c4;
this.coeff[1] = c3;
this.coeff[2] = c2;
this.coeff[3] = c1;
this.coeff[4] = c0;
}
public Polynomial(Polynomial p)
{
coeff = p.coeff;
}
public int getCoeff(int i)
{
return this.coeff[i];
}
public boolean setCoef(int pos, int num)
{
if (pos <= 4 || pos >= 0 )
{
coeff[pos] = num;
return true;
}
else
return false;
}
public String toString()
{
return coeff[4]+"X4"+" + "+coeff[3]+"X3"+" + "+coeff[2]+"X2"+" + "+coeff[1]+"X"+" + "+coeff[0];
}
public boolean equals(Polynomial p)
{
if ((p.coeff[4] != coeff[4]) ||(p.coeff[3] != coeff[3]) ||(p.coeff[2] != coeff[2]) ||(p.coeff[1] != coeff[1]) ||(p.coeff[0] != coeff[0]) )
return false;
return true;
}
public Polynomial add(Polynomial p)
{
return (new Polynomial(p.coeff[4]+coeff[4],p.coeff[3]+coeff[3],p.coeff[2]+coeff[2],p.coeff[1]+coeff[1],p.coeff[0]+coeff[0]));
}
public Polynomial derive()
{
return (new Polynomial(coeff[4]*4,coeff[3]*3,coeff[2]*2,coeff[1],0));
}
public double evaluate(double x){
return coeff[4]*x*x*x*x+coeff[3]*x*x*x+coeff[2]*x*x+coeff[1]*x+coeff[0];
}
}
|
|
|
|
|
This forum is here to help with technical problems, not to provide a free program verification service. If your code does not work in some way then please explain where and why and people will try to help you.
|
|
|
|
|
The copy constructor should do a 'deep' copy:
public Polynomial(Polynomial p)
{
coeff = (int[])p.coeff.clone();
}
The toString method could be improved:
public String toString() {
return coeff[4] + "X^4 + " +
coeff[3] + "X^3 + " +
coeff[2] + "X^2 + " +
coeff[1] + "X + " + coeff[0];
}
The override of the equals method is wrong, the signature must be equals(Object) , a standard way to do this is:
public boolean equals(Object obj) {
if (obj == null || this.getClass() != obj.getClass()) {
return false;
}
final Polynomial other = (Polynomial)obj;
return this.coeff.equals(other.coeff);
}
Sums are hard, your evaluate method can be easily improved:
public double evaluate(double x){
return (coeff[4] * x^4) +
(coeff[3] * x^3) +
(coeff[2] * x^2) +
(coeff[1] * x) +
coeff[0];
}
Layout makes code easy to read.
Easy to read makes mistakes easier to spot.
Easier to spot mistakes makes better code.
Finally get in the habit NOW of writing JavaDoc comments for all public methods.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Nagy Vilmos wrote: return this.coeff.equals(other.coeff);
Does not work. The easiest way is to use Arrays.equals(a, b);
|
|
|
|
|
thank you very much everyone for helping me out. I will take everyones suggestions into consideration and I will start working on the driver now.
|
|
|
|
|
Nagy, the equals(Polynominal p) is requested that way!
the toString() is supposed to have a bit more functionality:
The method toString method will return the string containing the equation including only the
non-zero term.
...And comments are a requested point too: Include comments in your program describing the main steps in your program.
the "driver" is simply the main-method that rocks the party. You should have that one by now if you've written the rest.
public Polynomial(Polynomial p)
{
coeff = p.coeff;
}
That does not work - coeff is a private member. Yu need to use at least getCoeff(int i) - which returns a single int value. So that one can also not be assigned to your Array coeff[5].
public Polynomial()
{
coeff = null;
}
...equals kind of a head shot. Your Array is invalid from that point on as it is null.
Please use decent Hungarian Code conventions:
int iValue
Array oValues
Polynominal oPoly
As Nagy says, good structure is the job half way done.
regards Torsten
I never finish anyth...
|
|
|
|
|
TorstenH. wrote: Nagy, the equals(Polynominal p) is requested that way!
Then the person who set the paper is WRONG.
Seriously, why teach people bad practices it makes it harder for us when they becoming worker droids.
TorstenH. wrote: the toString() is supposed to have a bit more functionality:
The method toString method will return the string containing the equation including only the
non-zero term.
POP:
public String toString() {
StringBuilder sb = new StringBuilder();
for (int c = 0;
c < 5;
c++) {
if (this.couf[c] != 0) {
if (sb.length() > 0) {
if (this.couf[c] > 0) {
sb.append(" +");
} else {
sb.append(" ");
}
}
sb.append(this.couf[c]);
if (c > 0) {
sb.append("X");
if (c > 1) {
sb.append("^").append(c);
}
}
}
}
return sb.toString();
}
[Do you think teacher will notice it wasn't a stoodent who wrote this?]
TorstenH. wrote: That does not work - coeff is a private member.
You can access the member variables, even private ones, of another instance of the same class.
Also using an array clone is the most efficient way to do it.
I missed the blatant bug in the null constructor; good call.
TorstenH. wrote: Please use decent Hungarian Code conventions:
I would discourage that. With type safe languages, the need for Hungarian notation is superfluous.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Nagy Vilmos wrote: Also using an array clone is the most efficient way to do it.
It's not a question of efficiency.
coeff = other.coeff.clone();
does something fundamentally different from
coeff = other.coeff;
In fact you could argue that the second form is more efficient since it creates fewer objects at runtime. But in this case it is probably not what we want to do so it is just plain wrong, never mind whether it is efficient or not.
|
|
|
|
|
The second copies the array reference, a shallow copy, and means that changing the content in the new instance changes the old one too.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Yes, but what has that got to do with efficiency? I think you are confusing "efficiency" with "correctness".
|
|
|
|
|
The most efficient way to clone an array.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
clone is faster because it only does a shallow copy of the array. So if you are working with a one-dimensional array of primitives (as in this case) you will get away with it. But for other cases it might not be what you want (two-dimensional arrays don't get cloned properly, for example). For copying arrays you're better off using Arrays copyOf or System arrayCopy. Again, it's a question of correctness not efficiency. Is it efficient to be fast but wrong?
|
|
|
|
|
It's a primitive array, that's why using a simple clone is the right approach in this case.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
So that's why you described clone as a "deep" copy in your original reply, then?
|
|
|
|
|
Okay, I sit corrected. Looking further Array.copy(...) is a better option.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
lol yea the teacher wants equals(Polynominal p) so is my way right or wrong?
|
|
|
|
|
looks good at a first glance - needs some fixes (read the comments).
How do you programm? Do you use a IDE like Eclipse or Netbeans?
regards Torsten
I never finish anyth...
|
|
|
|
|
Accessing private members that way? at least bad style.
my suggestion:
public Polynomial(Polynomial p)
{
coeff = p.getCoeff();
}
regards Torsten
I never finish anyth...
|
|
|
|
|
There is no method getCoeff() , just getCoeff(int) .
Seriously, I don't think accessing member variables in a constructor is necessarily bad, but I guess OP could go for the 'collapse all constructors into one' approach:
public Polynomial(Polynomial p)
{
this(p.getCoeff(0),
p.getCoeff(1),
p.getCoeff(2),
p.getCoeff(3),
p.getCoeff(4));
}
public Polynomial()
{
this(0, 0, 0, 0, 0);
}
public Polynomial(int c0, int c1, int c2, int c3, int c4)
{
this.coeff = new int[5];
this.coeff[0] = c0;
this.coeff[1] = c1;
this.coeff[2] = c2;
this.coeff[3] = c3;
this.coeff[4] = c4;
}
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
TorstenH. wrote: Please use decent Hungarian Code conventions:
Without opening a can of worms as to the pros and cons of Hungarian notation, I wouldn't teach a Java newbie to use Hungarian for the simple reason that most Java coders do not use it so it will just confuse people. Sun published a set of standards years ago which still pretty much form the basis of 99.9% of java shops' coding conventions in the real world, so I would advise newbies to go and read that and follow it.
|
|
|
|