Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
String Info
Gives information about a string.

Files: StringInfo.java
Areas: General


StringInfo.java
1import java.io.*;
2
3public class StringInfo {
4 /**
5 * Return the number of 'a's plus the number of 'b's in s.
6 * Precondition: s != null.
7 */
8 public static int numAsAndBs(String s) {
9 int count= 0;
10 for(int i= 0; i < s.length(); i++) {
11 if(s.charAt(i) == 'a' || s.charAt(i) == 'b') {
12 count++;
13 }
14 }
15 return count;
16 }
17
18
19 /**
20 * A run is a sequence of the same character. Return the letter
21 * that has the longest run in s. For example, in "aaabbbbccaa",
22 * 'b' would be the result, since it occurs 4 times in a row.
23 * Precondition: s != null && s.length() > 0.
24 */
25 public static char longestRun(String s) {
26 char longest= s.charAt(0);
27 char tempLongest= s.charAt(0);
28 int count= 1;
29 int tempCount= 1;
30
31 for(int i= 1; i < s.length(); i++) {
32 if(s.charAt(i) == tempLongest) {
33 tempCount++;
34 } else {
35 if(tempCount > count) {
36 count= tempCount;
37 longest= tempLongest;
38 }
39 tempCount= 1;
40 tempLongest= s.charAt(i);
41 }
42 }
43 if(tempCount > count) {
44 count= tempCount;
45 longest= tempLongest;
46 }
47
48 return longest;
49 }
50
51
52 /**
53 * Return whether s contains every letter in t. For example,
54 * "abcd" contains all the letters in "bbdc", since every letter
55 * in the second String appears in the first.
56 * Precondition: s != null && t != null.
57 */
58 public static boolean contains(String s, String t) {
59 boolean contains= true;
60 for(int i= 0; contains && i < t.length(); i++) {
61 contains= contains && (s.indexOf(t.charAt(i)) >= 0);
62 }
63 return contains;
64 }
65
66
67 /**
68 * Return whether s contains every letter in t, including duplicate
69 * letters. For example, "abcd" does not contain all the letters in
70 * "bbdc", since there is only 1 'b' in "abcd".
71 * Precondition: s != null && t != null.
72 */
73 public static boolean containsAllOf(String s, String t) {
74 boolean containsAll= true;
75 for(int i= 0; containsAll && i < t.length(); i++) {
76 int loc= s.indexOf(t.charAt(i));
77 if (loc >= 0) {
78 s= s.substring(0, loc) + s.substring(loc + 1);
79 } else {
80 containsAll= false;
81 }
82 }
83 return containsAll;
84 }
85
86
87 /**
88 * Read a series of Strings from file f, and return the longest String,
89 * or null if the file is empty.
90 * Precondition: f != null.
91 */
92 public static String getLongest(String f) throws IOException {
93 BufferedReader br= new BufferedReader(new FileReader(f));
94 String longest= null;
95 String line= br.readLine();
96 while(line != null) {
97 if (longest == null || line.length() > longest.length()) {
98 longest= line;
99 }
100 line= br.readLine();
101 }
102 return longest;
103 }
104
105
106 /**
107 * Read a series of Strings from file f until one is found that
108 * is longer than the sum of the lengths of the previous two, or
109 * null if no such Strings are found.
110 * Precondition: f != null.
111 */
112 public static String getLongerThanPreviousTwo(String f) throws IOException {
113 BufferedReader br= new BufferedReader(new FileReader(f));
114
115 /* The result. null until a line meeting the criterion is found or the
116 * end of the file is reached.
117 */
118 String result= null;
119
120 String l1= br.readLine(); // the penultimate line.
121 String l2= br.readLine(); // the previous line.
122
123 String next= br.readLine(); // The next candidate.
124 while(next != null && result == null) {
125 if (next.length() > l1.length() + l2.length()) {
126 result= next;
127 }
128 l1= l2;
129 l2= next;
130 next= br.readLine();
131 }
132 return result;
133 }
134
135
136 /**
137 * Read a series of Strings from file f, and return (concatenated without
138 * newlines) all the Strings that are longer than the sum of the lengths
139 * of the two previous to them, or null if no such Strings are found.
140 * Precondition: f != null.
141 */
142 public static String getAllLongerThanPreviousTwo(String f) throws IOException {
143 BufferedReader br= new BufferedReader(new FileReader(f));
144
145 /* The result. null until a line meeting the criterion is found or the
146 * end of the file is reached.
147 */
148 String result= null;
149
150 String l1= br.readLine(); // the penultimate line.
151 String l2= br.readLine(); // the previous line.
152
153 String next= br.readLine(); // The next candidate.
154 while(next != null) {
155 if (next.length() > l1.length() + l2.length()) {
156 if (result == null) {
157 result= next;
158 } else {
159 result += next;
160 }
161 }
162
163 l1= l2;
164 l2= next;
165 next= br.readLine();
166 }
167 return result;
168 }
169
170
171}


Question 1
Why are there no instance fields?
Hints Solution

Question 2
Why are all of the methods declared static?
Solution

Question 3
How would you use one of these methods?
Solution

Question 4
What does the following code return?
StringInfo.numAsAndBs("abcdefbcdefa");
Solution

Question 5
Consider the line
for(int i= 0; i < s.length(); i++) {
which appears in the body of the method numAsAndBs at line 10.

How many times will s.length() be evaluated when we ask for StringInfo.numAsAndBs("abc")?
Solution

Question 6
Consider the line
for(int i= 0; i < s.length(); i++) {
which appears in the body of the method numAsAndBs at line 10.

How can the efficiency of this line be improved?
Solution

Question 7
How does longestRun work?
Solution

Question 8
Consider the line
for(int i= 1; i < s.length(); i++) {
which appears in the body of the method longestRun at line 31.

Is there anything unusual about the first line of this for loop?
If so, what?
Hints Solution