<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-specifications-objectives" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Reading 4 Objectives</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_fad1628beb36"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:YZSw6s3C7ks", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 43.07, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V002900_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V002900/MIT600512016-V002900.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_fad1628beb36"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_fad1628beb36">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_fad1628beb36">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_fad1628beb36/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_7f24ae660515">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_7f24ae660515" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h4>Software in 6.005</h4>
<table class="table table-striped no-markdown">
<tbody>
<tr>
<th width="33%">Safe from bugs</th>
<th>Easy to understand</th>
<th>Ready for change</th>
</tr>
<tr>
<td>
Correct today and correct in the unknown future
</td>
<td>
Communicates clearly with future programmers, including future you
</td>
<td>
Designed to accommodate change without rewriting
</td>
</tr>
</tbody>
</table>
<h4>Objectives for Today</h4>
<p>In this reading, we’ll talk about specifications. </p>
<p>A specification is like a contract for part of your program, saying what it can count on from the rest of the program, and what it’s expected to do in return. Specifications help with all three of the big goals of this course: safety from bugs, ease of understanding, and readiness for change.</p>
<p>After today's reading, you should:</p>
<ul>
<li>Understand preconditions and postconditions in method specifications, and be able to write correct specifications;</li>
<li>Be able to write tests against a specification;</li>
<li>Know the difference between checked and unchecked exceptions in Java; and</li>
<li>Understand how to use exceptions for special results. </li>
</ul>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-why-specifications" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Why Specifications?</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_ab02e7ca4128"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:dw9ccK3kNT4", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 446.03, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003100_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003100/MIT600512016-V003100.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_ab02e7ca4128"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_ab02e7ca4128">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_ab02e7ca4128">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_ab02e7ca4128/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_5fcbddbfb798">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_5fcbddbfb798" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="introduction">Introduction</h2>
<div data-outline="introduction">
<p>Specifications are the linchpin of teamwork. It's impossible to delegate responsibility for implementing a method without a specification. The specification acts as a contract: the implementer is responsible for meeting the contract, and a client that uses the method can rely on the contract. In fact, we'll see that like real legal contracts, specifications place demands on both parties: when the specification has a precondition, the client has responsibilities too.</p>
<p>In this reading we'll look at the role played by specifications of methods. We'll discuss what preconditions and postconditions are, and what they mean for the implementor and the client of a method. We'll also talk about how to use exceptions, an important language feature found in Java, Python, and many other modern languages, which allows us to make a method's interface safer from bugs and easier to understand.</p>
</div>
<p>Before we dive into the structure and meaning of specifications…</p>
<h2 id="why_specifications">Why specifications?</h2>
<div data-outline="why_specifications">
<p>Many of the nastiest bugs in programs arise because of misunderstandings about behavior at the interface between two pieces of code. Although every programmer has specifications in mind, not all programmers write them down. As a result, different programmers on a team have <em>different</em> specifications in mind. When the program fails, it's hard to determine where the error is. Precise specifications in the code let you apportion blame (to code fragments, not people!), and can spare you the agony of puzzling over where a fix should go.</p>
<p>Specifications are good for the client of a method because they spare the task of reading code. If you're not convinced that reading a spec is easier than reading code, take a look at some of the standard Java specs and compare them to the source code that implements them.</p>
<p>Here's an example of one method from <a href="http://docs.oracle.com/javase/8/docs/api/?java/math/BigInteger.html"><code>BigInteger</code></a> — a class for representing integers up to arbitrary size without the size limit of primitive <code>int</code> — next to its code:</p>
<table class="side-by-side pull-margin">
<tbody>
<tr>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#add-java.math.BigInteger-">Specification from the API documentation</a>:</p>
<div class="panel panel-default">
<div class="panel-heading">
<p><strong>add</strong></p>
</div>
<div class="panel-body">
<p><code>public BigInteger add(BigInteger val)</code></p>
<p>Returns a BigInteger whose value is <code>(this + val)</code>.</p>
<p><strong>Parameters:</strong>
<br>
<code>val</code> - value to be added to this BigInteger.</p>
<p><strong>Returns:</strong>
<br>
<code>this + val</code></p>
</div>
</div>
</td>
<td>
<p><a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/math/BigInteger.java#l1207">Method body from Java 8 source</a>:</p>
<pre><code class="language-java hljs"><span class="hljs-keyword">if</span> (val.signum == <span class="hljs-number">0</span>)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>;
<span class="hljs-keyword">if</span> (signum == <span class="hljs-number">0</span>)
<span class="hljs-keyword">return</span> val;
<span class="hljs-keyword">if</span> (val.signum == signum)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> BigInteger(add(mag, val.mag), signum);
<span class="hljs-keyword">int</span> cmp = compareMagnitude(val);
<span class="hljs-keyword">if</span> (cmp == <span class="hljs-number">0</span>)
<span class="hljs-keyword">return</span> ZERO;
<span class="hljs-keyword">int</span>[] resultMag = (cmp > <span class="hljs-number">0</span> ? subtract(mag, val.mag)
: subtract(val.mag, mag));
resultMag = trustedStripLeadingZeroInts(resultMag);
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> BigInteger(resultMag, cmp == signum ? <span class="hljs-number">1</span> : -<span class="hljs-number">1</span>);</code></pre></td>
</tr>
</tbody>
</table>
<p>The spec for <code>BigInteger.add</code> is straightforward for clients to understand, and if we have questions about corner cases, the <code>BigInteger</code> class provides additional human-readable documentation. If all we had was the code, we'd have to read through the <code>BigInteger</code> constructor, <code>compareMagnitude</code>, <code>subtract</code>, and <code>trustedStripLeadingZeroInts</code> just as a starting point.</p>
<div class="panel panel-default panel-figure pull-right pull-margin no-markdown">
<div class="panel-body text-center">
<img src="/assets/courseware/v1/99b974f7a6565463edffbb838951480c/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-firewall.png" class="pull-right" width="196">
</div>
</div>
<p>Specifications are good for the implementer of a method because they give the implementor freedom to change the implementation without telling clients. Specifications can make code faster, too. We'll see that using a weaker specification can rule out certain states in which a method might be called. This restriction on the inputs might allow the implementor to skip an expensive check that is no longer necessary and use a more efficient implementation.</p>
<p>The contract acts as a <em>firewall</em> between client and implementor. It shields the client from the details of the <em>workings</em> of the unit — you don't need to read the source code of the procedure if you have its specification. And it shields the implementor from the details of the <em>usage</em> of the unit. The implementor doesn't have to ask every client how they plan to use the unit. This firewall results in <em>decoupling</em>, allowing the code of the unit and the code of a client to be changed independently, so long as the changes respect the specification — each obeying its obligation.</p>
<div class="clearfix"></div>
</div>
<h2 id="behavioral_equivalence">Behavioral equivalence</h2>
<div data-outline="behavioral_equivalence">
<p>Consider these two methods. Are they the same or different?</p><pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">findFirst</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> val)</span> </span>{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < arr.length; i++) {
<span class="hljs-keyword">if</span> (arr[i] == val) <span class="hljs-keyword">return</span> i;
}
<span class="hljs-keyword">return</span> arr.length;
}
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">findLast</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> val)</span> </span>{
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = arr.length -<span class="hljs-number">1</span> ; i >= <span class="hljs-number">0</span>; i--) {
<span class="hljs-keyword">if</span> (arr[i] == val) <span class="hljs-keyword">return</span> i;
}
<span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
}</code></pre>
<p>Of course the code is different, so in that sense they are different; and we've given them different names, just for the purpose of discussion. To determine <em>behavioral equivalence</em>, our question is whether we could substitute one implementation for the other.</p>
<p>Not only do these methods have different code, they actually have different behavior:</p>
<ul>
<li>when <code>val</code> is missing, <code>findFirst</code> returns the length of <code>arr</code> and <code>findLast</code> returns -1;</li>
<li>when <code>val</code> appears twice, <code>findFirst</code> returns the lower index and <code>findLast</code> returns the higher.</li>
</ul>
<p>But when <code>val</code> occurs at exactly one index of the array, the two methods behave the same: they both return that index. It may be that clients never rely on the behavior in the other cases. Whenever they call the method, they will be passing in an <code>arr</code> with exactly one element <code>val</code>. For such clients, these two methods are the same, and we could switch from one implementation to the other without issue.</p>
<p>The notion of equivalence is in the eye of the beholder — that is, the client. In order to make it possible to substitute one implementation for another, and to know when this is acceptable, we need a specification that states exactly what the client depends on.</p>
<p>In this case, our specification might be:</p><pre><p>static int find(int[] arr, int val)
<em>requires</em>: val occurs exactly once in arr
<em>effects</em>: returns index i such that arr[i] = val</p></pre>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_98dc26e0eaad" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-behave-nicely">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-behave-nicely" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-behave-nicely" class="problems-wrapper" role="group"
aria-labelledby="04-behave-nicely-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-behave-nicely" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-behave-nicely/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-behave-nicely-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-behave-nicely-problem-progress" tabindex="-1">
behave nicely
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-behave-nicely-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
static int findFirst(int[] a, int val) {
for (int i = 0; i &lt; a.length; i++) {
if (a[i] == val) return i;
}
return a.length;
}
</pre>
<pre class="code">
static int findLast(int[] a, int val) {
for (int i = a.length -1 ; i &gt;= 0; i--) {
if (a[i] == val) return i;
}
return -1;
}
</pre>
<p>If clients only care about calling the find method when they know <code>val</code> occurs exactly once in <code>a</code>, are <code>findFirst</code> and <code>findLast</code> behaviorally equivalent?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-behave-nicely_2_1">
<fieldset aria-describedby="status_04-behave-nicely_2_1">
<div class="field">
<input type="radio" name="input_04-behave-nicely_2_1" id="input_04-behave-nicely_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-behave-nicely_2_1-choice_0-label" for="input_04-behave-nicely_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-behave-nicely_2_1"> Yes
</label>
</div>
<div class="field">
<input type="radio" name="input_04-behave-nicely_2_1" id="input_04-behave-nicely_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-behave-nicely_2_1-choice_1-label" for="input_04-behave-nicely_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-behave-nicely_2_1"> No
</label>
</div>
<span id="answer_04-behave-nicely_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-behave-nicely_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-behave-nicely_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="behave nicely" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-behave-nicely" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-behave-nicely">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-behave-nicely-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-behave-nicely-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-behave-nicely-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-best-behavior">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-best-behavior" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-best-behavior" class="problems-wrapper" role="group"
aria-labelledby="04-best-behavior-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-best-behavior" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-best-behavior/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-best-behavior-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-best-behavior-problem-progress" tabindex="-1">
best behavior
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-best-behavior-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose clients only care that the find method should return:</p>
<p>- any index <code>i</code> such that <code>a[i] == val</code>, if <code>val</code> is in <code>a</code></p>
<p>- any integer <code>j</code> such that <code>j</code> is not a valid array index, otherwise</p>
<p>In this case, are <code>findFirst</code> and <code>findLast</code> behaviorally equivalent?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-best-behavior_2_1">
<fieldset aria-describedby="status_04-best-behavior_2_1">
<div class="field">
<input type="radio" name="input_04-best-behavior_2_1" id="input_04-best-behavior_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-best-behavior_2_1-choice_0-label" for="input_04-best-behavior_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-best-behavior_2_1"> Yes
</label>
</div>
<div class="field">
<input type="radio" name="input_04-best-behavior_2_1" id="input_04-best-behavior_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-best-behavior_2_1-choice_1-label" for="input_04-best-behavior_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-best-behavior_2_1"> No
</label>
</div>
<span id="answer_04-best-behavior_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-best-behavior_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-best-behavior_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="best behavior" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-best-behavior" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-best-behavior">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-best-behavior-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-best-behavior-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-best-behavior-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-specification-structure" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Specification Structure</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_52058f9388f1"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:JjRrUgxgQMM", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 448.8, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V002600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V002600/MIT600512016-V002600.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_52058f9388f1"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_52058f9388f1">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_52058f9388f1">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_52058f9388f1/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_a612d6aa44a1">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_a612d6aa44a1" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<div class="panel panel-default panel-figure pull-right pull-margin no-markdown">
<div class="panel-body text-center">
<img src="/assets/courseware/v1/7520b5d0e98836955ced468162fcb606/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-firewall-implies.png" class="pull-right" width="258">
</div>
</div>
<h2 id="specification_structure">Specification structure</h2>
<div data-outline="specification_structure">
<pre><p>static int find(int[] arr, int val)
<em>requires</em>: val occurs exactly once in arr
<em>effects</em>: returns index i such that arr[i] = val</p></pre>
<p>A specification of a method consists of two clauses:</p>
<ul>
<li>a <em>precondition</em>, indicated by the keyword <em>requires</em></li>
<li>a <em>postcondition</em>, indicated by the keyword <em>effects</em></li>
</ul>
<p>The precondition is an obligation on the client (i.e., the caller of the method). It's a condition over the state in which the method is invoked.</p>
<p>The postcondition is an obligation on the implementer of the method. If the precondition holds for the invoking state, the method is obliged to obey the postcondition, by returning appropriate values, throwing specified exceptions, modifying or not modifying objects, and so on.</p>
<div class="clearfix"></div>
<div class="panel panel-default panel-figure pull-right pull-margin no-markdown">
<div class="panel-body text-center">
<img src="/assets/courseware/v1/5c0fb2f0bafd4ed1d67bca2b37c374f9/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-firewall-unsat.png" class="pull-right" width="258">
</div>
</div>
<p>The overall structure is a logical implication: <em>if</em> the precondition holds when the method is called, <em>then</em> the postcondition must hold when the method completes.</p>
<p>If the precondition does <em>not</em> hold when the method is called, the implementation is <em>not</em> bound by the postcondition. It is free to do anything, including not terminating, throwing an exception, returning arbitrary results, making arbitrary modifications, etc.</p>
</div>
<h3 id="specifications_in_java">Specifications in Java</h3>
<div data-outline="specifications_in_java">
<p>Some languages (notably <a href="http://en.wikipedia.org/wiki/Eiffel_(programming_language)">Eiffel</a>) incorporate preconditions and postconditions as a fundamental part of the language, as expressions that the runtime system (or even the compiler) can automatically check to enforce the contracts between clients and implementers.</p>
<p>Java does not go quite so far, but its static type declarations <em>are</em> effectively part of the precondition and postcondition of a method, a part that is automatically checked and enforced by the compiler. The rest of the contract — the parts that we can't write as types — must be described in a comment preceding the method, and generally depends on human beings to check it and guarantee it.</p>
<p>Java has a convention for <a href="http://en.wikipedia.org/wiki/Javadoc">documentation comments</a>, in which parameters are described by <code>@param</code> clauses and results are described by <code>@return</code> and <code>@throws</code> clauses. You should put the preconditions into <code>@param</code> where possible, and postconditions into <code>@return</code> and <code>@throws</code>. So a specification like this:</p><pre><p>static int find(int[] arr, int val)
<em>requires</em>: val occurs exactly once in arr
<em>effects</em>: returns index i such that arr[i] = val</p></pre>
<p>… might be rendered in Java like this:</p><pre><code class="language-java hljs"><span class="hljs-comment handout-javadoc-comment">/**
* Find a value in an array.
* <span class="hljs-doctag">@param</span> arr array to search, requires that val occurs exactly once
* in arr
* <span class="hljs-doctag">@param</span> val value to search for
* <span class="hljs-doctag">@return</span> index i such that arr[i] = val
*/</span>
<span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">find</span><span class="hljs-params">(<span class="hljs-keyword">int</span>[] arr, <span class="hljs-keyword">int</span> val)</span></span></code></pre>
<p>The <a href="http://docs.oracle.com/javase/8/docs/api/">Java API documentation</a> is produced from Javadoc comments in the <a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java">Java standard library source code</a>. Documenting your specifications in Javadoc allows Eclipse to show you (and clients of your code) useful information, and allows you to <a href="http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-export-javadoc.htm">produce HTML documentation</a> in the same format as the Java API docs.</p>
</div>
<h2 id="null_references">Null references</h2>
<div data-outline="null_references">
<p>In Java, references to objects and arrays can also take on the special value <em>null</em>, which means that the reference doesn't point to an object. Null values are an unfortunate hole in Java's type system.</p>
<p>Primitives cannot be null:</p><pre><code class="language-java hljs"><span class="hljs-keyword">int</span> size = <span class="hljs-keyword">null</span>; <span class="hljs-comment">// illegal</span>
<span class="hljs-keyword">double</span> depth = <span class="hljs-keyword">null</span>; <span class="hljs-comment">// illegal</span></code></pre>
<p>and the compiler will reject such attempts with static errors.</p>
<p>On the other hand, we can assign null to any non-primitive variable:</p><pre><code class="language-java hljs">String name = <span class="hljs-keyword">null</span>;
<span class="hljs-keyword">int</span>[] points = <span class="hljs-keyword">null</span>;</code></pre>
<p>and the compiler happily accepts this code at compile time. But you'll get errors at runtime because you can't call any methods or use any fields with one of these references:</p><pre><code class="language-java hljs">name.length() <span class="hljs-comment">// throws NullPointerException </span>
points.length <span class="hljs-comment">// throws NullPointerException</span></code></pre>
<p>Note, in particular, that <code>null</code> is not the same as an empty string <code>""</code> or an empty array. On an empty string or empty array, you <em>can</em> call methods and access fields. The length of an empty array or an empty string is 0. The length of a string variable that points to <code>null</code> isn't anything: calling <code>length()</code> throws a <code>NullPointerException</code>.</p>
<p>Also note that arrays of non-primitives and collections like <code>List</code> might be non-null but contain null as a value:</p>
<pre><code class="language-java hljs">String[] names = <span class="hljs-keyword">new</span> String[] { <span class="hljs-keyword">null</span> };
List<Double> sizes = <span class="hljs-keyword">new</span> ArrayList<>();
sizes.add(<span class="hljs-keyword">null</span>);</code></pre>
<p>These nulls are likely to cause errors as soon as someone tries to use the contents of the collection.</p>
<p>Null values are troublesome and unsafe, so much so that you're well advised to remove them from your design vocabulary. In 6.005 — and in fact in most good Java programming — <strong>null values are implicitly disallowed in parameters and return values</strong>. So every method implicitly has a precondition on its object and array parameters that they be non-null. Every method that returns an object or an array implicitly has a postcondition that its return value is non-null. If a method allows null values for a parameter, it should explicitly state it, or if it might return a null value as a result, it should explicitly state it. But these are in general not good ideas.
<strong>Avoid null</strong>.</p>
<p>There are extensions to Java that allow you to forbid <code>null</code> directly in the type declaration, e.g.:</p><pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">addAll</span><span class="hljs-params">(@NonNull List<T> list1, @NonNull List<T> list2)</span></span></code></pre>
<p>where it can be <a href="http://types.cs.washington.edu/checker-framework/">checked automatically</a> at compile time or runtime.</p>
<p>Google has their own <a href="https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained">discussion of <code>null</code> in Guava, the company's core Java libraries</a>. The project explains:</p>
<blockquote>
<p>Careless use of <code>null</code> can cause a staggering variety of bugs. Studying the Google code base, we found that something like 95% of collections weren't supposed to have any null values in them, and having those <strong>fail fast</strong> rather than silently accept <code>null</code> would have been helpful to developers.</p>
<p>Additionally, <code>null</code> is unpleasantly ambiguous. It's rarely obvious what a <code>null</code> return value is supposed to mean — for example, <code>Map.get(key)</code> can return <code>null</code> either because the value in the map is <code>null</code>, or the value is not in the map. Null can mean failure, can mean success, can mean almost anything. Using something other than <code>null</code> <strong>makes your meaning clear</strong>.</p>
</blockquote>
<p>(Emphasis added.)</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_5207f8fefe0b" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implication">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implication" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-logical-implication" class="problems-wrapper" role="group"
aria-labelledby="04-logical-implication-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implication" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implication/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-logical-implication-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implication-problem-progress" tabindex="-1">
logical implication
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implication-problem-progress"></div>
<div class="problem">
<div>
<p>Here&#8217;s the spec we&#8217;ve been looking at:</p>
<pre><p>static int find(int[] arr, int val)
<em>requires</em>: val occurs exactly once in arr
<em>effects</em>: returns index i such that arr[i] = val</p></pre>
<p>As the implementer of <code>find</code>, which are legal? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-logical-implication_2_1">
<fieldset aria-describedby="status_04-logical-implication_2_1">
<div class="field">
<input type="checkbox" name="input_04-logical-implication_2_1[]" id="input_04-logical-implication_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-logical-implication_2_1-choice_0-label" for="input_04-logical-implication_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-logical-implication_2_1"> if arr is empty, return 0
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-logical-implication_2_1[]" id="input_04-logical-implication_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-logical-implication_2_1-choice_1-label" for="input_04-logical-implication_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-logical-implication_2_1"> if arr is empty, throw an exception
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-logical-implication_2_1[]" id="input_04-logical-implication_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-logical-implication_2_1-choice_2-label" for="input_04-logical-implication_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-logical-implication_2_1"> if val occurs twice in arr, throw an exception
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-logical-implication_2_1[]" id="input_04-logical-implication_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="04-logical-implication_2_1-choice_3-label" for="input_04-logical-implication_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-logical-implication_2_1"> if val occurs twice in arr, set all the values in arr to zero, then throw an exception
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-logical-implication_2_1[]" id="input_04-logical-implication_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="04-logical-implication_2_1-choice_4-label" for="input_04-logical-implication_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_04-logical-implication_2_1"> if arr is not empty but val doesn&#8217;t occur, pick an index at random, set it to val, and return that index
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-logical-implication_2_1[]" id="input_04-logical-implication_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="04-logical-implication_2_1-choice_5-label" for="input_04-logical-implication_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_04-logical-implication_2_1"> if arr[0] is val, continue checking the rest of the array and return the highest index where you find val (or 0 if you don&#8217;t find it again)
</label>
</div>
<span id="answer_04-logical-implication_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-logical-implication_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-logical-implication_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="logical implication" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-logical-implication" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-logical-implication">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-logical-implication-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-logical-implication-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-logical-implication-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implementation">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implementation" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-logical-implementation" class="problems-wrapper" role="group"
aria-labelledby="04-logical-implementation-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implementation" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implementation/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-logical-implementation-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implementation-problem-progress" tabindex="-1">
logical implementation
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-logical-implementation-problem-progress"></div>
<div class="problem">
<div>
<p>As the implementor of <code>find</code>, why would you choose to throw an exception if <code>arr</code> is empty?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-logical-implementation_2_1">
<fieldset aria-describedby="status_04-logical-implementation_2_1">
<div class="field">
<input type="radio" name="input_04-logical-implementation_2_1" id="input_04-logical-implementation_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-logical-implementation_2_1-choice_0-label" for="input_04-logical-implementation_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-logical-implementation_2_1"> DRY
</label>
</div>
<div class="field">
<input type="radio" name="input_04-logical-implementation_2_1" id="input_04-logical-implementation_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-logical-implementation_2_1-choice_1-label" for="input_04-logical-implementation_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-logical-implementation_2_1"> fail fast
</label>
</div>
<div class="field">
<input type="radio" name="input_04-logical-implementation_2_1" id="input_04-logical-implementation_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="04-logical-implementation_2_1-choice_2-label" for="input_04-logical-implementation_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-logical-implementation_2_1"> avoid magic numbers
</label>
</div>
<div class="field">
<input type="radio" name="input_04-logical-implementation_2_1" id="input_04-logical-implementation_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="04-logical-implementation_2_1-choice_3-label" for="input_04-logical-implementation_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-logical-implementation_2_1"> one purpose per variable
</label>
</div>
<div class="field">
<input type="radio" name="input_04-logical-implementation_2_1" id="input_04-logical-implementation_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="04-logical-implementation_2_1-choice_4-label" for="input_04-logical-implementation_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_04-logical-implementation_2_1"> avoid global variables
</label>
</div>
<div class="field">
<input type="radio" name="input_04-logical-implementation_2_1" id="input_04-logical-implementation_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="04-logical-implementation_2_1-choice_5-label" for="input_04-logical-implementation_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_04-logical-implementation_2_1"> return results
</label>
</div>
<span id="answer_04-logical-implementation_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-logical-implementation_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-logical-implementation_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="logical implementation" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-logical-implementation" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-logical-implementation">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-logical-implementation-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-logical-implementation-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-logical-implementation-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-nullpointerexception-accessing-problemname">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-nullpointerexception-accessing-problemname" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-nullpointerexception-accessing-problemname" class="problems-wrapper" role="group"
aria-labelledby="04-nullpointerexception-accessing-problemname-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-nullpointerexception-accessing-problemname" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-nullpointerexception-accessing-problemname/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-nullpointerexception-accessing-problemname-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-nullpointerexception-accessing-problemname-problem-progress" tabindex="-1">
NullPointerException accessing problem.name()
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-nullpointerexception-accessing-problemname-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following can be null?</p>
<p>If you're not sure, try it yourself in a small Java program.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="inputtype_04-nullpointerexception-accessing-problemname_2_1" class="choicegroup capa_inputtype">
<fieldset aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1">
<legend id="04-nullpointerexception-accessing-problemname_2_1-legend" class="response-fieldset-legend field-group-hd">Check all that apply:</legend>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_0" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_0"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_0-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_0" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> int a;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_1" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_1"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_1-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_1" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> char b;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_2" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_2"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_2-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_2" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> double c;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_3" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_3"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_3-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_3" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> int[] d;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_4" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_4"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_4-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_4" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> String e;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_5" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_5"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_5-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_5" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> String[] f;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_6" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_6"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_6-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_6" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> Double g;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_7" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_7"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_7-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_7" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> List<integer> h;
</integer></label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_8" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_8"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_8-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_8" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> final MouseTrap i;
</label>
</div>
<div class="field">
<input id="input_04-nullpointerexception-accessing-problemname_2_1_choice_9" type="checkbox" name="input_04-nullpointerexception-accessing-problemname_2_1[]" class="field-input input-checkbox" value="choice_9"/><label id="04-nullpointerexception-accessing-problemname_2_1-choice_9-label" for="input_04-nullpointerexception-accessing-problemname_2_1_choice_9" aria-describedby="status_04-nullpointerexception-accessing-problemname_2_1" class="response-label field-label label-inline"> static final String j;
</label>
</div>
<span id="answer_04-nullpointerexception-accessing-problemname_2_1"/>
</fieldset>
<div class="indicator-container">
<span id="status_04-nullpointerexception-accessing-problemname_2_1" data-tooltip="Not yet answered." class="status unanswered">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-nullpointerexception-accessing-problemname_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="NullPointerException accessing problem.name()" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-nullpointerexception-accessing-problemname" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-nullpointerexception-accessing-problemname">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-nullpointerexception-accessing-problemname-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-nullpointerexception-accessing-problemname-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-nullpointerexception-accessing-problemname-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-4" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-there-are-null-exercises-remaining">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-there-are-null-exercises-remaining" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-there-are-null-exercises-remaining" class="problems-wrapper" role="group"
aria-labelledby="04-there-are-null-exercises-remaining-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-there-are-null-exercises-remaining" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-there-are-null-exercises-remaining/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-there-are-null-exercises-remaining-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-there-are-null-exercises-remaining-problem-progress" tabindex="-1">
there are null exercises remaining
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-there-are-null-exercises-remaining-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
public static String none() {
return null; // (1)
}
public static void main(String[] args) {
String a = none(); // (2)
String b = null; // (3)
if (a.length() &gt; 0) { // (4)
b = a; // (5)
}
return b; // (6)
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_04-there-are-null-exercises-remaining_2_1" id="label_04-there-are-null-exercises-remaining_2_1">Which line contains a static error?</label>
<select name="input_04-there-are-null-exercises-remaining_2_1" id="input_04-there-are-null-exercises-remaining_2_1" aria-describedby="status_04-there-are-null-exercises-remaining_2_1">
<option value="option_04-there-are-null-exercises-remaining_2_1_dummy_default">Select an option</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
<option value="5"> 5</option>
<option value="6"> 6</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_04-there-are-null-exercises-remaining_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_04-there-are-null-exercises-remaining_2_1"/>
</div></div>
<p>If we comment out that line and run main...</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_04-there-are-null-exercises-remaining_3_1" id="label_04-there-are-null-exercises-remaining_3_1">Which line contains a dynamic error?</label>
<select name="input_04-there-are-null-exercises-remaining_3_1" id="input_04-there-are-null-exercises-remaining_3_1" aria-describedby="status_04-there-are-null-exercises-remaining_3_1">
<option value="option_04-there-are-null-exercises-remaining_3_1_dummy_default">Select an option</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
<option value="5"> 5</option>
<option value="6"> 6</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_04-there-are-null-exercises-remaining_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_04-there-are-null-exercises-remaining_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_04-there-are-null-exercises-remaining_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="there are null exercises remaining" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-there-are-null-exercises-remaining" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-there-are-null-exercises-remaining">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-there-are-null-exercises-remaining-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-there-are-null-exercises-remaining-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-there-are-null-exercises-remaining-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-what-a-specification-may-talk-about" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">What a Specification May Talk About</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_618195348d4b"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:TJVWDpruoMs", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 516.04, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003000_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003000/MIT600512016-V003000.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_618195348d4b"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_618195348d4b">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_618195348d4b">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_618195348d4b/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_df8b7d6f7f91">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_df8b7d6f7f91" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="what_a_specification_may_talk_about">What a specification may talk about</h2>
<div class="panel panel-default panel-figure pull-right pull-margin no-markdown">
<div class="panel-body text-center">
<img src="/assets/courseware/v1/370691e1d60b170fd8ae9464d77e503d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-firewall-talk.png" class="pull-right" width="196">
</div>
</div>
<div data-outline="what_a_specification_may_talk_about">
<p>A specification of a method can talk about the parameters and return value of the method, but it should never talk about local variables of the method or private fields of the method's class. You should consider the implementation invisible to the reader of the spec.</p>
<p>In Java, the source code of the method is often unavailable to the reader of your spec, because the Javadoc tool extracts the spec comments from your code and renders them as HTML.</p>
<div class="clearfix"></div>
</div>
<h2 id="testing_and_specifications">Testing and specifications</h2>
<div data-outline="testing_and_specifications">
<p>In testing, we talk about <em>black box tests</em> that are chosen with only the specification in mind, and <em>glass box tests</em> that are chosen with knowledge of the actual implementation (<a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-blackbox-and-whitebox-testing#blackbox_and_whitebox_testing"><em>Testing</em></a>). But it's important to note that <strong>even glass box tests must follow the specification</strong>. Your implementation may provide stronger guarantees than the specification calls for, or it may have specific behavior where the specification is undefined. But your test cases should not count on that behavior. Test cases must obey the contract, just like every other client.</p>
<p>For example, suppose you are testing this specification of <code>find</code>, which is slightly different from the one we've used so far:</p><pre><p>static int find(int[] arr, int val)
<em>requires</em>: val occurs in arr
<em>effects</em>: returns index i such that arr[i] = val</p></pre>
<p>This spec has a strong precondition in the sense that <code>val</code> is required to be found; and it has a fairly weak postcondition in the sense that if <code>val</code> appears more than once in the array, this specification says nothing about which particular index of <code>val</code> is returned. Even if you implemented <code>find</code> so that it always returns the lowest index, your test case can't assume that specific behavior:</p><pre class="no-markdown"><code class="java hljs"><span class="hljs-keyword">int</span>[] array = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[] { <span class="hljs-number">7</span>, <span class="hljs-number">7</span>, <span class="hljs-number">7</span> };
<strike>assertEquals(<span class="hljs-number">0</span>, find(array, <span class="hljs-number">7</span>));</strike> <span class="hljs-comment">// bad test case: violates the spec</span>
assertEquals(<span class="hljs-number">7</span>, array[find(array, <span class="hljs-number">7</span>)]); <span class="hljs-comment">// correct</span>
</code></pre>
<p>Similarly, even if you implemented <code>find</code> so that it (sensibly) throws an exception when <code>val</code> isn't found, instead of returning some arbitrary misleading index, your test case can't assume that behavior, because it can't call <code>find()</code> in a way that violates the precondition.</p>
<p>So what does glass box testing mean, if it can't go beyond the spec? It means you are trying to find new test cases that exercise different parts of the implementation, but still checking those test cases in an implementation-independent way.</p>
<h3 id="testing_units">Testing units</h3>
<div data-outline="testing_units">
<p>Recall the <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-unit-testing-and-stubs#unit_testing_and_stubs">web search example from <em>Testing</em></a> with these methods:</p><pre><code class="language-java hljs"><span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@return</span> the contents of the web page downloaded from url */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title">getWebPage</span><span class="hljs-params">(URL url)</span> </span>{ ... }
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@return</span> the words in string s, in the order they appear,
* where a word is a contiguous sequence of
* non-whitespace and non-punctuation characters */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List<String> <span class="hljs-title">extractWords</span><span class="hljs-params">(String s)</span> </span>{ ... }
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@return</span> an index mapping a word to the set of URLs
* containing that word, for all webpages in the input set */</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Map<String, Set<URL>> makeIndex(Set<URL> urls) {
...
calls getWebPage and extractWords
...
} </code></pre>
<p>We talked then about <em>unit testing</em>, the idea that we should write tests of each module of our program in isolation. A good unit test is focused on just a single specification. Our tests will nearly always rely on the specs of Java library methods, but a unit test for one method we've written shouldn't fail if a <em>different</em> method fails to satisfy its spec. As we saw in the example, a test for <code>extractWords</code> shouldn't fail if <code>getWebPage</code> doesn't satisfy its postcondition.</p>
<p>Good <em>integration tests</em>, tests that use a combination of modules, will make sure that our different methods have compatible specifications: callers and implementors of different methods are passing and returning values as the other expects. Integration tests cannot replace systematically-designed unit tests. From the example, if we only ever test <code>extractWords</code> by calling <code>makeIndex</code>, we will only test it on a potentially small part of its input space: inputs that are possible outputs of <code>getWebPage</code>. In doing so, we've left a place for bugs to hide, ready to jump out when we use <code>extractWords</code> for a different purpose elsewhere in our program, or when <code>getWebPage</code> starts returning web pages written in a new format, etc.</p>
</div>
</div>
<h2 id="specifications_for_mutating_methods">Specifications for mutating methods</h2>
<div data-outline="specifications_for_mutating_methods">
<p>We previously discussed mutable vs. immutable objects, but our specifications of <code>find</code> didn't give us the opportunity to illustrate how to describe side-effects — changes to mutable data — in the postcondition.</p>
<p>Here's a specification that describes a method that mutates an object:</p><pre class="no-markdown">static boolean addAll(List<T> list1, List<T> list2)
<em>requires</em>: list1 != list2
<em>effects</em>: modifies list1 by adding the elements of list2 to the end of
it, and returns true if list1 changed as a result of call</pre>
<p>We've taken this, slightly simplified, from the Java <a href="http://docs.oracle.com/javase/8/docs/api/?java/util/List.html"><code>List</code></a> interface. First, look at the postcondition. It gives two constraints: the first telling us how <code>list1</code> is modified, and the second telling us how the return value is determined.</p>
<div class="panel panel-figure pull-right pull-margin">
<img src="/assets/courseware/v1/9632053abd8bd383b7d1f31730fd2e7d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-addall1.png" alt="snapshot diagram showing list1 and list2 pointing to the same list" width="200"><br>
<img src="/assets/courseware/v1/1bcd46a49ad9b68b0714232d5f133162/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-addall2.png" alt="snapshot diagram after the first element of list2 has been added to list1" width="200"><br>
<img src="/assets/courseware/v1/23feefdf232ff2aeea891551d3329f85/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/04-addall-infinitum.png" alt="snapshot diagram showing that appending will continue forever" width="200"><br>
</div>
<p>Second, look at the precondition. It tells us that the behavior of the method if you attempt to add the elements of a list to itself is undefined. You can easily imagine why the implementor of the method would want to impose this constraint: it's not likely to rule out any useful applications of the method, and it makes it easier to implement. The specification allows a simple implementation in which you take an element from <code>list2</code> and add it to <code>list1</code>, then go on to the next element of <code>list2</code> until you get to the end of <code>list2</code>. If <code>list1</code> and <code>list2</code> are the same list, then both lists will keep growing, and you will never get to the end. The sequence of snapshot diagrams at right illustrate this behavior. So this simple algorithm will not terminate — or practically speaking it will throw a memory error when the list object has grown so large that it consumes all available memory. Either outcome, infinite loop or crash, is permitted by the specification because of its precondition.</p>
<p>Remember also our implicit precondition that <code>list1</code> and <code>list2</code> must be valid objects, rather than <code>null</code>. We'll usually omit saying this because it's virtually always required of object references.</p>
<p>Here is another example of a mutating method:</p><pre class="no-markdown">static void sort(List<String> list)
<em>requires</em>: nothing
<em>effects</em>: puts list in sorted order, i.e. list[i] <= list[j]
for all 0 <= i < j < list.size()</pre>
<p>And an example of a method that does not mutate its argument:</p><pre class="no-markdown">static List<String> toLowerCase(List<String> list)
<em>requires</em>: nothing
<em>effects</em>: returns a new list t where t[i] = list[i].toLowerCase()</pre>
<p>Just as we've said that <code>null</code> is implicitly disallowed unless stated otherwise, we will also use the convention that <strong>mutation is disallowed unless stated otherwise</strong>. The spec of <code>toLowerCase</code> could explicitly state as an <em>effect</em> that “list is not modified”, but in the absence of a postcondition describing mutation, we demand no mutation of the inputs.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_0a5a4a50fdd3" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-in-here">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-in-here" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-get-in-here" class="problems-wrapper" role="group"
aria-labelledby="04-get-in-here-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-in-here" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-in-here/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-get-in-here-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-in-here-problem-progress" tabindex="-1">
get in here
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-in-here-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following are part of a function's specification? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-get-in-here_2_1">
<fieldset aria-describedby="status_04-get-in-here_2_1">
<div class="field">
<input type="checkbox" name="input_04-get-in-here_2_1[]" id="input_04-get-in-here_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-get-in-here_2_1-choice_0-label" for="input_04-get-in-here_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-get-in-here_2_1"> 1. return type
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-get-in-here_2_1[]" id="input_04-get-in-here_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-get-in-here_2_1-choice_1-label" for="input_04-get-in-here_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-get-in-here_2_1"> 2. restrictions on return values
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-get-in-here_2_1[]" id="input_04-get-in-here_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-get-in-here_2_1-choice_2-label" for="input_04-get-in-here_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-get-in-here_2_1"> 3. number of arguments
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-get-in-here_2_1[]" id="input_04-get-in-here_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="04-get-in-here_2_1-choice_3-label" for="input_04-get-in-here_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-get-in-here_2_1"> 4. argument types
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-get-in-here_2_1[]" id="input_04-get-in-here_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="04-get-in-here_2_1-choice_4-label" for="input_04-get-in-here_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_04-get-in-here_2_1"> 5. restrictions on argument values
</label>
</div>
<span id="answer_04-get-in-here_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-get-in-here_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-get-in-here_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="get in here" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-get-in-here" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-get-in-here">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-get-in-here-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-get-in-here-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-get-in-here-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-greatest-common-denominator">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-greatest-common-denominator" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-greatest-common-denominator" class="problems-wrapper" role="group"
aria-labelledby="04-greatest-common-denominator-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-greatest-common-denominator" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-greatest-common-denominator/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="4"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-greatest-common-denominator-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-greatest-common-denominator-problem-progress" tabindex="-1">
greatest common denominator
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-greatest-common-denominator-problem-progress"></div>
<div class="problem">
<div>
<p>Alice writes the following code:</p>
<pre class="code">
public static int gcd(int a, int b) {
if (a &gt; b) {
return gcd(a-b, b);
} else if (b &gt; a) {
return gcd(a, b-a);
}
return a;
}
</pre>
<p>Bob writes the following test:</p>
<pre class="code">
@Test public void gcdTest() {
assertEquals(6, gcd(24, 54));
}
</pre>
<p>The test passes!</p>
<p>Alice should write <code>a &gt; 0</code> in the precondition of <code>gcd</code></p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-greatest-common-denominator_2_1">
<fieldset aria-describedby="status_04-greatest-common-denominator_2_1">
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_2_1" id="input_04-greatest-common-denominator_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-greatest-common-denominator_2_1-choice_0-label" for="input_04-greatest-common-denominator_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_2_1"> True
</label>
</div>
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_2_1" id="input_04-greatest-common-denominator_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-greatest-common-denominator_2_1-choice_1-label" for="input_04-greatest-common-denominator_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_2_1"> False
</label>
</div>
<span id="answer_04-greatest-common-denominator_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-greatest-common-denominator_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>Alice should write <code>b &gt; 0</code> in the precondition of <code>gcd</code></p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-greatest-common-denominator_3_1">
<fieldset aria-describedby="status_04-greatest-common-denominator_3_1">
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_3_1" id="input_04-greatest-common-denominator_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-greatest-common-denominator_3_1-choice_0-label" for="input_04-greatest-common-denominator_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_3_1"> True
</label>
</div>
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_3_1" id="input_04-greatest-common-denominator_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-greatest-common-denominator_3_1-choice_1-label" for="input_04-greatest-common-denominator_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_3_1"> False
</label>
</div>
<span id="answer_04-greatest-common-denominator_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-greatest-common-denominator_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p> Alice should write <code>gcd(a, b) &gt; 0</code> in the precondition of <code>gcd</code></p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-greatest-common-denominator_4_1">
<fieldset aria-describedby="status_04-greatest-common-denominator_4_1">
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_4_1" id="input_04-greatest-common-denominator_4_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-greatest-common-denominator_4_1-choice_0-label" for="input_04-greatest-common-denominator_4_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_4_1"> True
</label>
</div>
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_4_1" id="input_04-greatest-common-denominator_4_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-greatest-common-denominator_4_1-choice_1-label" for="input_04-greatest-common-denominator_4_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_4_1"> False
</label>
</div>
<span id="answer_04-greatest-common-denominator_4_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-greatest-common-denominator_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p> Alice should write <code>a and b are integers</code> in the precondition of <code>gcd</code></p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-greatest-common-denominator_5_1">
<fieldset aria-describedby="status_04-greatest-common-denominator_5_1">
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_5_1" id="input_04-greatest-common-denominator_5_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-greatest-common-denominator_5_1-choice_0-label" for="input_04-greatest-common-denominator_5_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_5_1"> True
</label>
</div>
<div class="field">
<input type="radio" name="input_04-greatest-common-denominator_5_1" id="input_04-greatest-common-denominator_5_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-greatest-common-denominator_5_1-choice_1-label" for="input_04-greatest-common-denominator_5_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-greatest-common-denominator_5_1"> False
</label>
</div>
<span id="answer_04-greatest-common-denominator_5_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-greatest-common-denominator_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-greatest-common-denominator_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="greatest common denominator" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-greatest-common-denominator" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-greatest-common-denominator">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-greatest-common-denominator-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-greatest-common-denominator-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-greatest-common-denominator-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-3" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-gcd-contd">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-gcd-contd" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-gcd-contd" class="problems-wrapper" role="group"
aria-labelledby="04-gcd-contd-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-gcd-contd" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-gcd-contd/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-gcd-contd-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-gcd-contd-problem-progress" tabindex="-1">
gcd, cont&#39;d
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-gcd-contd-problem-progress"></div>
<div class="problem">
<div>
<p>If Alice adds <code>a &gt; 0</code> to the precondition, Bob should test negative values of <code>a</code></p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-gcd-contd_2_1">
<fieldset aria-describedby="status_04-gcd-contd_2_1">
<div class="field">
<input type="radio" name="input_04-gcd-contd_2_1" id="input_04-gcd-contd_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-gcd-contd_2_1-choice_0-label" for="input_04-gcd-contd_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-gcd-contd_2_1"> True
</label>
</div>
<div class="field">
<input type="radio" name="input_04-gcd-contd_2_1" id="input_04-gcd-contd_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-gcd-contd_2_1-choice_1-label" for="input_04-gcd-contd_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-gcd-contd_2_1"> False
</label>
</div>
<span id="answer_04-gcd-contd_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-gcd-contd_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p> If Alice does not add <code>a &gt; 0</code> to the precondition, Bob should test negative values of <code>a</code></p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-gcd-contd_3_1">
<fieldset aria-describedby="status_04-gcd-contd_3_1">
<div class="field">
<input type="radio" name="input_04-gcd-contd_3_1" id="input_04-gcd-contd_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-gcd-contd_3_1-choice_0-label" for="input_04-gcd-contd_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-gcd-contd_3_1"> True
</label>
</div>
<div class="field">
<input type="radio" name="input_04-gcd-contd_3_1" id="input_04-gcd-contd_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-gcd-contd_3_1-choice_1-label" for="input_04-gcd-contd_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-gcd-contd_3_1"> False
</label>
</div>
<span id="answer_04-gcd-contd_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-gcd-contd_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-gcd-contd_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="gcd, cont&#39;d" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-gcd-contd" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-gcd-contd">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-gcd-contd-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-gcd-contd-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-gcd-contd-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Exceptions" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Exceptions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_d432ccdaa27a"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:SqHx1iSFeAo", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 644.96, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003500_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003500/MIT600512016-V003500.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_d432ccdaa27a"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_d432ccdaa27a">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_d432ccdaa27a">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_d432ccdaa27a/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_e1f62858a40f">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_e1f62858a40f" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<p>Now that we're writing specifications and thinking about how clients will use our methods, let's discuss how to handle <em>exceptional</em> cases in a way that is safe from bugs and easy to understand.</p>
<p>A method's <em>signature</em> — its name, parameter types, return type — is a core part of its specification, and the signature may also include <em>exceptions</em> that the method may trigger.</p>
<h2 id="exceptions_for_signaling_bugs">Exceptions for signaling bugs</h2>
<div data-outline="exceptions_for_signaling_bugs">
<p>You've probably already seen some exceptions in your Java programming so far, such as <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/ArrayIndexOutOfBoundsException.html"><code>ArrayIndexOutOfBoundsException</code></a> (thrown when an array index <code>foo[i]</code> is outside the valid range for the array <code>foo</code>) or <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/NullPointerException.html"><code>NullPointerException</code></a> (thrown when trying to call a method on a <code>null</code> object reference). These exceptions generally indicate <strong>bugs</strong> in your code, and the information displayed by Java when the exception is thrown can help you find and fix the bug.</p>
<p><code>ArrayIndexOutOfBounds-</code> and <code>NullPointerException</code> are probably the most common exceptions of this sort. Other examples include:</p>
<ul>
<li><a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/ArithmeticException.html"><code>ArithmeticException</code></a>, thrown for arithmetic errors like integer division by zero.</li>
<li><a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/NumberFormatException.html"><code>NumberFormatException</code></a>, thrown by methods like <code>Integer.parseInt</code> if you pass in a string that cannot be parsed into an integer.</li>
</ul>
</div>
<h2 id="exceptions_for_special_results">Exceptions for special results</h2>
<div data-outline="exceptions_for_special_results">
<p>Exceptions are not just for signaling bugs. They can be used to improve the structure of code that involves procedures with special results.</p>
<p>An unfortunately common way to handle special results is to return special values. Lookup operations in the Java library are often designed like this: you get an index of -1 when expecting a positive integer, or a <code>null</code> reference when expecting an object. This approach is OK if used sparingly, but it has two problems. First, it's tedious to check the return value. Second, it's easy to forget to do it. (We'll see that by using exceptions you can get help from the compiler in this.)</p>
<p>Also, it's not always easy to find a ‘special value'. Suppose we have a <code>BirthdayBook</code> class with a lookup method. Here's one possible method signature:</p><pre><code class="language-java hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BirthdayBook</span> </span>{
<span class="hljs-function">LocalDate <span class="hljs-title">lookup</span><span class="hljs-params">(String name)</span> </span>{ ... }
}</code></pre>
<p>(<a href="http://docs.oracle.com/javase/8/docs/api/?java/time/LocalDate.html"><code>LocalDate</code></a> is part of the Java API.)</p>
<p>What should the method do if the birthday book doesn't have an entry for the person whose name is given? Well, we could return some special date that is not going to be used as a real date. Bad programmers have been doing this for decades; they would return 9/9/99, for example, since it was <em>obvious</em> that no program written in 1960 would still be running at the end of the century. (
<a href="http://en.wikipedia.org/wiki/Year_2000_problem">They were wrong, by the way.</a>)</p>
<p>Here's a better approach. The method throws an exception:</p><pre><code class="language-java hljs"> <span class="hljs-function">LocalDate <span class="hljs-title">lookup</span><span class="hljs-params">(String name)</span> <span class="hljs-keyword">throws</span> NotFoundException </span>{
...
<span class="hljs-keyword">if</span> ( ...not found... )
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> NotFoundException();
...</code></pre>
<p>and the caller handles the exception with a <code>catch</code> clause. For example:</p><pre><code class="language-java hljs"> BirthdayBook birthdays = ...
<span class="hljs-keyword">try</span> {
LocalDate birthdate = birthdays.lookup(<span class="hljs-string">"Alyssa"</span>);
<span class="hljs-comment">// we know Alyssa's birthday</span>
} <span class="hljs-keyword">catch</span> (NotFoundException nfe) {
<span class="hljs-comment">// her birthday was not in the birthday book</span>
}</code></pre>
<p>Now there's no need for any special value, nor the checking associated with it.</p>
<!--
<div class="handout-solo alert alert-warning">
<p>Read: <strong><a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html" class="alert-link">Exceptions</a></strong> in the Java Tutorials.</p>
</div>
-->
</div>
<h2 id="checked_and_unchecked_exceptions">Checked and unchecked exceptions</h2>
<div data-outline="checked_and_unchecked_exceptions">
<p>We've seen two different purposes for exceptions: special results and bug detection. As a general rule, you'll want to use checked exceptions to signal special results and unchecked exceptions to signal bugs. In the next section of this reading, we'll refine this a bit.</p>
<p>Some terminology: <em>checked</em> exceptions are called that because they are checked by the compiler:</p>
<ul>
<li>If a method might throw a checked exception, the possibility must be declared in its signature.
<code>NotFoundException</code> would be a checked exception, and that's why the signature ends with <code>throws NotFoundException</code>.</li>
<li>If a method calls another method that may throw a checked exception, it must either handle it, or declare the exception itself, since if it isn't caught locally it will be propagated up to callers.</li>
</ul>
<p>So if you call <code>BirthdayBook</code>'s <code>lookup</code> method and forget to handle the <code>NotFoundException</code>, the compiler will reject your code. This is very useful, because it ensures that exceptions that are expected to occur will be handled.</p>
<p><em>Unchecked</em> exceptions, in contrast, are used to signal bugs. These exceptions are not expected to be handled by the code except perhaps at the top level. We wouldn't want every method up the call chain to have to declare that it (might) throw all the kinds of bug-related exceptions that can happen at lower call levels: index out of bounds, null pointers, illegal arguments, assertion failures, etc.</p>
<p>As a result, for an unchecked exception the compiler will not check for <code>try</code>-<code>catch</code> or a <code>throws</code> declaration. Java still allows you to write a <code>throws</code> clause for an unchecked exception as part of a method signature, but this has no effect, and is thus a bit funny, and we don't recommend doing it.</p>
<p>All exceptions may have a message associated with them. If not provided in the constructor, the reference to the message string is <code>null</code>.</p>
</div>
<h2 id="throwable_hierarchy">Throwable hierarchy</h2>
<div data-outline="throwable_hierarchy">
<p>To understand how Java decides whether an exception is checked or unchecked, let's look at the class hierarchy for Java exceptions.</p>
<div class="panel panel-figure pull-right pull-margin">
<div class="panel-body"><img src="/assets/courseware/v1/99719a82935844fa9908a1b8ac3e1d22/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/03-throwable.png" width="300"></div>
</div>
<p><a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/Throwable.html"><code>Throwable</code></a> is the class of objects that can be thrown or caught.
<code>Throwable</code>'s implementation records a stack trace at the point where the exception was thrown, along with an optional string describing the exception. Any object used in a <code>throw</code> or <code>catch</code> statement, or declared in the <code>throws</code> clause of a method, must be a subclass of <code>Throwable</code>.</p>
<p><a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/Error.html"><code>Error</code></a> is a subclass of <code>Throwable</code> that is reserved for errors produced by the Java runtime system, such as <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/StackOverflowError.html"><code>StackOverflowError</code></a> and <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/OutOfMemoryError.html"><code>OutOfMemoryError</code></a>. For some reason <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/AssertionError.html"><code>AssertionError</code></a> also extends <code>Error</code>, even though it indicates a bug in user code, not in the runtime. Errors should be considered unrecoverable, and are generally not caught.</p>
<p>Here's how Java distinguishes between checked and unchecked exceptions:</p>
<ul>
<li><code>RuntimeException</code>, <code>Error</code>, and their subclasses are <strong>unchecked</strong> exceptions. The compiler doesn't require them to be declared in the <code>throws</code> clause of a method that throws them, and doesn't require them to be caught or declared by a caller of such a method.</li>
<li>All other throwables — <code>Throwable</code>, <code>Exception</code>, and all of their subclasses except for those of the <code>RuntimeException</code> and <code>Error</code> lineage — are <strong>checked</strong> exceptions. The compiler requires these exceptions to be caught or declared when it's possible for them to be thrown.</li>
</ul>
<p>When you define your own exceptions, you should either subclass <code>RuntimeException</code> (to make it an unchecked exception) or <code>Exception</code> (to make it checked). Programmers generally don't subclass <code>Error</code> or <code>Throwable</code>, because these are reserved by Java itself.</p>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_627a4e40f626" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-to-the-point">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-to-the-point" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-get-to-the-point" class="problems-wrapper" role="group"
aria-labelledby="04-get-to-the-point-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-to-the-point" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-to-the-point/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-get-to-the-point-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-to-the-point-problem-progress" tabindex="-1">
get to the point
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-get-to-the-point-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose we're building a robot and we want to specify the function</p>
<pre class="code">
public static List&lt;Point&gt; findPath(Point initial, Point goal)
</pre>
<p>which is responsible for path-finding: determining a sequence of <code>Point</code>s that the robot should move through to navigate from <code>initial</code> to <code>goal</code>, past any obstacles that might be in the way.</p>
<p>In the postcondition, we say that <code>findPath</code> will search for paths only up to a bounded length (set elsewhere), and that it will throw an exception if it fails to find one.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-get-to-the-point_2_1">
<fieldset aria-describedby="status_04-get-to-the-point_2_1">
<legend id="04-get-to-the-point_2_1-legend" class="response-fieldset-legend field-group-hd">Which of these would we choose?</legend>
<div class="field">
<input type="radio" name="input_04-get-to-the-point_2_1" id="input_04-get-to-the-point_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-get-to-the-point_2_1-choice_0-label" for="input_04-get-to-the-point_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-get-to-the-point_2_1"> throws a checked <code>NoPathException</code>
</label>
</div>
<div class="field">
<input type="radio" name="input_04-get-to-the-point_2_1" id="input_04-get-to-the-point_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-get-to-the-point_2_1-choice_1-label" for="input_04-get-to-the-point_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-get-to-the-point_2_1"> throws an unchecked <code>NoPathException</code>
</label>
</div>
<div class="field">
<input type="radio" name="input_04-get-to-the-point_2_1" id="input_04-get-to-the-point_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="04-get-to-the-point_2_1-choice_2-label" for="input_04-get-to-the-point_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-get-to-the-point_2_1"> throws a checked <code>PathNotFoundException</code>
</label>
</div>
<div class="field">
<input type="radio" name="input_04-get-to-the-point_2_1" id="input_04-get-to-the-point_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="04-get-to-the-point_2_1-choice_3-label" for="input_04-get-to-the-point_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-get-to-the-point_2_1"> throws an unchecked <code>PathNotFoundException</code>
</label>
</div>
<span id="answer_04-get-to-the-point_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-get-to-the-point_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-get-to-the-point_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="get to the point" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-get-to-the-point" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-get-to-the-point">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-get-to-the-point-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-get-to-the-point-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-get-to-the-point-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-dont-point-that-thing-at-me">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-dont-point-that-thing-at-me" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-dont-point-that-thing-at-me" class="problems-wrapper" role="group"
aria-labelledby="04-dont-point-that-thing-at-me-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-dont-point-that-thing-at-me" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-dont-point-that-thing-at-me/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-dont-point-that-thing-at-me-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-dont-point-that-thing-at-me-problem-progress" tabindex="-1">
don&#39;t point that thing at me
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-dont-point-that-thing-at-me-problem-progress"></div>
<div class="problem">
<div>
<p>When we define our exception for <code>findPath</code>, which will we choose as our superclass?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-dont-point-that-thing-at-me_2_1">
<fieldset aria-describedby="status_04-dont-point-that-thing-at-me_2_1">
<div class="field">
<input type="radio" name="input_04-dont-point-that-thing-at-me_2_1" id="input_04-dont-point-that-thing-at-me_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-dont-point-that-thing-at-me_2_1-choice_0-label" for="input_04-dont-point-that-thing-at-me_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-dont-point-that-thing-at-me_2_1"> Throwable
</label>
</div>
<div class="field">
<input type="radio" name="input_04-dont-point-that-thing-at-me_2_1" id="input_04-dont-point-that-thing-at-me_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-dont-point-that-thing-at-me_2_1-choice_1-label" for="input_04-dont-point-that-thing-at-me_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-dont-point-that-thing-at-me_2_1"> Exception
</label>
</div>
<div class="field">
<input type="radio" name="input_04-dont-point-that-thing-at-me_2_1" id="input_04-dont-point-that-thing-at-me_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="04-dont-point-that-thing-at-me_2_1-choice_2-label" for="input_04-dont-point-that-thing-at-me_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-dont-point-that-thing-at-me_2_1"> Error
</label>
</div>
<div class="field">
<input type="radio" name="input_04-dont-point-that-thing-at-me_2_1" id="input_04-dont-point-that-thing-at-me_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="04-dont-point-that-thing-at-me_2_1-choice_3-label" for="input_04-dont-point-that-thing-at-me_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-dont-point-that-thing-at-me_2_1"> RuntimeException
</label>
</div>
<span id="answer_04-dont-point-that-thing-at-me_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-dont-point-that-thing-at-me_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-dont-point-that-thing-at-me_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="don&#39;t point that thing at me" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-dont-point-that-thing-at-me" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-dont-point-that-thing-at-me">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-dont-point-that-thing-at-me-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-dont-point-that-thing-at-me-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-dont-point-that-thing-at-me-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Exception_Design_Considerations" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Exception Design Considerations</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_61f0cb812cad"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:okFg1rNBfLM", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 351.76, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V002700_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V002700/MIT600512016-V002700.m3u8"]}'
data-bumper-metadata='null'
data-autoadvance-enabled="False"
data-poster='null'
tabindex="-1"
>
<div class="focus_grabber first"></div>
<div class="tc-wrapper">
<div class="video-wrapper">
<span tabindex="0" class="spinner" aria-hidden="false" aria-label="Loading video player"></span>
<span tabindex="-1" class="btn-play fa fa-youtube-play fa-2x is-hidden" aria-hidden="true" aria-label="Play video"></span>
<div class="video-player-pre"></div>
<div class="video-player">
<div id="video_61f0cb812cad"></div>
<h4 class="hd hd-4 video-error is-hidden">No playable video sources found.</h4>
<h4 class="hd hd-4 video-hls-error is-hidden">
Your browser does not support this video format. Try using a different browser.
</h4>
</div>
<div class="video-player-post"></div>
<div class="closed-captions"></div>
<div class="video-controls is-hidden">
<div>
<div class="vcr"><div class="vidtime">0:00 / 0:00</div></div>
<div class="secondary-controls"></div>
</div>
</div>
</div>
</div>
<div class="focus_grabber last"></div>
<h3 class="hd hd-4 downloads-heading sr" id="video-download-transcripts_video_61f0cb812cad">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_61f0cb812cad">
<div class="wrapper-download-transcripts">
<h4 class="hd hd-5">Transcripts</h4>
<ul class="list-download-transcripts">
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad/handler/transcript/download" data-value="srt">Download SubRip (.srt) file</a>
</li>
<li class="transcript-option">
<a class="btn btn-link" href="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_61f0cb812cad/handler/transcript/download" data-value="txt">Download Text (.txt) file</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_6bb5b19d6f8d">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_6bb5b19d6f8d" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="exception_design_considerations">Exception design considerations</h2>
<div data-outline="exception_design_considerations">
<p>The rule we have given — use checked exceptions for special results (i.e., anticipated situations), and unchecked exceptions to signal bugs (unexpected failures) — makes sense, but it isn't the end of the story. The snag is that exceptions in Java aren't as lightweight as they might be.</p>
<p>Aside from the performance penalty, exceptions in Java incur another (more serious) cost: they're a pain to use, in both method design and method use. If you <em>design</em> a method to have its own (new) exception, you have to create a new class for the exception. If you <em>call</em> a method that can throw a checked exception, you have to wrap it in a <code>try</code>-<code>catch</code> statement (even if you know the exception will never be thrown). This latter stipulation creates a dilemma. Suppose, for example, you're designing a queue abstraction. Should popping the queue throw a checked exception when the queue is empty? Suppose you want to support a style of programming in the client in which the queue is popped (in a loop say) until the exception is thrown. So you choose a checked exception. Now some client wants to use the method in a context in which, immediately prior to popping, the client tests whether the queue is empty and only pops if it isn't. Maddeningly, that client will still need to wrap the call in a <code>try</code>-<code>catch</code> statement.</p>
<p>This suggests a more refined rule:</p>
<ul>
<li>You should use an unchecked exception only to signal an unexpected failure (i.e. a bug), or if you expect that clients will usually write code that ensures the exception will not happen, because there is a convenient and inexpensive way to avoid the exception;</li>
<li>Otherwise you should use a checked exception.</li>
</ul>
<p>Here are some examples of applying this rule to hypothetical methods:</p>
<ul>
<li><code>Queue.pop()</code> throws an <em>unchecked</em> <code>EmptyQueueException</code> when the queue is empty, because it's reasonable to expect the caller to avoid this with a call like <code>Queue.size()</code> or <code>Queue.isEmpty()</code>.</li>
<li><code>Url.getWebPage()</code> throws a <em>checked</em> <code>IOException</code> when it can't retrieve the web page, because it's not easy for the caller to prevent this.</li>
<li><code>int integerSquareRoot(int x)</code> throws a <em>checked</em> <code>NotPerfectSquareException</code> when <code>x</code> has no integral square root, because testing whether <code>x</code> is a perfect square is just as hard as finding the actual square root, so it's not reasonable to expect the caller to prevent it.</li>
</ul>
<p>The cost of using exceptions in Java is one reason that many Java API's use the null reference as a special value. It's not a terrible thing to do, so long as it's done judiciously, and carefully specified.</p>
</div>
<h2 id="abuse_of_exceptions">Abuse of exceptions</h2>
<div data-outline="abuse_of_exceptions">
<p>Here's an example from <a href="https://books.google.com/books?id=ka2VUBqHiWkC&hl=en"><em>Effective Java</em> by Joshua Bloch</a> (Item 57 in the 2nd edition). </p><pre><code class="language-java hljs"><span class="hljs-keyword">try</span> {
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>)
a[i++].f();
} <span class="hljs-keyword">catch</span> (ArrayIndexOutOfBoundsException e) { }</code></pre>
<blockquote>
<p>What does this code do? It is not at all obvious from inspection, and that's reason enough not to use it. … The infinite loop terminates by throwing, catching, and ignoring an <code>ArrayIndexOutOfBoundsException</code> when it attempts to access the first array element outside the bounds of the array.</p>
</blockquote>
<p>It is supposed to be equivalent to:</p><pre><code class="language-java hljs"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < a.length; i++) {
a[i].f();
}</code></pre>
<p>Or (using appropriate type <code>T</code>) to:</p><pre><code class="language-java hljs"><span class="hljs-keyword">for</span> (T x : a) {
x.f();
}</code></pre>
<p>The exception-based idiom, Bloch writes:</p>
<blockquote>
<p>… is a misguided attempt to improve performance based on the faulty reasoning that, since the VM checks the bounds of array accesses, the normal loop termination test (<code>i < a.length</code>) is redundant and should be avoided.</p>
</blockquote>
<p>However, because exceptions in Java are designed for use only under exceptional circumstances, few, if any, JVM implementations attempt to optimize their performance. On a typical machine, the exception-based idiom runs 70 times slower than the standard one when looping from 0 to 99.</p>
<p>Much worse than that, the exception-based idiom is not even guaranteed to work! Suppose the computation of <code>f()</code> in the body of the loop contains a bug that results in an out-of-bounds access to some unrelated array. What happens?</p>
<blockquote>
<p>If a reasonable loop idiom were used, the bug would generate an uncaught exception, resulting in immediate thread termination with a full stack trace. If the misguided exception-based loop were used, the bug-related exception would be caught and misinterpreted as a normal loop termination.</p>
</blockquote>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_61c786cf9897" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-throw-all-the-things">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-throw-all-the-things" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-throw-all-the-things" class="problems-wrapper" role="group"
aria-labelledby="04-throw-all-the-things-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-throw-all-the-things" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-throw-all-the-things/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-throw-all-the-things-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-throw-all-the-things-problem-progress" tabindex="-1">
throw all the things!
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-throw-all-the-things-problem-progress"></div>
<div class="problem">
<div>
<p>Examine this code for analyzing some <code>Thing</code> objects:</p>
<pre class="code">
static void analyzeEverything() {
analyzeThingsInOrder();
}
static void analyzeThingsInOrder() {
try {
for (Thing t : ALL_THE_THINGS) {
analyzeOneThing(t);
}
} catch (AnalysisException e) {
return;
}
}
static void analyzeOneThing(Thing t) throws AnalysisException {
// ...
// ... maybe go off the end of an array
// ...
}
</pre>
<p><code>AnalysisException</code> is an unchecked exception.</p>
<p>Which exceptions could be thrown by a call to <code>analyzeEverything</code>? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-throw-all-the-things_2_1">
<fieldset aria-describedby="status_04-throw-all-the-things_2_1">
<div class="field">
<input type="checkbox" name="input_04-throw-all-the-things_2_1[]" id="input_04-throw-all-the-things_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-throw-all-the-things_2_1-choice_0-label" for="input_04-throw-all-the-things_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-throw-all-the-things_2_1"> ArrayIndexOutOfBoundsException
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-throw-all-the-things_2_1[]" id="input_04-throw-all-the-things_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-throw-all-the-things_2_1-choice_1-label" for="input_04-throw-all-the-things_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-throw-all-the-things_2_1"> IOException
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-throw-all-the-things_2_1[]" id="input_04-throw-all-the-things_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-throw-all-the-things_2_1-choice_2-label" for="input_04-throw-all-the-things_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-throw-all-the-things_2_1"> NullPointerException
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-throw-all-the-things_2_1[]" id="input_04-throw-all-the-things_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="04-throw-all-the-things_2_1-choice_3-label" for="input_04-throw-all-the-things_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-throw-all-the-things_2_1"> AnalysisException
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-throw-all-the-things_2_1[]" id="input_04-throw-all-the-things_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="04-throw-all-the-things_2_1-choice_4-label" for="input_04-throw-all-the-things_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_04-throw-all-the-things_2_1"> OutOfMemoryError
</label>
</div>
<span id="answer_04-throw-all-the-things_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-throw-all-the-things_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-throw-all-the-things_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="throw all the things!" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-throw-all-the-things" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-throw-all-the-things">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-throw-all-the-things-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-throw-all-the-things-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-throw-all-the-things-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-a-terrible-thing">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-a-terrible-thing" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-a-terrible-thing" class="problems-wrapper" role="group"
aria-labelledby="04-a-terrible-thing-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-a-terrible-thing" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-a-terrible-thing/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="1"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-a-terrible-thing-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-a-terrible-thing-problem-progress" tabindex="-1">
a terrible thing
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-a-terrible-thing-problem-progress"></div>
<div class="problem">
<div>
<p>What might happen if <code>analyzeOneThing</code> throws an <code>AnalysisException</code> in this code? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-a-terrible-thing_2_1">
<fieldset aria-describedby="status_04-a-terrible-thing_2_1">
<div class="field">
<input type="checkbox" name="input_04-a-terrible-thing_2_1[]" id="input_04-a-terrible-thing_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-a-terrible-thing_2_1-choice_0-label" for="input_04-a-terrible-thing_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-a-terrible-thing_2_1"> The program might crash
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-a-terrible-thing_2_1[]" id="input_04-a-terrible-thing_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-a-terrible-thing_2_1-choice_1-label" for="input_04-a-terrible-thing_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-a-terrible-thing_2_1"> We might fail to call analyzeOneThing on all of the Things in ALL_THE_THINGS
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-a-terrible-thing_2_1[]" id="input_04-a-terrible-thing_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-a-terrible-thing_2_1-choice_2-label" for="input_04-a-terrible-thing_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-a-terrible-thing_2_1"> We might call analyzeOneThing multiple times on some Thing(s)
</label>
</div>
<span id="answer_04-a-terrible-thing_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-a-terrible-thing_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-a-terrible-thing_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="a terrible thing" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-a-terrible-thing" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-a-terrible-thing">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-a-terrible-thing-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-a-terrible-thing-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-a-terrible-thing-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_5ff482ce7b85" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-words-with-specs">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-words-with-specs" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-words-with-specs" class="problems-wrapper" role="group"
aria-labelledby="04-words-with-specs-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-words-with-specs" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-words-with-specs/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-words-with-specs-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-words-with-specs-problem-progress" tabindex="-1">
words with specs
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-words-with-specs-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose we're working on the method below:</p>
<pre class="code">
/**
* Requires: tiles has length 7 &amp; contains only uppercase letters.
* crossings contains only uppercase letters, without duplicates.
* Effects: Returns a list of words where each word can be made by taking
* letters from tiles and at most 1 letter from crossings.
*/
public static List&lt;String&gt; scrabble(String tiles, String crossings) {
if (tiles.length() != 7) { throw new RuntimeException(); }
return new ArrayList&lt;&gt;();
}
</pre>
<p>Which are parts of the <em>postcondition</em> of <code>scrabble</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-words-with-specs_2_1">
<fieldset aria-describedby="status_04-words-with-specs_2_1">
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_2_1[]" id="input_04-words-with-specs_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-words-with-specs_2_1-choice_0-label" for="input_04-words-with-specs_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_2_1"> <code>tiles</code> has only uppercase letters
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_2_1[]" id="input_04-words-with-specs_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-words-with-specs_2_1-choice_1-label" for="input_04-words-with-specs_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_2_1"> <code>crossings</code> has no duplicates
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_2_1[]" id="input_04-words-with-specs_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-words-with-specs_2_1-choice_2-label" for="input_04-words-with-specs_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_2_1"> <code>scrabble</code> takes two arguments
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_2_1[]" id="input_04-words-with-specs_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="04-words-with-specs_2_1-choice_3-label" for="input_04-words-with-specs_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_2_1"> <code>scrabble</code> returns a list of strings
</label>
</div>
<span id="answer_04-words-with-specs_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-words-with-specs_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-words-with-specs_solution_1"/>
</div><p>Which are parts of the <em>precondition</em> of <code>scrabble</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-words-with-specs_3_1">
<fieldset aria-describedby="status_04-words-with-specs_3_1">
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_3_1[]" id="input_04-words-with-specs_3_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-words-with-specs_3_1-choice_0-label" for="input_04-words-with-specs_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_3_1"> <code>tiles</code> has length 7
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_3_1[]" id="input_04-words-with-specs_3_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-words-with-specs_3_1-choice_1-label" for="input_04-words-with-specs_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_3_1"> <code>crossings</code> is a string of uppercase letters
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_3_1[]" id="input_04-words-with-specs_3_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-words-with-specs_3_1-choice_2-label" for="input_04-words-with-specs_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_3_1"> <code>scrabble</code>'s arguments are of type <code>String</code> and <code>String</code>
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-words-with-specs_3_1[]" id="input_04-words-with-specs_3_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="04-words-with-specs_3_1-choice_3-label" for="input_04-words-with-specs_3_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-words-with-specs_3_1"> <code>scrabble</code> returns an empty <code>ArrayList</code>
</label>
</div>
<span id="answer_04-words-with-specs_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-words-with-specs_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-words-with-specs_solution_2"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="words with specs" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-words-with-specs" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-words-with-specs">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-words-with-specs-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-words-with-specs-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-words-with-specs-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
<div class="vert vert-2" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-scrabble-scramble">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-scrabble-scramble" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_04-scrabble-scramble" class="problems-wrapper" role="group"
aria-labelledby="04-scrabble-scramble-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-scrabble-scramble" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-scrabble-scramble/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="2"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="04-scrabble-scramble-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-scrabble-scramble-problem-progress" tabindex="-1">
Scrabble scramble
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@04-scrabble-scramble-problem-progress"></div>
<div class="problem">
<div>
<p>Which are <em>parts of the spec that are checked statically</em> by Java?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-scrabble-scramble_2_1">
<fieldset aria-describedby="status_04-scrabble-scramble_2_1">
<div class="field">
<input type="checkbox" name="input_04-scrabble-scramble_2_1[]" id="input_04-scrabble-scramble_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="04-scrabble-scramble_2_1-choice_0-label" for="input_04-scrabble-scramble_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_2_1"> <code>tiles</code> is a string of uppercase letters
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-scrabble-scramble_2_1[]" id="input_04-scrabble-scramble_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="04-scrabble-scramble_2_1-choice_1-label" for="input_04-scrabble-scramble_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_2_1"> <code>crossings</code> has no duplicates
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-scrabble-scramble_2_1[]" id="input_04-scrabble-scramble_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="04-scrabble-scramble_2_1-choice_2-label" for="input_04-scrabble-scramble_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_2_1"> when <code>tiles.length() != 7</code>, <code>scrabble</code> throws a <code>RuntimeException</code>
</label>
</div>
<div class="field">
<input type="checkbox" name="input_04-scrabble-scramble_2_1[]" id="input_04-scrabble-scramble_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="04-scrabble-scramble_2_1-choice_3-label" for="input_04-scrabble-scramble_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_2_1"> <code>scrabble</code> takes two arguments
</label>
</div>
<span id="answer_04-scrabble-scramble_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-scrabble-scramble_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>Does the <code>scrabble</code> implementation satisfy its specification?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_04-scrabble-scramble_3_1">
<fieldset aria-describedby="status_04-scrabble-scramble_3_1">
<div class="field">
<input type="radio" name="input_04-scrabble-scramble_3_1" id="input_04-scrabble-scramble_3_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="04-scrabble-scramble_3_1-choice_0-label" for="input_04-scrabble-scramble_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_3_1"> Yes
</label>
</div>
<div class="field">
<input type="radio" name="input_04-scrabble-scramble_3_1" id="input_04-scrabble-scramble_3_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="04-scrabble-scramble_3_1-choice_1-label" for="input_04-scrabble-scramble_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_3_1"> No, because it throws a <code>RuntimeException</code> when <code>tiles</code> has invalid length
</label>
</div>
<div class="field">
<input type="radio" name="input_04-scrabble-scramble_3_1" id="input_04-scrabble-scramble_3_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="04-scrabble-scramble_3_1-choice_2-label" for="input_04-scrabble-scramble_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_04-scrabble-scramble_3_1"> No, because it returns an empty result list even if we call it with tiles and crossings that can be combined to form valid words
</label>
</div>
<span id="answer_04-scrabble-scramble_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_04-scrabble-scramble_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_04-scrabble-scramble_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="Scrabble scramble" />
<div class="submit-attempt-container">
<button type="button" class="submit btn-brand" data-submitting="Submitting" data-value="Submit" data-should-enable-submit-button="True" aria-describedby="submission_feedback_04-scrabble-scramble" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_04-scrabble-scramble">
<span class="sr">Some problems have options such as save, reset, hints, or show answer. These options follow the Submit button.</span>
</div>
</div>
<div class="problem-action-buttons-wrapper">
</div>
</div>
<div class="notification warning notification-gentle-alert
is-hidden"
tabindex="-1">
<span class="icon fa fa-exclamation-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-scrabble-scramble-problem-title">
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification warning notification-save
is-hidden"
tabindex="-1">
<span class="icon fa fa-save" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-scrabble-scramble-problem-title">None
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
<div class="notification general notification-show-answer
is-hidden"
tabindex="-1">
<span class="icon fa fa-info-circle" aria-hidden="true"></span>
<span class="notification-message" aria-describedby="04-scrabble-scramble-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_4_Summary" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Reading 4 Summary</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_9e171e313dba">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_9e171e313dba" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="86a891424c3811ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2>Summary</h2>
<p>A specification acts as a crucial firewall between the implementor of a procedure and its client. It makes separate development possible: the client is free to write code that uses the procedure without seeing its source code, and the implementor is free to write the code that implements the procedure without knowing how it will be used.</p>
<p>Let's review how specifications help with the main goals of this course:</p>
<ul>
<li>
<p><strong>Safe from bugs</strong>. A good specification clearly documents the mutual assumptions that a client and implementor are relying on. Bugs often come from disagreements at the interfaces and the presence of a specification reduces that. Using machine-checked language features in your spec, like static typing and exceptions rather than just a human-readable comment, can further reduce bugs.</p>
</li>
<li>
<p><strong>Easy to understand</strong>. A short, simple spec is easier to understand than the implementation itself, and saves other people from having to read the code.</p>
</li>
<li>
<p><strong>Ready for change</strong>. Specs establish contracts between different parts of your code, allowing those parts to change independently as long as they continue to satisfy the requirements of the contract.</p>
</li>
</ul>
<div class="license">This reading was collaboratively authored with contributions from: Saman Amarasinghe, Adam Chlipala, Srini Devadas, Michael Ernst, Max Goldman, John Guttag, Daniel Jackson, Rob Miller, Martin Rinard, and Armando Solar-Lezama. This work is licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.</div>
</div>
</div>
</div>
</div>