Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #3145
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | Lew <noone@lewscanon.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Double always returning 0.0 |
| Date | Tue, 19 Apr 2011 14:03:03 -0400 |
| Organization | albasani.net |
| Lines | 153 |
| Message-ID | <iokioc$kmi$1@news.albasani.net> (permalink) |
| References | <201104182005433244-jasonsavlov@mecom> <iojj2h$4fn$1@news.metronet.hr> <2011041911160346799-jasonsavlov@mecom> <iok936$uh9$2@news.albasani.net> <2011041911200138175-jasonsavlov@mecom> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.albasani.net B3Bk7/ho/3mnPXlp2jAj5saTF5yPhbo6lBcVFGO7E/b5fglE/8mSPp6mk/4AEeLFgGloglrn3HM5YYW2vczJeY2nzTSXkZadKWK7RE7q9wWf+k3l5hbNcYfVm3O47R5w |
| NNTP-Posting-Date | Tue, 19 Apr 2011 18:02:52 +0000 (UTC) |
| Injection-Info | news.albasani.net; logging-data="OJ4IJpN4lrur+LhtsLy7oesTMm5PTDSzeWznyiOiskAdhmwTO7XcvJRNLO/Xq9y6mA/BBZ6AlfeqagTUV9TDNBWjdGx9JOaiqK5xKOusck5N6pYkWcjYZVQ/HNhFvdmw"; mail-complaints-to="abuse@albasani.net" |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110223 Thunderbird/3.1.8 |
| In-Reply-To | <2011041911200138175-jasonsavlov@mecom> |
| Cancel-Lock | sha1:6foaE4n03OQRuNxr2/CmIgFsAK0= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3145 |
Show key headers only | View raw
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Double always returning 0.0 Jason S <jasonsavlov@me.com> - 2011-04-18 20:05 -0400
Re: Double always returning 0.0 markspace <-@.> - 2011-04-18 17:43 -0700
Re: Double always returning 0.0 Jim <jkorman@alltel.net> - 2011-04-18 20:11 -0500
Re: Double always returning 0.0 Screamin Lord Byron <scre@min.dot> - 2011-04-19 11:02 +0200
Re: Double always returning 0.0 Jason S <jasonsavlov@me.com> - 2011-04-19 11:16 -0400
Re: Double always returning 0.0 Lew <noone@lewscanon.com> - 2011-04-19 11:18 -0400
Re: Double always returning 0.0 Jason S <jasonsavlov@me.com> - 2011-04-19 11:20 -0400
Re: Double always returning 0.0 Lew <noone@lewscanon.com> - 2011-04-19 11:57 -0400
Re: Double always returning 0.0 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-19 16:04 +0000
Re: Double always returning 0.0 Jason S <jasonsavlov@me.com> - 2011-04-19 17:22 -0400
Re: Double always returning 0.0 Screamin Lord Byron <scre@min.dot> - 2011-04-19 19:08 +0200
Re: Double always returning 0.0 Lew <noone@lewscanon.com> - 2011-04-19 14:03 -0400
Re: Double always returning 0.0 Lew <noone@lewscanon.com> - 2011-04-19 14:06 -0400
Re: Double always returning 0.0 Jason S <jason.savlov@gmail.com> - 2011-04-20 13:53 -0400
Re: Double always returning 0.0 Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-04-20 18:06 +0000
Re: Double always returning 0.0 Lew <noone@lewscanon.com> - 2011-04-20 14:11 -0400
Re: Double always returning 0.0 Jason S <jason.savlov@gmail.com> - 2011-04-20 14:19 -0400
Re: Double always returning 0.0 Lew <noone@lewscanon.com> - 2011-04-20 14:29 -0400
[OT] Re: Double always returning 0.0 Stanimir Stamenkov <s7an10@netscape.net> - 2011-04-21 07:30 +0300
Re: Double always returning 0.0 Screamin Lord Byron <scre@min.dot> - 2011-04-19 19:05 +0200
Re: Double always returning 0.0 Jason S <jasonsavlov@me.com> - 2011-04-19 13:07 -0400
Re: Double always returning 0.0 Roedy Green <see_website@mindprod.com.invalid> - 2011-04-19 16:30 -0700
csiph-web