|
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.
|
|
|
|
|
I know that most people do not use Hungarian Code Conventions for their code. But I think it makes programming especially for students simpler. Using clear naming for the variables and methods makes it much easier to deal with them.
regards Torsten
I never finish anyth...
|
|
|
|
|
As I said, I don't really want to get into the pros and cons of Hungarian notation. It's one of those religious discussions that usually descends into a pointless to-and-fro "my dad's bigger than your dad" kind of argument. It ranks somewhere alongside the question of whether opening braces should be placed on the same line or the following line.
|
|
|
|
|
naaa, it's not that kind of discussion.
I always see beginners using non-unique names for their variables and members like "b1, b2, b3, ..". It get's worse when it comes to list and labels, which are often named the same way: "l1, l2, l3, ...".
It'S so easy to get that cleared up! One can mess around when he's a pro and does code no one cares of. But as soon as a second person has to deal with the code, one should take a seconds time to think about.
EDIT: Check his new thread: http://www.codeproject.com/Messages/4032381/Polynomial-class-need-some-assistance-with-it.aspx[^]
He's using an int "coeff" in his main function and has the member Array[] "coeff" in the object he's dealing with. Technically allowed - but not helping at all.
regards Torsten
I never finish anyth...
|
|
|
|
|
Hello again I am creating again a new app in java which can animate images. I have problem with JRadionButton if I am going to select another one it marked with a dot and if I select another one it also marked with a dot without removing the pass which I had selected.
Here's my code
package star;
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class StarAnimate extends JFrame implements ItemListener {
JPanel panel = new JPanel();
JRadioButton btnBoard1 = new JRadioButton("Board 1", true);
JRadioButton btnBoard2 = new JRadioButton("Board 2");
JRadioButton btnBoard3 = new JRadioButton("Board 3");
Board board1 = new Board();
Board2 board2 = new Board2();
Board3 board3 = new Board3();
public StarAnimate() {
init();
}
private void init() {
btnBoard1.setBounds(5, 10, 100, 30);
btnBoard1.setBackground(Color.red);
btnBoard1.addItemListener(this);
btnBoard2.setBounds(5, 40, 100, 30);
btnBoard2.setBackground(Color.red);
btnBoard2.addItemListener(this);
btnBoard3.setBounds(5, 70, 100, 30);
btnBoard3.setBackground(Color.red);
btnBoard3.addItemListener(this);
panel.setLayout(null);
panel.setBounds(0, 0, 160, 540);
panel.setBackground(Color.red);
panel.add(btnBoard1);
panel.add(btnBoard2);
panel.add(btnBoard3);
this.add(panel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(560, 540);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setTitle("Star Animation");
this.setResizable(true);
this.setVisible(true);
}
public static void main(String[] args) {
StarAnimate star = new StarAnimate();
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == btnBoard1) {
this.add(board1);
this.remove(board2);
this.remove(board3);
}
if (e.getSource() == btnBoard2) {
this.add(board2);
this.remove(board1);
this.remove(board3);
}
if (e.getSource() == btnBoard3) {
this.add(board3);
this.remove(board2);
this.remove(board1);
}
}
}
|
|
|
|
|
Add your JRadioButton into a JRadioButtonGroup
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|
Lol this is it thanks. Anyway its just ButtonGroup not JRadioButtonGroup.
|
|
|
|
|
flashery wrote: Anyway its just ButtonGroup not JRadioButtonGroup.
Sorry about that, my brain's currently running in safe-mode (ie. debugging some VB apps)
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|