Help with a program...

The geek forum. PHP, Perl, HTML, hardware questions etc.. it's all in here. Got a techie question? We'll sort you out. Ask your questions or post a link to your own site here!

Help with a program...

Postby Destroyer2000 » Mon Oct 26, 2009 3:04 pm

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+"\"");
}
}
My Flickr
My Travel Blog

[color="Red"]Distance is to Love as Wind is to Fire...it extinguishes the small, and ignites the great. - Unknown[/color]

[color="RoyalBlue"]“Falling in love with someone isn't always going to be easy... Anger... tears... laughter.. It's when you want to be together despite it all. That's when you truly love another. I'm sure of it.â€
User avatar
Destroyer2000
 
Posts: 1756
Joined: Thu Sep 11, 2003 9:00 am
Location: Fields of Athenry

Postby Davidizer13 » Mon Oct 26, 2009 4:00 pm

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.
We are loved even though we suck.

Psalms 37:37 (NHEB)
Mark the perfect man, and see the upright, for there is a future for the man of peace.
User avatar
Davidizer13
 
Posts: 1080
Joined: Fri Jun 26, 2009 9:27 am
Location: VIOLENT CITY

Postby Whitefang » Mon Oct 26, 2009 8:24 pm

[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.
"It's not easy to act in the name of justice."

"Justice is not the only right in this world"
User avatar
Whitefang
 
Posts: 261
Joined: Wed Nov 19, 2008 9:17 pm
Location: Paradise

Postby blkmage » Tue Oct 27, 2009 8:25 am

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.
User avatar
blkmage
 
Posts: 4529
Joined: Mon May 03, 2004 5:40 pm

Postby Whitefang » Tue Oct 27, 2009 1:34 pm

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.
"It's not easy to act in the name of justice."

"Justice is not the only right in this world"
User avatar
Whitefang
 
Posts: 261
Joined: Wed Nov 19, 2008 9:17 pm
Location: Paradise

Postby Warrior4Christ » Wed Nov 04, 2009 2:45 am

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.
Everywhere like such as, and MOES.

"Expect great things from God; attempt great things for God." - William Carey
User avatar
Warrior4Christ
 
Posts: 2045
Joined: Sat Aug 20, 2005 8:10 pm
Location: Carefully place an additional prawn on the barbecue


Return to Computing and Links

Who is online

Users browsing this forum: No registered users and 218 guests