Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: needs a few pointers...

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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;
           }

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: needs a few pointers...

    What are the compiler messages?

    A couple of things I notice: the argument variables are h and u and these (particularly u) is what you should be using in the if condition. And you are missing a } at the end, but that may just be a typo.

Similar Threads

  1. Can pointers as to what's wrong?
    By BrianS in forum Java Applets
    Replies: 0
    Last Post: 03-14-2012, 06:33 AM
  2. Function pointers?
    By AedonetLIRA in forum New To Java
    Replies: 8
    Last Post: 01-18-2011, 04:01 AM
  3. Does Java support pointers?
    By kthaker in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 08:16 AM
  4. Java pointers? How to...
    By Krooger in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 08:30 PM
  5. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 01:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •