Page 1 of 1
Help with a program...
PostPosted: Mon Oct 26, 2009 3:04 pm
by Destroyer2000
My code seems to be wrong, but I can't figure it out, for whatever reason. Can anyone shed light on why it says, "setFeet() in Distance cannot be applied to (int)"?
public class Distance
{
private int feet;
private int inches;
public Distance()
{
feet = 0;
inches = 0;
}
public Distance(int feet, int inches)
{
this.feet = feet;
this.inches = inches;
}
public Distance(Distance distance)
{
distance.setFeet();
distance.setInches();
}
public int getFeet()
{
return feet;
}
public int getInches()
{
return inches;
}
public void setFeet(int feet)
{
feet = feet;
}
public void setInches(int inches)
{
this.inches = inches;
}
private int toInches()
{
return (distance.feet*12) + (distance.inches);
}
public void add(Distance dist)
{
}
public void add(int feet, int inches)
{
add(new Distance(feet, inches));
}
public String toString()
{
return (feet+"'"+inches+"\"");
}
}
PostPosted: Mon Oct 26, 2009 4:00 pm
by Davidizer13
Looks like you just missed the this. statement in the setFeet method. Try adding that and see what happens. Also, maybe it's just the way I code in Java, but you might want to make it so that the toInches method accepts a Distance object and make associated changes on the calculations you're doing with the variables.
PostPosted: Mon Oct 26, 2009 8:24 pm
by Whitefang
[quote="Destroyer2000 (post: 1352682)"]My code seems to be wrong, but I can't figure it out, for whatever reason. Can anyone shed light on why it says, "setFeet() in Distance cannot be applied to (int)"?
public Distance(Distance distance)
{
distance.setFeet()]
I believe this is your problem here. You probably meant to write setFeet(distance.getFeet()); ?
Anyway, the compiler looks for a setFeet() function, but finds a symbol setFeet(int) that takes an (int) argument, so it fails to compile. (In other words, setFeet() is not defined, but it is letting you know that setFeet(int) is defined.)
Also, and I'm no java expert, you probably want to change the names of your variables within the functions. For example, try rewriting setFeet(int feet) to:
public void setFeet(int feet_)
{
feet = feet_;
}
This way you and the program knows which int is which. It will also allow you not to have to use this.member_variable = member_variable; /*I suppose I could start a lesson on scope, but I'm worried I wouldn't explain it well enough*/
Also, try to refrain from using this to refer to members within the class. It's unnecessary and confusing. Only use it if you have to pass a reference to the object class (for example this is common when designing graphical user interfaces).
If any of this is confusing, let me know and I'll try to clarify. I hope it helps.
PostPosted: Tue Oct 27, 2009 8:25 am
by blkmage
Whitefang (post: 1352761) wrote:Also, and I'm no java expert, you probably want to change the names of your variables within the functions. For example, try rewriting setFeet(int feet) to:
public void setFeet(int feet_)
{
feet = feet_]this[/b] to refer to members within the class. It's unnecessary and confusing. Only use it if you have to pass a reference to the object class (for example this is common when designing graphical user interfaces).
Just to offer a different perspective, I find using
this a lot less confusing than appending or prefixing local variables or member variables with underscores. I also find it's a lot less error prone and easier to read. For me, explicitly using
this is an easy indicator that the variable is bound to the current object and that a variable that doesn't use the member operator is free and isn't a member of any object.
PostPosted: Tue Oct 27, 2009 1:34 pm
by Whitefang
I disagree 1000%. It's far easier to get scope confused than to confuse variables that have unique names. There are few good reasons not to give every variable a unique name.
Obviously, that may not be the case for everyone, but in general I find this to be true.
PostPosted: Wed Nov 04, 2009 2:45 am
by Warrior4Christ
I can see both sides. Don't use 'this' unless you strictly use it everywhere, otherwise you'll forget to add 'this.' sometimes, such as in "feet = feet" above. The appending the underscore makes the compiler work on your side in finding typos.
At work, we use C++, but our coding standard says that member variables are prefixed with 'my' - so that would be "myFeet = feet". Easy.