Here's what I plan to do for my recitation. I'd recommend looking at the Driver.java file as you read along with this, so that you can see what I'm talking about
- Give your students the premise of the problem: you want a program that allows you to scramble input in various ways, and prints it back out. Show them the
Scrambler
abstract class, containing thescramble
method. - Run the program and walk them through the the two options it presents as input,
(c)hoose a scrambler
and(e)nter text to scramble
- Show them the main method of the code in Driver.java, and give them a general overview of the different helper methods. Briefly walk them through the helper methods creating what they see in the terminal. Skip over the
showScramblerOutput
method for now. - Choose option
c
, and selectUppercaseScrambler
. - Then choose option
e
, and enter "C3 recitation is the best!" or whatever string you want. - Show them how the output now shows the
UppercaseScrambler
acting on the inputted string. - Choose option
c
again, and selectBackwardsScrambler
. Show how the output instantly changes from an uppercase string to a backwards string. Stress the fact that you were able to do this without recompiling the program or changing the code! - Go back to the main method in the Driver class, and work with them to figure out how this works. Suggest that you take a look at the
showScramblerOutput
method. - Look at the
showScramblerOutput
method, namely the line containingchosenScrambler.scramble(chosenText)
. Note that even though chosenScrambler was given the typeScrambler
, which is abstract, we are still able to call thescramble
method! How? Let's trace through main to find more code referencing the scramblers. - Trace the main method's code down to
chooseScrambler()
and look at that method next. - Find the subclass instantiations and show them how this is the part of the code that makes it all possible. Remind them that Scrambler is abstract, but a
BackwardsScrambler
is-aScrambler
, so we can say ScramblerchosenScrambler = new BackwardsScrambler()
. - (If you have more time / if the students seem comfortable with what you have so far.) Mention that there is another Scrambler type that we didn't include in the list of options, and thus there's no way to get to it with the code given. Show how you can add a Scrambler[] array to the Driver class and let the user pick any scrambler out of the array to use instead, to reduce hardcoding.
Short demo: