From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch10;
02
03 importimport means to make the classes and/or packages available in this program java.util.Collections;
04 importimport means to make the classes and/or packages available in this program java.util.Scanner;
05
06 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects SentinelControlledLoop {open braces start code blocks and must be matched with a close brace
07 /**
08 * staticstatic means that an instance is not required for access (class level access) means there is only one; keyboard can be used in multiple
09 * methods
10 */
11 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) Scanner keyboard =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(System.in);
12
13 /**
14 * Get a line from the user. Prints the prompt on the console followed
15 * by a space. Then waits forfor is a looping structure for repeatedly executing a block of code user to enter a line and returns the
16 * full line of text to the calling method.
17 *
18 * @paramthis is the Javadoc tag for documenting the purpose of parameters prompt the prompt to print forfor is a looping structure for repeatedly executing a block of code the user.
19 *
20 * @returnnull the line entered by the user (everything up to but not
21 * including the <returnreturn means to provide the result of the method and/or cease execution of the method immediately> key)
22 */
23 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) String getLine(String prompt) {open braces start code blocks and must be matched with a close brace
24 System.out.print(prompt);
25 System.out.print(" ");
26 returnreturn means to provide the result of the method and/or cease execution of the method immediately keyboard.nextLine();
27 }close braces end code blocks and must match an earlier open brace
28
29 /**
30 * Main program. Uses getLine to prompt user and read lines in a
31 * sentinel controlled loop. User enters the sentinel value "done"
32 * when they want to quit. All other lines are converted to upper-casecase is used with switch for multiple alternatives (like if/else if...)
33 * and echoed back.
34 *
35 * @paramthis is the Javadoc tag for documenting the purpose of parameters args command-line arguments - ignored by thisthis means the current object (the implicit parameter) program
36 */
37 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 mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
38 String line =this assignment operator makes the left side equal to the right side "";
39 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to trueline.equalsIgnoreCase("done")) {open braces start code blocks and must be matched with a close brace
40 line =this assignment operator makes the left side equal to the right side getLine("Line to capitalize ('done' to finish):");
41 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueline.equalsIgnoreCase("done")) {open braces start code blocks and must be matched with a close brace
42 System.out.println(line.toUpperCase());
43 }close braces end code blocks and must match an earlier open brace
44 }close braces end code blocks and must match an earlier open brace
45 }close braces end code blocks and must match an earlier open brace
46 }close braces end code blocks and must match an earlier open brace
47
48 //Uploaded on Mon Mar 29 21:42:09 EDT 2010
|
Download/View scg/ch10/SentinelControlledLoop.java