From ggc
|
01 packagepackage is used to name the directory or folder a class is in intermediate;
02
03
04 importimport means to make the classes and/or packages available in this program java.io.*;
05 importimport means to make the classes and/or packages available in this program java.util.*;
06
07 /**thisthis means the current object (the implicit parameter) classclass is a group of fields and methods used for making objects gets average word lengths*/
08 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 Smarter
09 {open braces start code blocks and must be matched with a close brace
10 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false areTooSimilar(String passage1, String passage2)
11 {open braces start code blocks and must be matched with a close brace
12 Scanner one=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(passage1);
13 Scanner two=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(passage2);
14 ArrayList<String> oneWord=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<String>();
15 ArrayList<String> twoWord=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor ArrayList<String>();
16 whilewhile is a looping structure for executing code repeatedly(one.hasNext())
17 {open braces start code blocks and must be matched with a close brace
18 String single=this assignment operator makes the left side equal to the right sideone.next().toLowerCase();
19 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 trueoneWord.contains(single)) oneWord.add(single);
20 }close braces end code blocks and must match an earlier open brace
21 whilewhile is a looping structure for executing code repeatedly(two.hasNext())
22 {open braces start code blocks and must be matched with a close brace
23 String single=this assignment operator makes the left side equal to the right sidetwo.next().toLowerCase();
24 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 truetwoWord.contains(single)) twoWord.add(single);
25 }close braces end code blocks and must match an earlier open brace
26 doubledouble is the type for numbers that can contain decimal fractions same=this assignment operator makes the left side equal to the right side0;
27 forfor is a looping structure for repeatedly executing a block of code(String single: oneWord)
28 {open braces start code blocks and must be matched with a close brace
29 ifif executes the next statement only if the condition in parenthesis evaluates to true(twoWord.contains(single))
30 same++this is the increment operator, which increases the variable by 1;
31 }close braces end code blocks and must match an earlier open brace
32 ifif executes the next statement only if the condition in parenthesis evaluates to true(oneWord.size()==this is the comparison operator which evaluates to true if both sides are the same0 ||this is boolean or, meaning if either or both are true then the result is true twoWord.size()==this is the comparison operator which evaluates to true if both sides are the same0) returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
33 ifif executes the next statement only if the condition in parenthesis evaluates to true(same/oneWord.size()>0.9 ||this is boolean or, meaning if either or both are true then the result is true same/twoWord.size()>0.9)
34 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
35 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
36 //fill in here
37 }close braces end code blocks and must match an earlier open brace
38
39 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)
40 {open braces start code blocks and must be matched with a close brace
41 String filename=this assignment operator makes the left side equal to the right sidenullnull is the value used to refer to a non-existant object;
42 Scanner console=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(System.in);
43 whilewhile is a looping structure for executing code repeatedly(filename==this is the comparison operator which evaluates to true if both sides are the samenullnull is the value used to refer to a non-existant object)
44 {open braces start code blocks and must be matched with a close brace
45 System.out.println("Enter in a file name.");
46 filename=this assignment operator makes the left side equal to the right sideconsole.next();
47 trytry is for executing a code block that may experience exceptions (errors)
48 {open braces start code blocks and must be matched with a close brace
49 File file=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor File(filename);
50 Scanner fromFile=this assignment operator makes the left side equal to the right sidenewnew is used to create objects by calling the constructor Scanner(file);
51 doubledouble is the type for numbers that can contain decimal fractions totalChars=this assignment operator makes the left side equal to the right side0;
52 intint is the type for whole numbers and it is short for integer totalWords=this assignment operator makes the left side equal to the right side0;
53 whilewhile is a looping structure for executing code repeatedly(fromFile.hasNext())
54 {open braces start code blocks and must be matched with a close brace
55 String oneWord=this assignment operator makes the left side equal to the right sidefromFile.next();
56 totalWords++this is the increment operator, which increases the variable by 1;
57 totalChars+=this increases the variable on the left by the value on the rightoneWord.length();
58 }close braces end code blocks and must match an earlier open brace
59 ifif executes the next statement only if the condition in parenthesis evaluates to true(((doubledouble is the type for numbers that can contain decimal fractions)totalChars)/totalWords>=this evaluates to true if the left side is not less than the right side6.75)
60 {open braces start code blocks and must be matched with a close brace
61 System.out.println("The author is smarter than a fifth grader");
62 }close braces end code blocks and must match an earlier open brace
63 elseelse is what happens when the if condition is false
64 {open braces start code blocks and must be matched with a close brace
65 System.out.println("Written by an elementary student.");
66 }close braces end code blocks and must match an earlier open brace
67 }close braces end code blocks and must match an earlier open brace
68 catchcatch means to handle an exception that may occur(Exception e)
69 {open braces start code blocks and must be matched with a close brace
70 System.out.println("Sorry, "+adds two numbers together or concatenates Strings togetherfilename+adds two numbers together or concatenates Strings together" not found, please try again.");
71 filename=this assignment operator makes the left side equal to the right sidenullnull is the value used to refer to a non-existant object;
72 }close braces end code blocks and must match an earlier open brace
73 }close braces end code blocks and must match an earlier open brace
74
75 }close braces end code blocks and must match an earlier open brace
76 }close braces end code blocks and must match an earlier open brace
|
Download/View intermediate/Smarter.java