Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This problem is asking for me to write a method in the SentenceCounter class that will pass the test in the images above. Strings with

image text in transcribedimage text in transcribed

This problem is asking for me to write a method in the SentenceCounter class that will pass the test in the images above.

Strings with No Blanks? about the situations that eird situation is going to bre- . Not checking for this still behaves nicely and ions like this, we often ount value that would s happened. (In a at something strange has happened to Up to this point in writing firstBlankPosition (), we haven't worried about are out of the ordinary, but we always have to worry about what weird situ our code. For this method, the weird situation is when there are no blanks. Ne possibility would be a classic error. We want to be sure that the method still returns some reasonable value even if this situation occurs. In situations i if no blank is found. That clearly isn't a count ya return a value that signifies that something strange has happened. So, let's M firstBlankPosition () return -1 if no blank is found normally be returned. so it's a reasonable indication that something strange b. to deal with this, but, for now, this strategy will wa Let's write a separate test for this behavior, because we're going to need to use than the one the two previous tests have been using: need to use a different String @Test public void testFirstBlankPositionNoBlanks () ks In This sentence"); SentenceCounter sc = new SentenceCounter ("NoBlanksInThissent assertEquals(-1, sc.firstBlankPosition ()); name of the test and you This should compile, so just run it to get to RED. In order to fix the error, look at the message JUnit gives us. Click on the name of the test should see this in the frame below the JUnit frame: java.lang.String IndexOutOfBoundsException: String index out of range: 22 at at java.lang.String.charAt(String.java:558) at SentenceCounter.firstBlankPosition (SentenceCounter.java:115) TestSentenceCounter.testFirstBlankPositionNoBlanks (TestSentenceCountersaya. Bu If you read the messages from bottom to top, these lines give you the sequence of methods that were running when the test encountered a problem: our test (named testFirstBlankPositionNoBlanks (0) ) had called firstBlankPosition (0) which had called charAt(0. You can double click on any of those lines to have the editor show you exactly where it is in the code (except that you probably don't have the source code for the String class). We were within the loop of firstBlankPosition () when something went wrong as we tried to get a character out of our String. The top line of this output tries to tell you exactly what went wrong. "Index out of bounds exception" means that we tried to index Something (in this case a String) with an invalid position. It even tells you that the invalid index was 22 (our string only contains 22 characters so its positions are numbered from o to 21). Essentially, this means that our loop kept going! beyond the end of our String. As it is currently written, our loop stops when it encounters blank. Since our String doesn't contain any blanks, the loop just keeps going until it runs String to search

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions