Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
7.4.8. Free Response - StringFormatter B¶
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 https://apstudents.collegeboard.org/courses/ap-computer-science-a/free-response-questions-by-year.
This question involves the process of taking a list of words, called
wordList, and producing a formatted string of a specified length.
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.
184.108.40.206. Part B¶
basicGapWidth, which returns the basic gap width as defined above.
totalLetters works as specified regardless of what you wrote in part (a).
You must use
totalLetters appropriately to receive full credit.
/** 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)
220.127.116.11. How to Solve Part B¶
Click to reveal the algorithm and multiple choice questions that may help you write your solution.
18.104.22.168. Put the Code in Order¶
22.214.171.124. Write the Code¶
Finish writing the
basicGapWidth method below so that it returns the size that the gap should be. The
main method below will test your code to check that you solved it correctly.