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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Write a short program that takes as <strong>in</strong>put three <strong>in</strong>tegers, a, b, <strong>and</strong> c, from the<br />

<strong>Java</strong> console <strong>and</strong> determ<strong>in</strong>es if they can be used <strong>in</strong> a correct arithmetic formula<br />

(<strong>in</strong> the given order), like "a + b = c," "a = b − c," or "a*b = c."<br />

R-2.15<br />

Write a short <strong>Java</strong> program that creates a Pair class that can store two objects<br />

declared as generic types. Demonstrate this program by creat<strong>in</strong>g <strong>and</strong> pr<strong>in</strong>t<strong>in</strong>g<br />

Pair objects that conta<strong>in</strong> five different k<strong>in</strong>ds of pairs, such as<br />

<strong>and</strong> .<br />

R-2.16<br />

Generic parameters are not <strong>in</strong>cluded <strong>in</strong> the signature of a method declaration, so<br />

you cannot have different methods <strong>in</strong> the same class that have different generic<br />

parameters but otherwise have the same names <strong>and</strong> the types <strong>and</strong> number of<br />

their parameters. How can you change the signatures of the conflict<strong>in</strong>g methods<br />

to get around this restriction?<br />

Creativity<br />

C-2.1<br />

Expla<strong>in</strong> why the <strong>Java</strong> dynamic dispatch algorithm, which looks for the method<br />

to <strong>in</strong>voke for a call o.a(), will never get <strong>in</strong>to an <strong>in</strong>f<strong>in</strong>ite loop.<br />

C-2.2<br />

Write a <strong>Java</strong> class that extends the Progression class so that each value <strong>in</strong><br />

the progression is the absolute value of the difference between the previ- ous<br />

two values. You should <strong>in</strong>clude a default constructor that starts with 2 <strong>and</strong> 200<br />

as the first two values <strong>and</strong> a parametric constructor that starts with a specified<br />

pair of numbers as the first two values.<br />

C-2.3<br />

Write a <strong>Java</strong> class that extends the Progression class so that each value <strong>in</strong><br />

the progression is the square root of the previous value. (Note that you can no<br />

longer represent each value with an <strong>in</strong>teger.) You should <strong>in</strong>clude a default<br />

constructor that has 65,536 as the first value <strong>and</strong> a parametric constructor that<br />

starts with a specified (double) number as the first value.<br />

C-2.4<br />

Rewrite all the classes <strong>in</strong> the Progression hierarchy so that all values are<br />

from the BigInteger class, <strong>in</strong> order to avoid overflows all together.<br />

134

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

Saved successfully!

Ooh no, something went wrong!