|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
lack of automated tests: Because I/O makes for difficult testing (often there is no predictable result to assertEquals in unit tests) so you don't need to turn in any unit tests for this homework. You will turn in your code and documentation together in class on Oct.31 (halloWednesday). (Of course, you'll still be running (However, it's possible to still make unit tests which read input from a fixed String, and you can inspect the terminal window manually.)
We will write a class TextIO, which will help communicate between your explorer's world and somebody playing the game.
Make a second (“overloaded”) constructor, which takes one argument — the Scanner this TextIO will read from.
This variant will be very handy for testing -- you can make a new TextIO( new Scanner( "chill inventory dude" ) ), and then test the methods below without having to type input from the keyboard every time.1 (This explanation will make more sense after you've finished the parts below.)
/** Display game info to the player. * @param msg The message to display. */ public void display( String msg ) |
Write the following method, for TextIO:
/** Print a message to the screen, and get a response. * @param msg A message to print to the screen, * for example "What month is your birthday? ". * @return The response -- just one word, which is taken * from this TextIO's internal Scanner. (If this Scanner happens * to be reading from System.in, then this function is getting * an answer interactively.) * Example: String myFave = someIO.promptForString( "What month is yer bday?" ); * */ public String promptForString( String msg ) |
Rationale: As seen in lab10b—input: Scanner, a common bit of code is to (a) print some information on the screen, and then (b) immediately use a scanner to read an answer. In fact, these two tasks are so often done together that it's convenient to be able to do that with one single method call. For example, instead of writing2
System.out.print( "Enter a celsius temperature: " ); myLocalVar = myScannerObject.nextInt(); |
myLocalVar = myTextIoObject.promptForInt( "Enter a celsius temperature:" ); |
Write a method exploreOnce, inside of class Explorer. This method will
This method should only communicate with the user via methods in TextIO; this method must not call System.out.println directly.
preview: Later, you'll be provided with a class GuiIO, which has exactly the same methods as TextIO, but it uses a GUI window rather than the console and keyboard. But your explorer program should run equally well if you use the GuiIO in place of the TextIO. That is why your exploreOnce method doesn't interact directly with the keyboard or screen.
public static void main( String[] ignored ) { Explorer hero = new Explorer( "Dora" ); hero.exploreOnce(); hero.exploreOnce(); hero.exploreOnce(); } |
Some extra credit: As it stands, we have two different TextIO constructors. This is a bit bothersome, since if we ever add more statements to one of those constructors in the future, we'll have to remember that we probably want to add those same statements in the other constructor as well. It would be better to not have to repeat such code. (And in fact, there is a smidgen of repeated code as-is: assigning to the field.)
Read about using this with a constructor to see how one constructor can call another. Then, modify one of your existing constructors so that it's just a single line which calls the other constructor.
1 It's equally easy to even have a file with all the input text, and make a TextIO which reads from that java.io.File. ↩
2 Note that you may prefer to use System.out.print instead of System.out.println. ↩
3You use a non-passé phrase, if you like. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Nov.11 (Sun) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |