From ggc
|
01 packagepackage is used to name the directory or folder a class is in jam;
02 importimport means to make the classes and/or packages available in this program java.util.*;
03 importimport means to make the classes and/or packages available in this program java.io.*;
04 importimport means to make the classes and/or packages available in this program java.net.*;
05 importimport means to make the classes and/or packages available in this program wiki.Wiki;
06 /**
07 * All about my application here.
08 * @authorthis is the Javadoc tag for documenting who created the source code Jam Jenkins
09 */
10 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 Hangman
11 {open braces start code blocks and must be matched with a close brace
12 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)
13 {open braces start code blocks and must be matched with a close brace
14 String phrase=this assignment operator makes the left side equal to the right side"Hangman is easy.";
15 trytry is for executing a code block that may experience exceptions (errors)
16 {open braces start code blocks and must be matched with a close brace
17 Scanner fromFile=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(Wiki.getInputStream("Hangman.txt"));
18 ArrayList<String> allPhrases=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<String>();
19 fromFile.useDelimiter("\n");
20 whilewhile is a looping structure for executing code repeatedly(fromFile.hasNext())
21 {open braces start code blocks and must be matched with a close brace
22 allPhrases.add(fromFile.next().trim());
23 }close braces end code blocks and must match an earlier open brace
24 Random random=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Random();
25 phrase=this assignment operator makes the left side equal to the right sideallPhrases.get(random.nextInt(allPhrases.size()));
26 }close braces end code blocks and must match an earlier open brace
27 catchcatch means to handle an exception that may occur(IOException io)
28 {open braces start code blocks and must be matched with a close brace
29 io.printStackTrace();
30 }close braces end code blocks and must match an earlier open brace
31 String unknown=this assignment operator makes the left side equal to the right sidephrase.replaceAll("[a-zA-Z]", "*");
32 Scanner scanner=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(Wiki.in);
33 whilewhile is a looping structure for executing code repeatedly(unknown.indexOf("*")>=this evaluates to true if the left side is not less than the right side0)
34 {open braces start code blocks and must be matched with a close brace
35 Wiki.out.println("Enter next letter");
36 charchar is the type for a single letter or symbol and it is short for character letter=this assignment operator makes the left side equal to the right sidescanner.next().toLowerCase().charAt(0);
37 forfor is a looping structure for repeatedly executing a block of code(intint is the type for whole numbers and it is short for integer i=this assignment operator makes the left side equal to the right side0; i<phrase.length(); i++this is the increment operator, which increases the variable by 1)
38 {open braces start code blocks and must be matched with a close brace
39 charchar is the type for a single letter or symbol and it is short for character one=this assignment operator makes the left side equal to the right sidephrase.toLowerCase().charAt(i);
40 ifif executes the next statement only if the condition in parenthesis evaluates to true(one==this is the comparison operator which evaluates to true if both sides are the sameletter)
41 {open braces start code blocks and must be matched with a close brace
42 String firstPart=this assignment operator makes the left side equal to the right side"";
43 String lastPart=this assignment operator makes the left side equal to the right side"";
44 firstPart=this assignment operator makes the left side equal to the right sideunknown.substring(0, i);
45 ifif executes the next statement only if the condition in parenthesis evaluates to true(i+adds two numbers together or concatenates Strings together1<phrase.length())
46 lastPart=this assignment operator makes the left side equal to the right sideunknown.substring(i+adds two numbers together or concatenates Strings together1);
47 unknown=this assignment operator makes the left side equal to the right sidefirstPart+adds two numbers together or concatenates Strings togetherphrase.charAt(i)+adds two numbers together or concatenates Strings togetherlastPart;
48 //"H"+'a'+"ngman is easy."
49 //"*"+'a'+"***** ** ****.";
50 }close braces end code blocks and must match an earlier open brace
51 }close braces end code blocks and must match an earlier open brace
52 Wiki.out.println("Unknown is "+adds two numbers together or concatenates Strings togetherunknown);
53 }close braces end code blocks and must match an earlier open brace
54 }close braces end code blocks and must match an earlier open brace
55 }close braces end code blocks and must match an earlier open brace
|
Download/View jam/Hangman.java