Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 20 Apr 2011 12:53:55 -0500 From: Jason S Newsgroups: comp.lang.java.programmer Date: Wed, 20 Apr 2011 13:53:55 -0400 Message-ID: <2011042013535527735-jasonsavlov@gmailcom> References: <201104182005433244-jasonsavlov@mecom> <2011041911160346799-jasonsavlov@mecom> <2011041911200138175-jasonsavlov@mecom> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: Double always returning 0.0 User-Agent: Unison/2.1.4 Lines: 159 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-fJB3cJPnGU1CKTnBi7p/jNhbpacjHQCpNbTkoQl6l+fXV37/Tx0WDi2Rx9mS2erYZJiBdukaF0ZV5Vn!c0xS1uUww4N4DmPGLp/hxxWj6ROVdoPj3/S5zMPylT6wjk7L+xZEeLv1G/tKNdO732eRVJc= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5607 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3162 Thank you! I understand how that works now. Except it still outputs 0.000 for the cat's weight. Everything else works though. On 2011-04-19 14:03:03 -0400, Lew said: > 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 ); > } > > } > > ============================================================ -- Jason