Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Contains
This method is part of the solution to an assignment given out in the fall of 2003. Here is the description of the method as given in the assignment:
/**
* Return whether s contains every letter in t. For example,
* "abcd" contains all the letters in "bbdc", since every letter
* in the second String appears in the first.
* Precondition: s != null && t != null.
*/
public static boolean contains(String s, String t)


Download files: Contains.java
Areas: Introduction to loops.


Contains.java
1/**
2 * Return whether s contains every letter in t. For example,
3 * "abcd" contains all the letters in "bbdc", since every letter
4 * in the second String appears in the first.
5 * Precondition: s != null && t != null.
6 */
7public static boolean contains(String s, String t) {
8  boolean contains= true;
9  for(int i= 0; contains && i < t.length(); i++) {
10    contains= contains && (s.indexOf(t.charAt(i)) >= 0);
Displaying 10 of 13 lines
View full source


Question 1
When this method returns, the variable contains has the obvious meaning: it is true if and only if s contains every letter of t. What is the meaning of contains when tested at the top of the for-loop on line 9, while i=n and t.length()>n?
Hints Solution