Re: Double always returning 0.0

Discussion in 'Java' started by Screamin Lord Byron, Apr 19, 2011.

  1. On 19.04.2011 02:05, Jason S wrote:
    > I'm trying to figure out why I can't seem to get my program to return
    > the correct value here. It always returns 0.0 even though I (think) that
    > everything is declared/cast correctly...
    >
    > Code located at http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8


    Remember that instance variables aren't inherited. That is, you are
    using values from Dog and Cat, rather than Pet, which are initialized to
    their defaults.
    Screamin Lord Byron, Apr 19, 2011
    #1
    1. Advertising

  2. Screamin Lord Byron

    Lew Guest

    Lew, Apr 19, 2011
    #2
    1. Advertising

  3. Screamin Lord Byron

    Lew Guest

    Jason S wrote:
    > Lew said:
    >> Jason S wrote:
    >>> Yup, that was the problem. Got it working! (Sort of...) The weight for the cat
    >>> keeps turning up as 0.000.


    >> What does your code look like now?


    > http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8
    >
    > The code is there in full


    Awesome, thanks.

    --
    Lew
    BTW, I went through the State U. of NY system also.
    Lew, Apr 19, 2011
    #3
  4. Jason S <> wrote:
    > On 2011-04-19 11:18:10 -0400, Lew said:
    >> Jason S wrote:
    >>> Yup, that was the problem. Got it working! (Sort of...) The weight for the cat
    >>> keeps turning up as 0.000.

    >> What does your code look like now?

    > http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8
    > The code is there in full


    So, now you initialize both sets of variables from the new
    subclass methods. That looks quite goofy to me. Why not just
    use Pet's variables and use baseclass' public getWeight() to
    access baseclass' private weight for calculations?

    public double acepromazine() {
    return ((getWeight() / 2.2) * (0.002 / 10.0));
    }

    And then remove each subclass' copies of those fields altogether.

    I'd expect that whereever the remaining bug really hides, it will
    either show up, or silently disappear once you get the fields sane.


    Btw., normally one would throw exceptions rather than exit the
    process with System.exit, but even if you really want to exit
    the process, you most likely don't want to exit with a zero
    exit-status. A zero exit-status from a process is typically
    meant as "success", but that obviously isn't the case where
    you call System.exit(0).
    Andreas Leitgeb, Apr 19, 2011
    #4
  5. On 19.04.2011 17:16, Jason S wrote:
    > On 2011-04-19 05:02:09 -0400, Screamin Lord Byron said:
    >
    >> On 19.04.2011 02:05, Jason S wrote:
    >>> I'm trying to figure out why I can't seem to get my program to return
    >>> the correct value here. It always returns 0.0 even though I (think) that
    >>> everything is declared/cast correctly...
    >>>
    >>> Code located at http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8

    >>
    >> Remember that instance variables aren't inherited. That is, you are
    >> using values from Dog and Cat, rather than Pet, which are initialized to
    >> their defaults.

    >
    > Yup, that was the problem. Got it working! (Sort of...) The weight for
    > the cat keeps turning up as 0.000.


    I think you didn't understood what I said. You need only one name, one
    age and one weight varable per object. Why do you have two?
    Screamin Lord Byron, Apr 19, 2011
    #5
  6. On 19.04.2011 17:20, Jason S wrote:
    > On 2011-04-19 11:18:10 -0400, Lew said:
    >
    >> Jason S wrote:
    >>> Yup, that was the problem. Got it working! (Sort of...) The weight
    >>> for the cat
    >>> keeps turning up as 0.000.

    >>
    >> What does your code look like now?

    >
    > http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8
    >
    > The code is there in full


    I don't see the difference between the code when I first looked and now.
    Screamin Lord Byron, Apr 19, 2011
    #6
  7. Screamin Lord Byron

    Lew Guest

    Jason S wrote:
    > http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8
    >
    > The code is there in full


    There are a few things you should watch out for in your code.

    Stylistically, in a class declaration you should list all fields first, then
    constructors, then methods. (You haven't learned about 'static' members yet,
    so I do not speak of those at this time.) This makes it easier to find what
    you're looking for.

    You declare the same exact attributes in supertype and subtype:

    public class Pet {
    private String name;
    private int age; // years
    private double weight; // lbs
    ...

    public class Cat extends Pet {
    private String name;
    private int age;
    private double weight;
    ...

    This is bad. Because 'Cat' /is-a/ 'Pet', it already possesses within itself
    these attributes already, without redeclaring them. The only question is how
    it can gain access to them. As it is, you call 'setAge()' on a 'Cat' and it
    will fail to do what you expect. Plus, the type's clients have no way to read
    the attributes.

    The answer (one answer) is to add accessor ("getter") and mutator ("setter")
    methods for each attribute. (No setter is needed for read-only attributes.)
    I added exceptions and a couple of 'this()' constructor references for your
    study fun. Note that your individual constructors only work because the
    argument types differ. That's a power trick you used there.

    You should use 0.0 for a double constant, not 0.

    I have omitted Javadocs for this example, but that's a bad habit.

    As someone else suggested, the dosage methods should be abstract in the parent
    type.

    ============================================================

    public abstract class Pet {
    private String name;
    private int age; // years
    private double weight; // lbs

    public Pet( String name, int age, double weight ) {
    setName( name ); // works because the setters are final
    setAge( age );
    setWeight( weight );
    }

    public Pet( String name ) {
    this( name, 0, 0.0 );
    }

    public Pet( int age ) {
    this( "", age, 0.0 );
    }

    public Pet( double weight ) {
    this( "", 0, weight );
    }

    public Pet()
    this( "", 0, 0.0 );
    }

    public final String getName() (
    return this.name;
    }
    public final void setName( String name ) {
    if ( name == null ) {
    final String msg = "null name.";
    throw new IllegalArgumentException( msg );
    }
    this.name = name;
    }

    public final String getAge() (
    return this.age;
    }
    public final void setAge( int age ) {
    if ( age < 0) {
    final String msg = "negative age "+ age +".";
    throw new IllegalArgumentException( msg );
    }
    this.age = age;
    }

    public final String getWeight(() (
    return this.weight;
    }
    public final void setWeight( double weight ) {
    if ( weight < 0.0 ) {
    final String msg = "negative weight "+ weight +".";
    throw new IllegalArgumentException( msg );
    }
    this.weight = weight;
    }

    public abstract double acepromazine();
    public abstract double carprofen();

    }

    ============================================================

    public class Cat extends Pet {

    public Cat( String name, int age, double weight ) {
    super( name, age, weight );
    }

    public Cat( String name ) {
    super( name );
    }

    public Cat( int age ) {
    super( age );
    }

    public Cat( double weight ) {
    super( weight );
    }

    public Cat()
    }

    @Override
    public double acepromazine() {
    return (weight * 0.002 / 10.0 / 2.2 );
    }

    @Override
    public double carprofen() {
    return ( weight * 0.25 / 12.0 / 2.2 );
    }

    }

    ============================================================

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Apr 19, 2011
    #7
  8. Screamin Lord Byron

    Lew Guest

    OOOPS!

    Forgot to use the accessor method. Sorry.

    Lew wrote:
    > ============================================================
    >
    > public class Cat extends Pet {
    >


    ....

    > @Override
    > public double acepromazine() {
    > return (


    getWeight()

    > * 0.002 / 10.0 / 2.2 );
    > }



    > @Override
    > public double carprofen() {
    > return (


    getWeight()

    > * 0.25 / 12.0 / 2.2 );
    > }
    > }
    >
    > ============================================================



    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Apr 19, 2011
    #8
  9. Jason S <> wrote:
    > Thank you! I understand how that works now. Except it still outputs
    > 0.000 for the cat's weight. Everything else works though.


    If you've now removed the weight field in Dog and Cat (and only left
    the one in Pet), and you still see zero weights, then upload the new
    versions to that url, and repost the URL.
    Andreas Leitgeb, Apr 20, 2011
    #9
  10. Screamin Lord Byron

    Lew Guest

    Andreas Leitgeb wrote:
    > Jason S wrote:
    >> Thank you! I understand how that works now. Except it still outputs
    >> 0.000 for the cat's weight. Everything else works though.


    > If you've now removed the weight field in Dog and Cat (and only left
    > the one in Pet), and you still see zero weights, then upload the new
    > versions to that url, and repost the URL.


    And Jason, don't top-post, ok?

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Apr 20, 2011
    #10
  11. Screamin Lord Byron

    Lew Guest

    Jason S wrote:
    > Nevermind, it works. Either the value 0.002 mentioned for the cat's
    > acepromazine is a typo, or my cat's weight is too light to return anything
    > more than what would be formatted with the decimalformat :p
    >
    > so it works. http://web.cs.sunyit.edu/~savlovj/CS249/pg472_8.
    >
    > Thanks for the help guys! Highly appreciated :)
    >
    > And lew [sic]: sorry about the top posting


    Regarding:

    public Cat() {
    super();
    }

    The call to the 'super()' constructor is unnecessary, albeit harmless.

    --
    Lew
    Honi soit qui mal y pense.
    http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
    Lew, Apr 20, 2011
    #11
  12. [OT] Re: Double always returning 0.0

    Wed, 20 Apr 2011 14:29:35 -0400, /Lew/:

    > public Cat() {
    > super();
    > }
    >
    > The call to the 'super()' constructor is unnecessary, albeit harmless.


    I often want the "default" no-args constructor explicitly declared
    but I've setup my environment to trigger warning for empty
    undocumented blocks like:

    public Cat() {
    }

    so I usually write it as:

    public Cat() {
    //super();
    }

    The commented out |super()| call (implicitly done by the compiler)
    acting as documentation.

    --
    Stanimir
    Stanimir Stamenkov, Apr 21, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    3
    Views:
    716
    darrel
    Aug 6, 2004
  2. Siamak Zahedi

    DropDownList always returning the first item

    Siamak Zahedi, Jan 27, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    750
    Karl Seguin
    Jan 28, 2005
  3. Nathan Sokalski
    Replies:
    2
    Views:
    530
    Juan T. Llibre
    Sep 5, 2005
  4. Deryck
    Replies:
    4
    Views:
    506
    derek giroulle
    Jun 22, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,451
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page