Skip to content

Commit

Permalink
Merge pull request bhoffman0#28 from emkbrown/master
Browse files Browse the repository at this point in the history
restoring reveals/mc for string formatter a & b that got overwritten
  • Loading branch information
bhoffman0 authored Aug 21, 2020
2 parents 27c1589 + 344095e commit b9834c5
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 51 deletions.
92 changes: 66 additions & 26 deletions _sources/Unit7-ArrayList/2016freeresponseQ4A.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,78 @@ Complete method ``totalLetters`` below.
How to Solve Part A
=====================

We need to return the total number of letters for all of the strings in ``wordList``. We will need to create an
integer variable to keep track of the number of letters and initialize it to 0. Then we will loop through all of the strings in ``wordList`` and
add the length of the current string to the number of letters. When the loop is finished we will return the number of letters.
Click to reveal the algorithm and multiple choice problems that may help you write your solution.

.. reveal:: algorithm_stringFormatterA
:showtitle: Reveal Algorithm
:hidetitle: Hide Algorithm
:optional:

We need to return the total number of letters for all of the strings in ``wordList``. We will need to create an
integer variable to keep track of the number of letters and initialize it to 0. Then we will loop through all of the strings in ``wordList`` and
add the length of the current string to the number of letters. When the loop is finished we will return the number of letters.


.. reveal:: fr_formatter_r1
:showtitle: Reveal Problems
:hidetitle: Hide Problems
:optional:

.. mchoice:: fr_formatter_1
:answer_a: while
:answer_b: for
:answer_c: for-each
:correct: c
:feedback_a: A while loop is the best choice when you don't know the number of times you need to loop.
:feedback_b: You could use a for loop, but there is a more concise option since you are not changing any values of wordList.
:feedback_c: Correct! A for-each loop is the most concise way to access every string in wordList to keep track of numLetters

Which loop would be best for this problem?

.. mchoice:: fr_formatter_2
:answer_a: str.size()
:answer_b: str.length()
:answer_c: str.length
:correct: b
:feedback_a: .size() is not the correct method call to find the length of a string. .size() is used with ArrayLists. Try again!
:feedback_b: Correct! str.length() will return the length of String str.
:feedback_c: Almost! length() is a method call, so parentheses are required.

What is the correct way to access the length of a String str?



Put the Code in Order
======================

.. parsonsprob:: 2016Q4A
:numbered: left
:adaptive:
.. reveal:: stringFormatterA_parsons
:showtitle: Reveal Mixed Up Code
:hidetitle: Hide Mixed Up Code

The following has the correct code to solve this problem, but also contains extra code that isn't needed in a correct solution. Drag the needed blocks from the left into the correct order on the right and indent them as well. Check your solution by clicking on the <i>Check Me</i> button. You will be told if any of the blocks are in the wrong or are in the wrong order. You will also be told if the indention is wrong.
-----
public static int totalLetters(List<String> wordList)
{
=====
int numLetters = 0;
=====
for (String s : wordList)
=====
for (String s in wordList) #paired
=====
numLetters = numLetters + s.length();
=====
numLetters = numLetters + wordList.length(); #paired
=====
return numLetters;
=====
return numletters; #paired
=====
}
.. parsonsprob:: stringFormatterA
:numbered: left
:adaptive:

The following has the correct code to solve this problem, but also contains extra code that isn't needed in a correct solution. Drag the needed blocks from the left into the correct order on the right and indent them as well. Check your solution by clicking on the <i>Check Me</i> button. You will be told if any of the blocks are in the wrong or are in the wrong order. You will also be told if the indention is wrong.
-----
public static int totalLetters(List<String> wordList)
{
=====
int numLetters = 0;
=====
for (String s : wordList)
=====
for (String s in wordList) #paired
=====
numLetters = numLetters + s.length();
=====
numLetters = numLetters + wordList.length(); #paired
=====
return numLetters;
=====
return numletters; #paired
=====
} //end method

Write the Code
==================
Expand Down
87 changes: 62 additions & 25 deletions _sources/Unit7-ArrayList/2016freeresponseQ4B.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,40 +51,77 @@ Complete method ``basicGapWidth`` below.
How to Solve Part B
=====================

Click to reveal the algorithm and multiple choice questions that may help you write your solution.

.. reveal:: algorithm_stringFormatterB
:showtitle: Reveal Algorithm
:hidetitle: Hide Algorithm
:optional:

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.
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.

.. reveal:: fr_formatterb_r1
:showtitle: Reveal Problems
:hidetitle: Hide Problems
:optional:

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.
.. mchoice:: fr_formatterb_1
:answer_a: list.length()
:answer_b: list.size
:answer_c: list.size()
:correct: c
:feedback_a: .length() is used with Arrays to return the number of items. Try again!
:feedback_b: .size is a method call, so parentheses are required.
:feedback_c: Correct! ArrayLists use .size() to return the number of items in a list.

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.
How do you access the number of items in an ArrayList<String> called list?

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.
.. mchoice:: fr_formatterb_2
:answer_a: True
:answer_b: False
:correct: b
:feedback_a: Incorrect. You do not need to access any of the individual items in wordList.
:feedback_b: Correct! All you need is the size of wordList, which you can find without a loop.

True or False: A loop is required to correctly solve this problem.

Put the Code in Order
======================

.. parsonsprob:: 2016Q4B
:numbered: left
:adaptive:
.. reveal:: stringFormatterB_parsons
:showtitle: Reveal Mixed Up Code
:hidetitle: Hide Mixed Up Code

The following has the correct code to solve this problem, but also contains extra code that isn't needed in a correct solution. Drag the needed blocks from the left into the correct order on the right and indent them as well. Check your solution by clicking on the <i>Check Me</i> button. You will be told if any of the blocks are in the wrong or are in the wrong order. You will also be told if the indention is wrong.
-----
public static int basicGapWidth(List<String> wordList,
int formattedLen)
=====
{
=====
int numSpaces = formattedLen - totalLetters(wordList);
int numGaps = wordList.size() - 1;
=====
int numSpaces = formattedLen + totalLetters(wordList); #paired
int numGaps = wordList.length - 1;
=====
return numSpaces / numGaps;
=====
}
.. parsonsprob:: stringFormatterB
:numbered: left
:adaptive:

The following has the correct code to solve this problem, but also contains extra code that isn't needed in a correct solution. Drag the needed blocks from the left into the correct order on the right and indent them as well. Check your solution by clicking on the <i>Check Me</i> button. You will be told if any of the blocks are in the wrong or are in the wrong order. You will also be told if the indention is wrong.
-----
public static int basicGapWidth(List<String> wordList,
int formattedLen)
=====
{
=====
int numSpaces = formattedLen - totalLetters(wordList);
int numGaps = wordList.size() - 1;
=====
int numSpaces = formattedLen + totalLetters(wordList); #paired
int numGaps = wordList.length - 1;
=====
return numSpaces / numGaps;
=====
} //end method



Write the Code
Expand Down

0 comments on commit b9834c5

Please sign in to comment.