11. Inheritance 2
Noisy
It takes 0.2 seconds per letter for an animal to make a noise, so “meow” takes 0.8 seconds and “meeeou” is 1.2 seconds. There are two methods to think about:
- public String noise(): the noise that something
Noisy
makes; and - public double timeNoise(): the number of seconds it takes to produce that noise.
- Is this best as an interface or an abstract class? Explain your answer.
- Write the
Noisy
thing in Java. - Make a
Dolphin
class that makes the “click” noise.
Colorable
A class is Colorable
if it has the methods void setColor(int c)
and int getColor()
.
- Is this best as an interface or an abstract class? Explain your choice.
- Add the
Colorable
abilities to yourDolphin
.
Cats
A Cat
is a Noisy
thing that makes one of three noises: “meow”,
“purr”, or “rrrrrrr”. When you construct a cat it should get a number
0, 1, or 2 that it uses to choose which noise to make.
A RandomCat
is a kind of Cat
that makes an unspecified random
noise. Each random cat could make a different noise, but a given
random cat will always make the same noise.
A VeryRandomCat
makes a different random noise every time it makes a
noise. Figure out a way to create this class without duplicating code
from other classes, possibly by going back to rewrite what you have in
a different way.