23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

•<br />

Class Goat extends Object <strong>and</strong> adds an <strong>in</strong>stance variable tail <strong>and</strong><br />

methods milk() <strong>and</strong> jump().<br />

•<br />

Class Pig extends Object <strong>and</strong> adds an <strong>in</strong>stance variable nose <strong>and</strong><br />

methods eat() <strong>and</strong> wallow().<br />

•<br />

Class Horse extends Object <strong>and</strong> adds <strong>in</strong>stance variables height <strong>and</strong><br />

color, <strong>and</strong> methods run() <strong>and</strong> jump().<br />

•<br />

Class Racer extends Horse <strong>and</strong> adds a method race().<br />

•<br />

R-2.6<br />

Class Equestrian extends Horse <strong>and</strong> adds an <strong>in</strong>stance variable<br />

weight <strong>and</strong> methods trot() <strong>and</strong> is Tra<strong>in</strong>ed().<br />

Give a short fragment of <strong>Java</strong> code that uses the progression classes from<br />

Section 2.2.3 to f<strong>in</strong>d the 8th value of a Fibonacci progression that starts with 2<br />

<strong>and</strong> 2 as its first two values.<br />

R-2.7<br />

If we choose <strong>in</strong>c = 128, how many calls to the nextValue method from the<br />

ArithProgression class of Section 2.2.3 can we make before we cause a<br />

long-<strong>in</strong>teger overflow?<br />

R-2.8<br />

Suppose we have an <strong>in</strong>stance variable p that is declared of type<br />

Progression, us<strong>in</strong>g the classes of Section 2.2.3. Suppose further that p<br />

actually refers to an <strong>in</strong>stance of the class Geom Progression that was<br />

created with the default constructor. If we cast p to type Progression <strong>and</strong><br />

call p.firstValue(), what will be returned? Why?<br />

R-2.9<br />

131

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!