Strategy 6
Principles
- Do not repeat variables from the superclass.
- Do not repeat functions from the superclass.
- Override little functions instead of rewriting big functions.
- When the superclass has a constructor, make sure you call it.
Almost Simple
-
Make an interface
Booter
that has one functions:int foot(int x)
. -
Make an abstract class
F
that implements theBooter
interface.-
Private ints
p
q
and Strings
. -
The constructor takes in two int inputs, and a String, and remembers all of them.
-
When
foot(1)
is called, the class increments the int (adds one) and returns it. -
When
foot(2)
is called, the integer remembered is altered to be ten times its current value. -
The method
String getStr()
returns the remembered string. -
The method
String grokk()
is not described. -
There is a method
void grind(int k)
:public void grind (int k) { while(k>0) { System.out.println(grokk()+p+s+q); k -= 1; } }
-
-
Make a class
GG
that extendsF
.- The constructor takes the same information.
- The
grokk()
method returnsgetStr() + getStr()
.
-
Make a class
HH
that extendsF
.- The constructor takes in a single int
n
and uses that to determine both of the numbers needed by theF
constructor:2*n
and3*n
. Use “HH” for the value of the string passed. - The
grokk()
method returns “H” always.
- The constructor takes in a single int
More Complex
-
Make an interface
A
containing the following methods:void af()
int agg()
int ahhh(int x);
-
Write a class
B
that implements theA
interface with the following behavior:B
remembers two int variables (m
andn
)- The constructor for
B
takes in one int and setsm
to that number andn
to 5. - The
af()
function setsm
ton
. - The
agg()
function gives the product ofm
andn
. - The
ahhh(int x)
increasesn
byx
and then returnsm
.
-
Write a class
C
extendingB
.- The
C
constructor takes in only one intn
, and uses10*n
as the input to theB
constructor. - The
ahhh(int x)
function inC
does the same with as theahhh(int x)
function inB
. However, it keeps track of how many times it was called and returns the number of times it was called.
- The
Testing code for the A interface.