Results 1 to 5 of 5
Thread: needs a few pointers...
- 04-20-2012, 12:42 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
needs a few pointers...
-change two parameter method to test its units parameter to see if equal to allowable symbols; if equal, set instance variables and return true. If not equal to values, return false.
from the book:
public void setHeight(double height, String units)
{
this.height = height;
this.units = units;
}
my solution that I have not been sure is correct as I am not sure how to utilize the returned value, if it even works the way that is intended:
public boolean setHeight(double h, String u)
{
height = h;
units = u;
return units.equals("m") || units.equals("cm") || units.equals("mm") || units.equals("in") || units.equals(
ft")
}
I am able to do it in other ways but I am not sure how to do it the way that the book wants. The book seems tricky. I think they make it obscure on purpose.
- 04-20-2012, 01:48 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: needs a few pointers...
In the revised method you are supposed to do two things: return a boolean and set the field if you can. Use the condition you have figured out as part of an if statement.
As for seeing if what you have is correct, for that you need a main() method in this or another class. It should call the method you have written with both "good" and "bad" units then print,eg, what is returned and check to see that the units were set correctly.
- 04-20-2012, 11:17 AM #3
Re: needs a few pointers...
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-20-2012, 08:50 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
Re: needs a few pointers...
I am not sure about the syntax on returning a boolean while setting the values. I need to look through the book for examples and re-read the relevant sections more. I'm just concerned that I am spending too much time trying to figure it out on my own. The problem that I am having with checking the program is using the returned boolean value. What variable do I use to call the returned boolean value, or how do I write the method to represent it as '!' or not? the first code that I posted compiles but I am not sure how to get at the boolean value. The code listed below does not compile.
Putting the boolean aspect into the constructor does not make sense to me.
Thanks for your help!
Java Code:public boolean setHeight(double h, String u) { if(units.equals("m") || units.equals("cm") || units.equals("mm") || units.equals("in") || units.equals("ft")) { height = h; units = u; return true; } else { return false; }
- 04-21-2012, 12:27 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Can pointers as to what's wrong?
By BrianS in forum Java AppletsReplies: 0Last Post: 03-14-2012, 06:33 AM -
Function pointers?
By AedonetLIRA in forum New To JavaReplies: 8Last Post: 01-18-2011, 04:01 AM -
Does Java support pointers?
By kthaker in forum New To JavaReplies: 2Last Post: 10-07-2009, 08:16 AM -
Java pointers? How to...
By Krooger in forum New To JavaReplies: 4Last Post: 11-04-2008, 08:30 PM -
Pointers
By ravian in forum New To JavaReplies: 5Last Post: 11-28-2007, 01:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks