This lesson is a lot of fun.
We’ll integrate what we’ve learned recently about Strings, algorithms, and then functions.
And then we’ll get some practice approaching a few new problems using Strings.
But let’s start with some debugging practice!
Create a method named catchupGrading that accepts an array of double values and implements the following
catch-up grading policy.
Given a score on Quiz N, if a student does better on the next quiz (Quiz N + 1), then you should replace their
score on Quiz N with the average of their score on Quiz N and their score on Quiz N + 1.
Otherwise, their score on Quiz N is unchanged.
Return the number of times that the student does strictly better on the next quiz than they did on the previous one.
Scores are stored in the array in order.
So, given an array with the scores {100.0, 80.0, 90.0}, you would modify the array to contain
{100.0, 85.0, 90.0}, and return 1.
A few hints about how to approach this problem. You'll want to use a loop to go through each pair of scores in the array, so in the loop you'll be examining both one quiz and the next. This loop is similar to but a bit different from the one that we're used to writing. Your function should modify the values in the passed array.
First, we have a few new things to cover.
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.
Strings Are ImmutableStrings Are ImmutableOne of the important things to understand about Java’s Strings 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:
Write a function called reverse. It should accept a single String argument and return that String, reversed!
There are several ways to approach this problem—so have fun!
String AlgorithmsString AlgorithmsNow let’s have some fun and write a few new algorithms that work on Strings!
String Character SearchString Character SearchFirst, let’s try and write a function that determines if a String contains a particular character.
We’ll sketch out our algorithm first, explore some potentially useful String methods, and construct and test a solution.
String ParsingString ParsingFrequently 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.
Write a method name flipHalves that returns a passed String rearranged as follows.
You should move the second half of the string to the front, and the first half to the back.
So given the String "CS" you would return "SC", and given the String "testme" you would return "tmetes".
However, if the length of the String is odd the middle character should remain in place.
So given the String "CS124" you would return "241CS".
If the passed String is empty, just return the empty String.
You will definitely want to review the substring String method, and also spend some time experimenting with it
before you begin crafting your solution.
You also may want to consider odd and even length Strings separately.
Words from languages that we are unfamiliar with can be difficult to pronounce correctly. Phonetic pronunciation guides can help make them more accessible to us. In this problem we'll complete one part of a pronunciation guide for words in the Hawaiian language.
Consonants in the Hawaiian language are pronounced similarly to the English versions. The only exception is 'w', which is pronounced as a "v" if it follows an 'i' or 'e' and pronounced as "w" otherwise.
Complete a method named rewriteW which, given a non-null String, rewrites all the "w"s according to the rule
above.
For example, given the String "wahine" you would return "wahine", since the "w" is not preceded by an 'i' or 'e'.
Given the String "iwa" you would return "iva", since the "w" is preceded by an 'i'.
You should treat upper and lowercase characters equivalently, and your return String should be all lowercase.
Joy Buolamwini is a self-described poet of code who, through her art, voice, and research, is drawing attention to important social implications of the widespread use of artificial intelligence. Watch her speak for herself, in words both beautiful and powerful:
If you have a few more minutes, watch her TED Talk on algorithmic bias—what she calls “the coded gaze”.
Need more practice? Head over to the practice page.