Skip to main content
← Intro to CS (Java) Study Guide

Unit 4 · Steady

Working With Text

Text that you can look inside but never change. Counting from zero takes some getting used to, and almost every early text bug is a position being one off.

What a strong answer looks like

A strong Unit 4 answer numbers the characters first and says whether the original text was changed, which it never is.

Topics in this unit

1

Positions in Text

Practice this topic →

Know

Characters are numbered from zero, so the last one sits at one less than the length. The length itself is never a valid position.

Apply

Write the numbers under the characters before answering anything about position.

Watch out

Asking for the character at the length, which is one past the end.

Study move

Number every character of a short word and name the first and last valid positions.

2

Taking Part of a Text

Practice this topic →

Know

Taking a portion includes the starting position and stops just before the ending one. Searching returns the position of the first match, or a negative number when there is none.

Apply

Mark both ends on paper before working out the result.

Watch out

Expecting the ending position to be included, which gives a portion one character short.

Study move

Pull out the part of a word before a chosen letter, then try it when the letter is absent.

3

Text Never Changes

Practice this topic →

Know

Every text operation hands back a new piece of text and leaves the original alone. If you do not store the result, nothing happens.

Apply

Store the result of every text operation.

Watch out

Calling an operation and expecting the variable to have changed, which silently does nothing at all.

Study move

Write two lines differing only in whether the result is stored, and predict both outputs.

4

Going Through Text

Practice this topic →

Know

Looking at every character means looping over the positions. Comparing neighbouring characters means stopping one position early.

Apply

Match the loop bound to what you are comparing.

Watch out

Comparing each character with the next one all the way to the end, which reaches past it.

Study move

Count how many neighbouring characters are the same, and check it on a one-letter word.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Writing positions down rather than counting in your head
  • Storing the result of every text operation
  • Adjusting the bound when comparing neighbours

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • StringBuilder. Mentioned in some sections for efficiency.
  • Character methods. Depth varies by course.

Mastery checklist

  • Name the first and last valid position in a word.
  • Take a portion with both ends marked.
  • Say what happens when a search finds nothing.
  • Explain why an unstored text operation does nothing.

Check yourself

  • Why is the length never a valid position?
  • Which end of a portion is left out?
  • What happens to the original when you change text?

Modeling drill

Write a method that returns the part of an address before the first space, and say what it does when there is no space.

PositionIndexPortionSearchUnchangeableTraversal