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
Booterthat has one functions:int foot(int x). -
Make an abstract class
Fthat implements theBooterinterface.-
Private ints
pqand 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
GGthat extendsF.- The constructor takes the same information.
- The
grokk()method returnsgetStr() + getStr().
-
Make a class
HHthat extendsF.- The constructor takes in a single int
nand uses that to determine both of the numbers needed by theFconstructor:2*nand3*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
Acontaining the following methods:void af()int agg()int ahhh(int x);
-
Write a class
Bthat implements theAinterface with the following behavior:Bremembers two int variables (mandn)- The constructor for
Btakes in one int and setsmto that number andnto 5. - The
af()function setsmton. - The
agg()function gives the product ofmandn. - The
ahhh(int x)increasesnbyxand then returnsm.
-
Write a class
CextendingB.- The
Cconstructor takes in only one intn, and uses10*nas the input to theBconstructor. - The
ahhh(int x)function inCdoes 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.