Return to Index
Inheritance : 07/05/2022
Data Modeling 1 : 07/05/2022
Static : 07/01/2022
Encapsulation : 07/01/2022
Constructors : 06/30/2022
Objects, Continued : 06/30/2022
Introduction to Objects : 06/29/2022
Compilation and Type Inference : 06/29/2022
Practice with Collections : 06/28/2022
Maps and Sets : 06/28/2022
Lists and Type Parameters : 06/27/2022
Imports and Libraries : 06/27/2022
Multidimensional Arrays : 06/24/2022
Practice with Strings : 06/24/2022
null : 06/23/2022
Algorithms and Strings : 06/23/2022
Strings : 06/22/2022
Functions and Algorithms : 06/22/2022
Practice with Functions : 06/21/2022
More About Functions : 06/21/2022
Errors and Debugging : 06/20/2022
Functions : 06/20/2022
Practice with Loops and Algorithms : 06/16/2022
Algorithms I : 06/16/2022
Loops : 06/15/2022
Arrays : 06/15/2022
Compound Conditionals : 06/14/2022
Conditional Expressions and Statements : 06/14/2022
Operations on Variables : 06/13/2022
Variables and Types : 06/13/2022
Welcome to CS 124 : 06/13/2022
Algorithms and Strings
Java
Created By: Geoffrey Challen
/ Updated: 2022-05-26
This lesson is a lot of fun.
We'll integrate what we've learned recently about String
s, algortihms, and then functions.
And then we'll get some practice approaching a few new problems using String
s.
Odds and Ends
But first, we have a few new things to cover.
Casting
When we started working with variables and Java's eight primitive types, we observed that there were certain types of assignments that would fail.
For example:
But, there are also other kinds of assignments from one type to another that will succeed:
Let's discuss why that is, and how to can force Java to perform certain type changes when necessary.
Discuss type casting, both manual and automatic.
Interactive Walkthrough
Click on an icon below to start!
String
s Are Immutable
One of the important things to understand about Java's String
s is that none of the methods that we can call on them change that String
.
Instead, they always return a new String
that may be modified in some way.
Let's see that in action:
Demonstrate how String
operations don't change their contents.
Interactive Walkthrough
Click on an icon below to start!
Show how to complete the homework problem above. Feel free to cover multiple approaches!
String
Algorithms
Now let's have some fun and write a few new algorithms that work on String
s!
String
Character Search
First, let's try and write a function that determines if a String
contains a particular character.
We'll sketch our our algorithm first, explore some potentially useful String
methods, and construct and test a solution.
Show how to write a function that searches a String
for a character.
Sketch in comments first and then replace with code.
Interactive Walkthrough
Click on an icon below to start!
Structured String
Parsing
Frequently when computers work with text data, we are processing data that was itself created by a machine.
Or set up for machines to easily process.
One example is data stored in comma-separated-value format.
Let's say we wanted to track how many people were tested for some random respiratory illness each day.
We might save that data in a String
that looked like this:
(Note that this is not real data!)
Let's experiment with how we might work with this kind of data.
We'll use some existing String
methods and a few new ones.
We'll also use another unfamiliar method: Integer.parseInt
, which allows us to convert a String
to an int
.
Walk through some examples using the CSV data.
Use the same approach as previously: sketch in comments, then replace with code.
First, parse it.
Discuss Integer.parseInt
.
Then do something fun with data: total number of tests, or day with the most tests, or whatever.
Interactive Walkthrough
Click on an icon below to start!
Show how to complete the homework problem above. Feel free to cover multiple approaches!