Strategy 4
Write the interface or class in each question.
-
The interface
G
has methodsString who()
,int when()
, andvoid walk(int howlong)
. -
Copy or type the tester class.
public class TestG {
public static void tester(G x) {
System.out.println(x.who());
System.out.println(x.when());
x.walk(5);
System.out.println(x.when());
}
}
- The class
G1
implements theG
interface. Use+
to join strings.
public static void demoA() {
G1 x = new G1("Samuel",10,30);
tester(x);
// Output: "Big Samuel" 1030 1035
}
- The class
G2
implements theG
interface. When asked to walk for N minutes, these objects actually take 2N minutes.
public static void demoB() {
G2 y = new G2("Jordan", 8, 20);
tester(y);
// Output: "Little Jordan" 820 830
}
- The class
G2X
extendsG2
. The name changes to add “Fast” but nothing else changes.
public static void demoC() {
G2 y = new G2X("Jordan", 8, 20);
tester(y);
// Output: "Fast Little Jordan" 820 830
}
-
Unrelated to the above, create a class
H
that counts from its lower limit to its upper limit. Methods:int start()
,boolean done()
,int next()
.- The start() method gives the first number to count from.
- The next() method gives the next number in the sequence.
- The done() method returns true when the count has reached the end number.
Calling demoD()
produces output: 31 32 33 34 35 36 37 38 39 40
Below is the demoD
and testH
method. The demoD
method is
complicated just to show you how to write a
conditional (if statement) in Java.
public static void demoD() {
H a = new H(30, 40);
int s = 0; // choose "else" clause always
if ( s < 0 ) {
System.out.println("Small s, no work.");
} else {
testH(a);
}
}
public static void testH(H b) {
int n = b.start();
while (! b.done()) {
n = b.next();
System.out.println(n);
}
}
- Make a class
H4
extendingH
, altering the behavior so it counts up by 4. Test your class by modifying thedemoD
function or by using the short code segment below (put in the same class as yourtestH
).
public static void demoE() {
testH(new H4(20,60)); // print 24, 28, ..., 60
}
- Modify your
H
class if necessary so that it resets to the beginning every timestart()
is called.
public static void demoF() {
H x = new H(20,30);
testH(x); // print 21, 22, ..., 30
testH(x); // print 21, 22, ..., 30 again
}