< Unit 6 - ArrayLists and String Methods ('22-'23)

Lesson 7: String Methods

45 minutes

Overview

How can I analyze parts of a String?

Students explore the structure of a String and discover the IndexOutOfBoundsException. Students learn about text segmentation and use the indexOf() and substring() methods to implement algorithms to break text into words or sentences and apply natural language processing techniques.

CSA Conceptual Framework
      • VAR-1.E.10 - A String object has index values from 0 to length – 1. Attempting to access indices outside this range will result in an IndexOutOfBoundsException.
      • VAR-1.E.12 - The following String methods and constructors—including what they do and when they are used—are part of the Java Quick Reference: • String(String str) — Constructs a new String object that represents the same sequence of characters as str • int length() — Returns the number of characters in a String object • String substring(int from, int to) — Returns the substring beginning at index from and ending at index to - 1 • String substring(int from) — Returns substring(from, length()) • int indexOf(String str) — Returns the index of the first occurrence of str; returns -1 if not found • boolean equals(String other) — Returns true if this is equal to other; returns false otherwise • int compareTo(String other) — Returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other
      • VAR-1.E.13 - A string identical to the single element substring at position index can be created by calling substring(index, index + 1).
      • VAR-1.E.2 - String objects are immutable, meaning that String methods do not change the String object.

Agenda

Objectives

Students will be able to:
  • Explain how characters in a String are indexed
  • Use methods in the String class to find the location of a specified substring within a String and to retrieve substrings

Preparation

  • Check the Teacher's Lounge for verified teachers on the CSA Forum to find additional strategies or resources shared by fellow teachers

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the students

Vocabulary

  • IndexOutOfBoundsException - An error that occurs when a program attempts to access an index that is negative or an index that is greater than or equal to the length of the object
  • text segmentation - the process of dividing text into words, sentences, or topics

Teaching Guide

Warm Up (5 minutes)

Text Segmentation

Remarks

Text segmentation is a technique used in natural language processing to identify individual words in a sentence and break up paragraphs into individual sentences.

Do This: Click through the animated slide to define text segmentation and introduce segmentation problems.

Discuss: Click through the animated slide to display the prompts.

  • What separates the end of one word from the beginning of the next?
  • What separates the end of one sentence from the beginning of the next?
  • What steps would our programs need to take to obtain a word from a sentence?
  • What about a sentence from a paragraph?

Discussion Goal: Students note that spaces separate words, and punctuation, such as periods, question marks, and exclamation points, separate sentences. Students suggest taking the characters between the spaces and punctuation marks to obtain individual words or sentences.

Activity (35 minutes)

Finding Words and Sentences (15 minutes)

Remarks

We previously learned that Strings are objects. Let’s take a look at how Strings are structured and some of the methods in the String class.

Do This: Review the lesson objectives.

Do This: Direct students to Level 1 on Code Studio to investigate the program with a partner. Students make the changes to the program as prompted.

Discuss: Click through the animated slide to display the prompts.

  • What do you notice about the code in this program?
  • What do you wonder about the code in this program?

Discussion Goal: Students notice that the indexOf() method returns the index of the substring within the given String and that there are two versions of the substring() method. Students also notice that the indexOf() method returns -1 if the substring is not found within the given String. Students realize that the two-parameter version of the substring() method returns the characters between the specified index values and that the one-parameter version returns the characters starting at the specified index values to the end of the String. Students may wonder if the IndexOutOfBoundsException is similar to the ArrayIndexOutOfBoundsException.

Do This: Click through the animated slide to explain how Strings are indexed and demonstrate the IndexOutOfBoundsException.

Do This: Click through the animated slide to demonstrate the indexOf() method.

Do This: Click through the animated slide to demonstrate the substring() method.

Do This: Click through the animated slide to demonstrate using the substring() method to obtain a single element in a String.

Do This: Explain that String objects are immutable and that String methods do not change the String object.

Using String Methods (20 minutes)

Remarks

In natural language processing, keyword extraction and part-of-speech tagging is often used to identify common words or their parts of speech. These techniques are often used as the first step to creating meaning of text.

Do This: Click through the animated slide to introduce keyword extraction and how it is used.

Do This: Click through the animated slide to introduce part-of-speech tagging and how it is used.

Do This: Have students write pseudocode for obtaining individual words from text.

Do This: Direct students to Level 2 on Code Studio to complete Levels 2, 3, and 4. Students complete a Check for Understanding on Level 2, then continue to Level 3 to implement their algorithm. On Level 4, students complete a choice level to use the indexOf() and substring() methods.

Wrap Up (5 minutes)

Glows, Grows, Want-to-Knows

Remarks

You have used your software engineering skills to write algorithms using natural language processing techniques.

Discuss: Click through the animated slide to display the prompts.

  • What was awesome about writing your code?
  • What is one action you can take to improve your code?
  • What questions do you have about today?

Discussion Goal: Students share aspects of their program they enjoyed or strengths of writing algorithms and identify areas of improvement to strengthen their programming skills. Students also share any questions or misconceptions they may have.

Do This: Review the concepts covered in this lesson.

Display: Key Vocabulary


Assessment: Check for Understanding

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

AP Classroom Topic Questions

To assign questions from the AP Classroom Question Bank that align with this lesson, create a custom quiz in AP Classroom by searching the Question Bank for the Essential Knowledge statements listed at the top of this lesson plan. You can find instructions and video demonstrations to do this on AP Central.

Creative Commons License (CC BY-NC-SA 4.0).

This work is available under a Creative Commons License (CC BY-NC-SA 4.0).

If you are interested in licensing Code.org materials for commercial purposes contact us.