Unit 4B - Free Response - StringFormatter B

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java1 class period1 exercise4 graded tests

Files

README.mdDownload

Unit 4B - Free Response - StringFormatter B

Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.

The following is a free response question from 2016. It was question 4 part B on the exam. You can see all the free response questions from past exams at Past AP CSA Exams .

This question involves the process of taking a list of words, called wordList, and producing a formatted string of a specified length. The list wordList contains at least two words, consisting of letters only. When the formatted string is constructed, spaces are placed in the gaps between words so that as many spaces as possible are evenly distributed to each gap. The equal number of spaces inserted into each gap is referred to as the basic gap width. Any leftover spaces are inserted one at a time into the gaps from left to right until there are no more leftover spaces.

The following three examples illustrate these concepts. In each example, the list of words is to be placed into a formatted string of length 20.

2016FRQ4A1

Part B

  1. Write the StringFormatter method basicGapWidth, which returns the basic gap width as defined above.

2016FRQ4B1

Assume that totalLetters works as specified regardless of what you wrote in part (a). You must use totalLetters appropriately to receive full credit.

Complete method basicGapWidth below.

/** Returns the basic gap width when wordList is used to produce
*  a formatted string of formattedLen characters.
*  Precondition: wordList contains at least two words, consisting of letters only.
*            formattedLen is large enough for all the words and gaps.
*/
public static int basicGapWidth(List<String> wordList,
                                 int formattedLen)

How to Solve Part B

To calculate basicGapWidth we need to find the number of spaces left after the characters fill the formattedLen and divide that by the number of gaps between words. We can use totalLetters (written in part A) to get the total number of characters for all the strings in wordList. The number of gaps between words is the number of words in wordList minus 1. The basicGapWidth is the number of spaces left divided by the number of gaps between words. Remember that if we do an integer division any fractional part will be thrown away, which is what we want to happen in this case.

For example, if formattedLen is 20 and wordList is [“AP”, “COMP”, “SCI”, “ROCKS”] then the number of spaces left is 20 - 14 = 6 and the number of gaps is 4 - 1 = 3. The result is 6 / 3 = 2.

If formattedLen is 20 and wordList is [“GREEN”, “EGGS”, “AND”, “HAM”] then the number of spaces left is 20 - 15 = 5 and the number of gaps is 4 - 1 = 3 so 5 / 3 = 1. There will be two extra spaces left over.

If formattedLen is 20 and wordList is [“BEACH”, “BALL”] then the number of spaces left is 20 - 9 = 11 and the number of gaps is 2 - 1 = 1 so 11 / 1 = 11.

Check your understanding: How do you access the number of items in an ArrayList<String> called list? — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/stringFormatterB.html#fr_formatterb_1

Check your understanding: True or False: A loop is required to correctly solve this problem. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/stringFormatterB.html#fr_formatterb_2

Put the Code in Order

Check your understanding: The following has the correct code to solve this problem, but also contains extra code that isn’t needed in a correct solution. — answer it on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/stringFormatterB.html#stringFormatterB

Write the Code

Exercise 1

Finish writing the basicGapWidth method so that it returns the size that the gap should be. The main method will test your code to check that you solved it correctly.

Open the 01-lcfrsbasicGapWidth folder and edit StringFormatter.java. Press Run to try it; Run Tests checks it (4 tests).

For teachers

Unit 4B - Free Response - StringFormatter B

Students read CSAwesome2's “Unit 4B - Free Response - StringFormatter B” in CS Room and work through its 1 Java coding exercise.

  • Time: 1 class period
  • Tests: 4 in 1 graded exercise, set up for you when you import the lesson

Goals

  • Practice for Unit 4: Data Collections.

Before class

In class

  1. Students read README.md. Questions that need CS Awesome's interactive widgets link back to it.
  2. Students do exercise 01 in its folder, pressing Run to try their code.
  3. Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.

Exercises

FolderFileGradingTests
01-lcfrsbasicGapWidthStringFormatter.javaAutograded4

Teachers can download a reference solution after signing in.