2-B: Strings
We start to delve into algorithms and basic AI with our first CollegeBoard-issued lab. We will learn about String manipulation so we can have a conversation with our computer.
Learning Targets
I can use common String methods to modify a string.
I can evaluate the contents of a String.
I can use a Scanner to take inputs.
I can isolate an object in the user's input and use it in Magpie's response.
Magpie Lab
CollegeBoard used to have three official projects, Magpie, Elevens, and PicLab. A few years ago, they dropped those four and started four new ones: Celebrity, Consumer Review, DataLab, and Steganography. But I still think Magpie is the best lab for starters.
What's a magpie?
A bid that can mimic speech.


Check out the chatbot in Activity 1:
Strings
Check out the official documentation.
Strings are a special data type in Java. They're not primitives but they behave a little differently than most instantiated objects. In many ways, they're an array of primitive char objects. The biggest difference is that Strings also have many helpful methods to change their formatting, to search and examine the Strings, and lots of other helpful tools.



StringExplorer
Create a new class in our project called StringExplorer and drop this code in:

Activity 2 Starter Code
Now we're going to add a few new classes to the project.
Magpie.java
Now write a commit message to bookmark these changes and push the new version to GitHub. Then following along with the exercises in Activity 2.
Activity 3: Better method
Magpie's current structure and use of .indexOf("something") >= 0 is full of logic errors. It's caps sensitive, it can't tell if you've entered no response at all, and it sees the word "no" inside of "know". Let's do better.
Let's drop these two methods into your Magpie class (they're overloaded):
Now we've got to update our getResponse method to use this instead of indexOf.
Let's use this chart to walk through what's happening:

Activity 4: Slice and dice
Okay, now you're ready to follow along the student guide with the class and ask some questions along the way.
Review
Let's review some basics before we move onto our next, advanced concept. Now's the time to hit codingbat, SoloLearn, Codecademy, or other training websites to practice. You want the basics down pat so you can focus on new concepts as we move forward.
Last updated
Was this helpful?