From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch12;
002
003 importimport means to make the classes and/or packages available in this program java.io.PrintWriter;
004 importimport means to make the classes and/or packages available in this program java.util.Scanner;
005
006 publicpublic is used to indicate unrestricted access (any other class can have access) abstractAbstract means there is implementation missing. classclass is a group of fields and methods used for making objects AdventurePage {open braces start code blocks and must be matched with a close brace
007 /** index value used to indicate that there is no matching page */
008 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer NO_SUCH_PAGE =this assignment operator makes the left side equal to the right side -1;
009
010 /**
011 * The format of the input file (as far as thisthis means the current object (the implicit parameter) method is concerned)
012 * is the name of a page type (subclass of {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace)
013 * followed by the data forfor is a looping structure for repeatedly executing a block of code the content of that object. This method
014 * reads the line (containing "Answer" or "Question") and constructs
015 * the appropriate type of page, passing in a reference to thisthis means the current object (the implicit parameter) book
016 * and the scanner; the page constructor is responsible forfor is a looping structure for repeatedly executing a block of code consuming
017 * all necessary information and leaving the scanner set to read the
018 * next classclass is a group of fields and methods used for making objects naming line.
019 *
020 * @paramthis is the Javadoc tag for documenting the purpose of parameters book the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace to which the newnew is used to create objects by calling the constructor page
021 * belongs
022 * @paramthis is the Javadoc tag for documenting the purpose of parameters scanner input {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace attached to a proper input
023 * stream
024 *
025 * @returnnull a newnew is used to create objects by calling the constructor page ({open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace or {open braces start code blocks and must be matched with a close brace@link Answer}close braces end code blocks and must match an earlier open brace)
026 */
027 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) AdventurePage readPageFromFile(AdventureBook book,
028 Scanner scanner) {open braces start code blocks and must be matched with a close brace
029 String questionOrAnswer =this assignment operator makes the left side equal to the right side scanner.nextLine();
030 ifif executes the next statement only if the condition in parenthesis evaluates to true (questionOrAnswer.equals("Question")) {open braces start code blocks and must be matched with a close brace
031 returnreturn means to provide the result of the method and/or cease execution of the method immediately newnew is used to create objects by calling the constructor Question(book, scanner);
032 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
033 returnreturn means to provide the result of the method and/or cease execution of the method immediately newnew is used to create objects by calling the constructor Answer(book, scanner);
034 }close braces end code blocks and must match an earlier open brace
035 }close braces end code blocks and must match an earlier open brace
036
037 /**
038 * Parallel with {open braces start code blocks and must be matched with a close brace@link #readPageFromFile(AdventureBook, Scanner)}close braces end code blocks and must match an earlier open brace,
039 * thisthis means the current object (the implicit parameter) method writes a given page out to the open text output file.
040 *
041 * @paramthis is the Javadoc tag for documenting the purpose of parameters page the page to write
042 * @paramthis is the Javadoc tag for documenting the purpose of parameters out the {open braces start code blocks and must be matched with a close brace@link PrintWriter}close braces end code blocks and must match an earlier open brace to which the page should be
043 * saved
044 */
045 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value writePageToFile(AdventurePage page,
046 PrintWriter out) {open braces start code blocks and must be matched with a close brace
047 page.save(out);
048 }close braces end code blocks and must match an earlier open brace
049
050 /** the book to which thisthis means the current object (the implicit parameter) page belongs */
051 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) AdventureBook book;
052
053 /** the index of the parent node */
054 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer parentNdx;
055
056 /** thisthis means the current object (the implicit parameter) node's text value */
057 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) String text;
058
059 /**
060 * Construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace. The page will have the given
061 * text as its content and will be added to the given {open braces start code blocks and must be matched with a close brace@link
062 * AdventureBook}close braces end code blocks and must match an earlier open brace.
063 *
064 * @paramthis is the Javadoc tag for documenting the purpose of parameters book the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace to which thisthis means the current object (the implicit parameter) page should
065 * belong
066 * @paramthis is the Javadoc tag for documenting the purpose of parameters text the page contents
067 */
068 protectedprotected is used to restrict access to the current class and subclasses only AdventurePage(AdventureBook book, String text,
069 intint is the type for whole numbers and it is short for integer parentNdx) {open braces start code blocks and must be matched with a close brace
070 thisthis means the current object (the implicit parameter).book =this assignment operator makes the left side equal to the right side book;
071 book.add(thisthis means the current object (the implicit parameter));
072 thisthis means the current object (the implicit parameter).text =this assignment operator makes the left side equal to the right side text;
073 setParentNdx(parentNdx);
074 }close braces end code blocks and must match an earlier open brace
075
076 /**
077 * Get the page number of thisthis means the current object (the implicit parameter) page in the book to which it belongs.
078 *
079 * @returnnull the 0-based page number (index) of thisthis means the current object (the implicit parameter) page
080 */
081 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer getNdx() {open braces start code blocks and must be matched with a close brace
082 returnreturn means to provide the result of the method and/or cease execution of the method immediately book.indexOf(thisthis means the current object (the implicit parameter));
083 }close braces end code blocks and must match an earlier open brace
084
085 /**
086 * Return the current text value forfor is a looping structure for repeatedly executing a block of code thisthis means the current object (the implicit parameter) node
087 *
088 * @returnnull current text value
089 */
090 publicpublic is used to indicate unrestricted access (any other class can have access) String getText() {open braces start code blocks and must be matched with a close brace
091 returnreturn means to provide the result of the method and/or cease execution of the method immediately text;
092 }close braces end code blocks and must match an earlier open brace
093
094 /**
095 * Play thisthis means the current object (the implicit parameter) page. A page is played by presenting some text to the
096 * user (through standard output) and accept some input from the user
097 * (through standard input). This method _must_ be provided by all
098 * subclasses of thisthis means the current object (the implicit parameter) classclass is a group of fields and methods used for making objects. It is provided here solely to describe
099 * the publicpublic is used to indicate unrestricted access (any other class can have access) interfaceinterface is a named collection of declared methods forfor is a looping structure for repeatedly executing a block of code users of {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace objects.
100 *
101 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true the computer won the game, falsefalse is a value for the boolean type and means not true otherwise.
102 */
103 publicpublic is used to indicate unrestricted access (any other class can have access) abstractAbstract means there is implementation missing. booleanboolean is a type that is either true or false play();
104
105 /**
106 * Get the book to which thisthis means the current object (the implicit parameter) page belongs. This is protectedprotected is used to restrict access to the current class and subclasses only because
107 * the book is only of interest to thisthis means the current object (the implicit parameter) object and the pages which
108 * extend it.
109 *
110 * @returnnull the {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace to which thisthis means the current object (the implicit parameter) page belongs
111 */
112 protectedprotected is used to restrict access to the current class and subclasses only AdventureBook getBook() {open braces start code blocks and must be matched with a close brace
113 returnreturn means to provide the result of the method and/or cease execution of the method immediately book;
114 }close braces end code blocks and must match an earlier open brace
115
116 /**
117 * The parent of thisthis means the current object (the implicit parameter) node must be a {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace (ifif executes the next statement only if the condition in parenthesis evaluates to true it exists).
118 * Thus the cast here is safe.
119 *
120 * @returnnull reference to the {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace which leads to thisthis means the current object (the implicit parameter) page
121 * (ifif executes the next statement only if the condition in parenthesis evaluates to true there is one); nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true the node is the root of the
122 * tree
123 */
124 protectedprotected is used to restrict access to the current class and subclasses only Question getParent() {open braces start code blocks and must be matched with a close brace
125 ifif executes the next statement only if the condition in parenthesis evaluates to true (getParentNdx() !=this is the not equals operator which evaluates to true if both sides are different NO_SUCH_PAGE) {open braces start code blocks and must be matched with a close brace
126 returnreturn means to provide the result of the method and/or cease execution of the method immediately (Question) getBook().get(getParentNdx());
127 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
128 returnreturn means to provide the result of the method and/or cease execution of the method immediately nullnull is the value used to refer to a non-existant object;
129 }close braces end code blocks and must match an earlier open brace
130 }close braces end code blocks and must match an earlier open brace
131
132 /**
133 * Get a reference to the {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace (ifif executes the next statement only if the condition in parenthesis evaluates to true any) forfor is a looping structure for repeatedly executing a block of code which thisthis means the current object (the implicit parameter)
134 * question is an answer. Implementation detail must be visible to all
135 * {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace types.
136 *
137 * @returnnull reference to {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace (ifif executes the next statement only if the condition in parenthesis evaluates to true there is one) of which
138 * thisthis means the current object (the implicit parameter) {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace is a child.
139 */
140 protectedprotected is used to restrict access to the current class and subclasses only intint is the type for whole numbers and it is short for integer getParentNdx() {open braces start code blocks and must be matched with a close brace
141 returnreturn means to provide the result of the method and/or cease execution of the method immediately parentNdx;
142 }close braces end code blocks and must match an earlier open brace
143
144 /**
145 * Write the {open braces start code blocks and must be matched with a close brace@link AdventurePage}close braces end code blocks and must match an earlier open brace to the given text file so that it
146 * can be read back in by {open braces start code blocks and must be matched with a close brace@link #readPageFromFile(AdventureBook,
147 * Scanner)}close braces end code blocks and must match an earlier open brace. Overriding methods _must_ call thisthis means the current object (the implicit parameter) method before
148 * writing anything to the text file.
149 *
150 * @paramthis is the Javadoc tag for documenting the purpose of parameters out a {open braces start code blocks and must be matched with a close brace@link PrintWriter}close braces end code blocks and must match an earlier open brace, opened and attached to an
151 * output text file. Where to save thisthis means the current object (the implicit parameter) page.
152 */
153 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value save(PrintWriter out) {open braces start code blocks and must be matched with a close brace
154 out.println(getClass().getName());
155 out.println(getText());
156 out.println(getParentNdx());
157 }close braces end code blocks and must match an earlier open brace
158
159 /**
160 * Set the value of the parent of the current {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace.
161 * Implementation detail must be visible to all {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace
162 * types.
163 *
164 * @paramthis is the Javadoc tag for documenting the purpose of parameters parent reference to the {open braces start code blocks and must be matched with a close brace@link Question}close braces end code blocks and must match an earlier open brace of which thisthis means the current object (the implicit parameter)
165 * {open braces start code blocks and must be matched with a close brace@link AdventureBook}close braces end code blocks and must match an earlier open brace is a child.
166 */
167 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value setParentNdx(intint is the type for whole numbers and it is short for integer parentNdx) {open braces start code blocks and must be matched with a close brace
168 thisthis means the current object (the implicit parameter).parentNdx =this assignment operator makes the left side equal to the right side parentNdx;
169 }close braces end code blocks and must match an earlier open brace
170 }close braces end code blocks and must match an earlier open brace
171
172 //Uploaded on Mon Mar 29 21:39:45 EDT 2010
|
Download/View scg/ch12/AdventurePage.java