id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

orion.a.smith@gmail.com apcsa-loops

Description:AP CS A - Looping problems (Unit 4)

These problems will get you more practice with Java while and for loops. Remember that loops all have the important characteristic of a loop control variable, which changes its value as the loop proceeds. In the case of a while loop, that might look like this:

boolean stillWorking = true; // loop control variable
while (stillWorking)
{
  // some other stuff happens here
  if (/* some condition is met */) {
    stillWorking = false;  // something changes to signal the loop should terminate
  }
  // maybe some other stuff happens afterward
}

A for loop in most languages really just packaging up the important components of a while loop. That said, for loops are often used to go through a defined set of numeric values, or numeric indexes. The structure of a for loop makes programmers remember to include important ideas like iteration and updates. Here's an example for loop:

String str = "hello world";
for (int i = 0; i < str.length(); i++)
{
  // some other stuff happens here
}

Looping with numbers and arithmetic


Remember the basic structure of a loop.  Either use while loops or for loops, but either way you are going to have a loop control variable and a condition that must be met in order to continue looping.  Because these problems are just about number sequences and arithmetic, they can even be completed before you work with Strings or with arrays.

apcsaLoopsAddInclusive  apcsaLoopsAddExclusive  apcsaLoopsAddEveryOtherInclusive  apcsaLoopsAddEveryOddInclusive  apcsaLoopsAddInclusiveEitherWay  apcsaLoopsAddMultiplyInclusive  apcsaLoopsSumFactors  apcsaLoopsIsPrime  apcsaLoopsFindPattern  apcsaLoopsCountPattern  apcsaLoopsWhichFib  apcsaLoopsZeroFraction  apcsaLoopsCollatzSequence 

Looping with String concatenation


Strings are a more complex animal than is described here, but one nice feature of Strings is that they can be built by "concatenating," or sequentially adding on, data to create an ever-increasing sequence of characters.  Remember to assign your String output to "" when you begin, then use the + operator to add more characters at the end.  This sequence of problems allows more flexibility than returning an int, since it tests the order in which your code is operating.  Here's an example:
// make "aaaaa" using a for loop
String output = "";
for (int i = 0; i < 5; i++) {
  // could also be written output += "a";
  output = output + "a";
}

apcsaLoopsConcatInclusiveAscending  apcsaLoopsConcatWithAllSpaces  apcsaLoopsConcatInclusiveAscendingWithSpaces  apcsaLoopsConcatExclusiveAscending  apcsaLoopsConcatInclusiveDescending  apcsaLoopsConcatInclusiveEitherWay  apcsaLoopsConcatInclusiveEvensAscending  apcsaLoopsConcatInclusiveNsAscending  apcsaLoopsConcatFactorsOfB 


String looping, but not concatenation

These problems challenge you to think about String data as sequences of characters that you look around inside using a loop, much like many of the stock CodingBat String problems.

apcsaLoopsSameCharacter  apcsaLoopsCharacterVariety  apcsaLoopsMaxCharacter  apcsaLoopsMaxIndex  apcsaLoopsMaxTwo 


Chatbot problems


The following String problems replicate some of the skills developed by the Magpie lab, one of the stock labs provided for the AP Computer Science A course. No, these problems are not as interesting as a functioning chatbot but they do work on String processing skills.


About Words

Many problems in this set will have specialized definitions of what a word is in a String.  Sometimes, a very loose definition is used like "anything that isn't a space character is part of a word."  Other times, you will see more specific restrictions like "letters count as parts of words, but numbers do not."  Pay attention to these definitions when doing the problems.  There's a reason for this: in real language processing, it's important to figure out this question especially when you think about characters like apostrophes, periods, commas and exclamation marks.

Harder problems, and challenges

Some of these problems are more difficult and are marked with "Hard" at the end of the method name.  You should try them, but also feel free to move on if you have trouble and come back later.  Some of the problems also have challenges listed with them, worth extra credit if you can do them.  The challenges are significantly harder than anything else we have done so far, though.

Indexing problems

apcsaChatbotsFindSpace  apcsaChatbotsNumSpaces  apcsaChatbotsCountWords  apcsaChatbotsCountWordsHard 

Word detection problems

apcsaChatbotsFirstWord  apcsaChatbotsWordExists  apcsaChatbotsPunctuatedWord  apcsaChatbotsLastWordHard 


Re-arranging problems

apcsaChatbotsRearrangeStartsIAm  apcsaChatbotsRearrangeContainsIAm  apcsaChatbotsRearrangeContainsIYou 
Authoring docs

Copyright Nick Parlante 2017 - privacy