Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Double always returning 0.0

Reply
Thread Tools

Re: Double always returning 0.0

 
 
Screamin Lord Byron
Guest
Posts: n/a
 
      04-19-2011
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.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      04-19-2011
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?

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      04-19-2011
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.
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-19-2011
Jason S <(E-Mail Removed)> 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).
 
Reply With Quote
 
Screamin Lord Byron
Guest
Posts: n/a
 
      04-19-2011
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?
 
Reply With Quote
 
Screamin Lord Byron
Guest
Posts: n/a
 
      04-19-2011
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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-19-2011
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-19-2011
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      04-20-2011
Jason S <(E-Mail Removed)> 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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-20-2011
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/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList is always returning the value from index 0 Nathan Sokalski ASP .Net 2 09-04-2005 11:28 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
DropDownList always returning the first item Siamak Zahedi ASP .Net 3 01-28-2005 02:08 PM
'exit function'...always use it when returning a value? darrel ASP .Net 3 08-06-2004 07:02 PM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments