Lab 05 10 pts Week 2 Due Apr 10

Lab 05: Text Analyzer

String methods, content comparison, and boolean classification

string-methods equals boolean-logic charAt substring
Clone from GitHub
3 checkpoints 5 autograder 2 timeliness
Prerequisites: lesson-1-7

After this lab, you will be able to:

  • Use length(), charAt(), substring(), and toUpperCase() to inspect and transform strings
  • Compare string content with .equals() instead of ==
  • Store boolean results in variables and combine them with &&, ||, !
  • Classify text based on multiple conditions

What You’re Building

You will write a text analyzer that takes a string and reports its length, first and last characters, whether it contains a keyword, and a classification based on boolean logic (e.g., “short and uppercase” vs. “long and mixed case”). This covers the essential String methods and the .equals() rule that trips up every Java beginner.


Concepts and Misconceptions

Concept Common Mistake What the Test Catches
.equals() vs == Using == to compare strings (compares references, not content) Classification is wrong despite identical-looking strings
charAt indexing Using charAt(length) instead of charAt(length - 1) for last char StringIndexOutOfBoundsException at runtime
substring range Thinking substring(0, 3) returns 4 characters (it returns 3) Extracted text is one character too short or too long
Boolean variables Inlining complex conditions instead of storing in named booleans Logic errors that are hard to trace

Checkpoints

Checkpoint 1: Extract Length, First Character, Last Character

What to do: Read a string, then print its length(), charAt(0), and charAt(length - 1) on labeled lines.

What the test checks: For "Hello", output includes Length: 5, First: H, Last: o.

Debugging tip: If you get a StringIndexOutOfBoundsException, you are passing an out-of-range index. Remember: indices run from 0 to length() - 1. An empty string has no valid index at all — handle that edge case if the test requires it.

Checkpoint 2: Check Content with .equals()

What to do: Check whether the string contains a keyword using .contains() and whether it equals a specific value using .equals(). Print the results as booleans.

What the test checks: The contains and equals results match expected values. Using == instead of .equals() will produce false even when the strings look identical.

Debugging tip: If .equals() returns false but the strings look the same in your print output, check for invisible trailing whitespace or newline characters. Print the strings with brackets to reveal hidden characters: "[" + text + "]".

Checkpoint 3: Classify Using Boolean Variables and Compound Conditions

What to do: Store conditions in named boolean variables (e.g., boolean isShort = text.length() < 10;, boolean isUpper = text.equals(text.toUpperCase());). Combine them to classify the text and print the classification.

What the test checks: The classification label matches expected output for several test inputs. All boolean variables must be declared and used — the test inspects the source.

Debugging tip: If your logic seems right but the classification is wrong, print each boolean variable separately. One of them is evaluating to the opposite of what you expect. Isolating each condition makes the bug obvious.


How to Debug

  1. Print with brackets. When string comparisons fail mysteriously, print "[" + str + "]" to see if whitespace or newline characters are hiding at the boundaries. This is the fastest way to find invisible-character bugs.

  2. Test .equals() in both directions. If str might be null, str.equals("target") throws a NullPointerException but "target".equals(str) safely returns false. Put the known non-null string on the left.

  3. Break compound conditions into pieces. If isShort && isUpper gives the wrong result, print isShort and isUpper individually. Never try to debug a compound boolean by staring at it — decompose it.


Scoring

Component Points Criteria
Checkpoints 3 1 pt each. Binary: the checkpoint test passes or it does not.
Autograder 5 Correctness across all test cases. Partial credit by proportion of tests passed.
Timeliness 2 Full credit if submitted by the due date. 0 if late.
Total 10