<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@2728f7e2e05d4a84b5e3a557735f724f">
<h2 class="hd hd-2 unit-title">This Course on Open Learning Library</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@6182d09a8f90411493c7216df60e084e">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@6182d09a8f90411493c7216df60e084e">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<p style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; vertical-align: baseline; color: #313131;">In this version of <em style="text-rendering: optimizelegibility; margin: 0px; padding: 0px; border: 0px; outline: 0px; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">Software Construction in JavaPractices</em> on Open Learning Library:</p>
<ul style="text-rendering: optimizelegibility; border: 0px; outline: 0px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; line-height: 1.4em; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; vertical-align: baseline; color: #313131;">
<li style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">No certificates can be earned on Open Learning Library</li>
<li style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">Runs as ‘self-paced’ and all dates mentioned within are irrelevant</li>
<li style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">Some assessment material may have been removed and grading adjusted accordingly</li>
<li style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">Any discussion forums have been removed</li>
<li style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">All assessments have been set to unlimited attempts</li>
<li style="text-rendering: optimizelegibility; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; border: 0px; outline: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: 1.4em; font-family: inherit; vertical-align: baseline;">Some course content may have been removed </li>
</ul>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_1_Objectives">
<h2 class="hd hd-2 unit-title">Reading 1 Objectives</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_a59de57c7336">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_a59de57c7336">
<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_a59de57c7336"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_a59de57c7336/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_a59de57c7336/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_a59de57c7336/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_a59de57c7336/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:Ol6Iz9AbAHY", "prioritizeHls": false, "duration": 71.3, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V003600/MIT600512016-V003600.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_a59de57c7336"></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_a59de57c7336">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_a59de57c7336">
<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_a59de57c7336/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_a59de57c7336/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_3bc68a9e3edc">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_3bc68a9e3edc">
<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>Objectives for Today</h4>
<p>Today's class has two topics:</p>
<ul>
<li>static typing</li>
<li>the big three properties of good software</li>
</ul>
<p>In this first reading, we’re going to talk about an important feature of Java that makes it different from other programming languages you may have learned, like Python and Javascript. That feature is called static typing.</p>
<p>From there, we’ll introduce the three big goals of this course, because this course isn’t really a Java programming course. We want to learn how to write good code in any language, and the three big ideas are about what we mean by good code. It should be:
<ol>
<li>safe from bugs, or in other words correct;</li>
<li>easy to understand for other programmers; and</li>
<li>ready to be changed in the future.</li>
</ol>
<p>Every idea in the course will help with these goals.</p>
<p>Let me also say something about the readings for this course. Each section of a reading has a text part, plus a video part in which I basically read through the text. The text and the video both have the same information. So you can just forget about the video and read the text if you prefer to do that, or you can listen to the video while scrolling through the text, or you can just watch the video and ignore the text at first. But if you need to refer back to the section later, you may find the text part easier to skim and search and review.</p>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Hailstone_Sequence">
<h2 class="hd hd-2 unit-title">Hailstone Sequence</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_407d6fcee224">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_407d6fcee224">
<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_407d6fcee224"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_407d6fcee224/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_407d6fcee224/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_407d6fcee224/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_407d6fcee224/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:yDy7HtuPh2E", "prioritizeHls": false, "duration": 282.87, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000100_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000100/MIT600512016-V000100.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_407d6fcee224"></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_407d6fcee224">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_407d6fcee224">
<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_407d6fcee224/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_407d6fcee224/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_f109ca9c5146">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_f109ca9c5146">
<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="hailstone_sequence">Hailstone Sequence</h2>
<div data-outline="hailstone_sequence">
<p>As a running example, we're going to explore the hailstone sequence, which is defined as follows. Starting with a number n, the next number in the sequence is n/2 if n is even, or 3n+1 if n is odd. The sequence ends when it reaches 1. Here are some examples:</p><pre><code class="hljs cpp"><span class="hljs-number">2</span>, <span class="hljs-number">1</span>
<span class="hljs-number">3</span>, <span class="hljs-number">10</span>, <span class="hljs-number">5</span>, <span class="hljs-number">16</span>, <span class="hljs-number">8</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>
<span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>
<span class="hljs-number">2</span><sup>n</sup>, <span class="hljs-number">2</span><sup>n-<span class="hljs-number">1</span></sup> , ... , <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>
<span class="hljs-number">5</span>, <span class="hljs-number">16</span>, <span class="hljs-number">8</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>
<span class="hljs-number">7</span>, <span class="hljs-number">22</span>, <span class="hljs-number">11</span>, <span class="hljs-number">34</span>, <span class="hljs-number">17</span>, <span class="hljs-number">52</span>, <span class="hljs-number">26</span>, <span class="hljs-number">13</span>, <span class="hljs-number">40</span>, ...? (where does this stop?)</code></pre>
<p>Because of the odd-number rule, the sequence may bounce up and down before decreasing to 1. It's conjectured that all hailstones eventually fall to the ground — i.e., the hailstone sequence reaches 1 for all starting n — but that's still an <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">open question</a>. Why is it called a hailstone sequence? Because hailstones form in clouds by bouncing up and down, until they eventually build enough weight to fall to earth. </p>
</div>
<h2 id="computing_hailstones">Computing Hailstones</h2>
<div data-outline="computing_hailstones">
<p>Here's some code for computing and printing the hailstone sequence for some starting n. We'll write Java and Python side by side for comparison:</p>
<table>
<tbody>
<tr>
<td style="vertical-align: top; padding-right: 10px"><pre><code class="language-java hljs"><span class="hljs-comment">// Java</span>
<span class="hljs-keyword">int</span> n = <span class="hljs-number">3</span>;
<span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) {
System.out.println(n);
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
} <span class="hljs-keyword">else</span> {
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>;
}
}
System.out.println(n);</code></pre></td>
<td style="vertical-align: top"><pre><code class="language-python hljs"><span class="hljs-comment"># Python</span>
n = <span class="hljs-number">3</span>
<span class="hljs-keyword">while</span> n != <span class="hljs-number">1</span>:
<span class="hljs-keyword">print</span> n
<span class="hljs-keyword">if</span> n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>:
n = n / <span class="hljs-number">2</span>
<span class="hljs-keyword">else</span>:
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>
<span class="hljs-keyword">print</span> n</code></pre></td>
</tr>
</tbody>
</table>
<p>A few things are worth noting here:</p>
<ul>
<li>The basic semantics of expressions and statements in Java are very similar to Python: while and if behave the same, for example. </li>
<li>Java requires semicolons at the ends of statements. The extra punctuation can be a pain, but it also gives you more freedom in how you organize your code — you can split a statement into multiple lines for more readability.</li>
<li>Java requires parentheses around the conditions of the if and while. There's no good reason for this, and personally I find it annoying.</li>
<li>Java requires curly braces around blocks, instead of indentation.
<br> You should always indent the block, even though Java won't pay any attention to your extra spaces. Programming is a form of communication, and you're communicating not only to the compiler, but also to human beings. Humans need that indentation. We'll come back to this later. </li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Questions">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-python-refresher-1">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-1">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-python-refresher-1" class="problems-wrapper" role="group"
aria-labelledby="01-python-refresher-1-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-1" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-1/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="01-python-refresher-1-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-1-problem-progress" tabindex="-1">
reading the hailstone code
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-1-problem-progress"></div>
<div class="problem">
<div>
<pre>
n = 4
while n != 1:
if n % 2 == 0: # if n is even...
n = n / 2
else:
n = 3 * n + 1
print n
</pre>
<p>What is the first number that this code will print?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-python-refresher-1_2_1">
<fieldset aria-describedby="status_01-python-refresher-1_2_1">
<div class="field">
<input type="radio" name="input_01-python-refresher-1_2_1" id="input_01-python-refresher-1_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-python-refresher-1_2_1-choice_0-label" for="input_01-python-refresher-1_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-1_2_1"> 1
</label>
</div>
<div class="field">
<input type="radio" name="input_01-python-refresher-1_2_1" id="input_01-python-refresher-1_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-python-refresher-1_2_1-choice_1-label" for="input_01-python-refresher-1_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-1_2_1"> 2
</label>
</div>
<div class="field">
<input type="radio" name="input_01-python-refresher-1_2_1" id="input_01-python-refresher-1_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-python-refresher-1_2_1-choice_2-label" for="input_01-python-refresher-1_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-1_2_1"> 6
</label>
</div>
<div class="field">
<input type="radio" name="input_01-python-refresher-1_2_1" id="input_01-python-refresher-1_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="01-python-refresher-1_2_1-choice_3-label" for="input_01-python-refresher-1_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-1_2_1"> 7
</label>
</div>
<span id="answer_01-python-refresher-1_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-python-refresher-1_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_01-python-refresher-1_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="reading the hailstone code" />
<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_01-python-refresher-1" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-python-refresher-1">
<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="01-python-refresher-1-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="01-python-refresher-1-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="01-python-refresher-1-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@01-python-refresher-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-python-refresher-2" class="problems-wrapper" role="group"
aria-labelledby="01-python-refresher-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-2/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="01-python-refresher-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-2-problem-progress" tabindex="-1">
programming terminology
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-python-refresher-2-problem-progress"></div>
<div class="problem">
<div>
<p>In the program, what kind of a thing is <code>n % 2 == 0</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-python-refresher-2_2_1">
<fieldset aria-describedby="status_01-python-refresher-2_2_1">
<div class="field">
<input type="radio" name="input_01-python-refresher-2_2_1" id="input_01-python-refresher-2_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-python-refresher-2_2_1-choice_0-label" for="input_01-python-refresher-2_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-2_2_1"> variable
</label>
</div>
<div class="field">
<input type="radio" name="input_01-python-refresher-2_2_1" id="input_01-python-refresher-2_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-python-refresher-2_2_1-choice_1-label" for="input_01-python-refresher-2_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-2_2_1"> operator
</label>
</div>
<div class="field">
<input type="radio" name="input_01-python-refresher-2_2_1" id="input_01-python-refresher-2_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-python-refresher-2_2_1-choice_2-label" for="input_01-python-refresher-2_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-2_2_1"> statement
</label>
</div>
<div class="field">
<input type="radio" name="input_01-python-refresher-2_2_1" id="input_01-python-refresher-2_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="01-python-refresher-2_2_1-choice_3-label" for="input_01-python-refresher-2_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-python-refresher-2_2_1"> expression
</label>
</div>
<span id="answer_01-python-refresher-2_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-python-refresher-2_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_01-python-refresher-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="programming terminology" />
<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_01-python-refresher-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-python-refresher-2">
<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="01-python-refresher-2-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="01-python-refresher-2-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="01-python-refresher-2-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-types">
<h2 class="hd hd-2 unit-title">Types</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_efd3f2dc7f87">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_efd3f2dc7f87">
<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_efd3f2dc7f87"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_efd3f2dc7f87/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_efd3f2dc7f87/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_efd3f2dc7f87/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_efd3f2dc7f87/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:expvDeP30gg", "prioritizeHls": false, "duration": 351.74, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000300_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000300/MIT600512016-V000300.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_efd3f2dc7f87"></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_efd3f2dc7f87">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_efd3f2dc7f87">
<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_efd3f2dc7f87/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_efd3f2dc7f87/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_07a081b97aed">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_07a081b97aed">
<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="types">Types</h2>
<div data-outline="types">
<p>The most important semantic difference between the Python and Java code above is the declaration of the variable <code>n</code>, which specifies its type: <code>int</code>.</p>
<table>
<tbody>
<tr>
<td style="vertical-align: top; padding-right: 10px"><pre><code class="language-java hljs"><span class="hljs-comment">// Java</span>
<span class="hljs-keyword">int</span> n = <span class="hljs-number">3</span>;</code></pre></td>
<td style="vertical-align: top"><pre><code class="language-python hljs"><span class="hljs-comment"># Python</span>
n = <span class="hljs-number">3</span></code></pre></td>
</tr>
</tbody>
</table>
<p>A <strong>type</strong> is a set of values, along with operations that can be performed on those values. </p>
<p>Java has several <strong>primitive types</strong>, among them:</p>
<ul>
<li><code>int</code> (for integers like 5 and -200, but limited to the range ± 2^31, or roughly ± 2 billion)</li>
<li><code>long</code> (for larger integers up to ± 2^63)</li>
<li><code>boolean</code> (for true or false)</li>
<li><code>double</code> (for floating-point numbers, which represent a subset of the real numbers)</li>
<li><code>char</code> (for single characters like <code>'A'</code> and <code>'$'</code>)</li>
</ul>
<p>Java also has <strong>object types</strong>, for example: </p>
<ul>
<li><code>String</code> represents a sequence of characters, like a Python string. </li>
<li><code>BigInteger</code> represents an integer of arbitrary size, so it acts like a Python integer.</li>
</ul>
<p>By Java convention, primitive types are lowercase, while object types start with a capital letter.</p>
<p><em>Operations</em> are functions that take input values and produce output values. (An operation can sometimes change the input values too, which is called <em>mutation</em> -- we'll talk about it later in this reading.) The syntax for operations varies, but we still think of them as functions no matter how they're written. Here are three different syntaxes for an operation in Python or Java:</p>
<ul>
<li><em>As an infix, prefix, or postfix operator.</em> For example, <code>a + b</code> invokes the operation <code>+ : int × int → int</code>.</li>
<li><em>As a method of an object.</em> For example, <code>bigint1.add(bigint2)</code> calls the operation <code>add: BigInteger × BigInteger → BigInteger</code>.</li>
<li><em>As a function.</em> For example, <code>Math.sin(theta)</code> calls the operation <code>sin: double → double</code>. Here, <code>Math</code> is not an object. It's the class that contains the <code>sin</code> function.</li>
</ul>
<p>Contrast Java's <code>str.length()</code> with Python's <code>len(str)</code>. It's the same operation in both languages — a function that takes a string and returns its length — but it just uses different syntax.</p>
<p>Some operations are <strong>overloaded</strong> in the sense that the same operation name is used for different types. The arithmetic operators +, -, *, / are heavily overloaded for the numeric primitive types in Java. Methods can also be overloaded. Most programming languages have some degree of overloading.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_32f5df732e99">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-cumulative-average-1">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-1">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-cumulative-average-1" class="problems-wrapper" role="group"
aria-labelledby="01-cumulative-average-1-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-1" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-1/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="01-cumulative-average-1-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-1-problem-progress" tabindex="-1">
how types affect execution
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-1-problem-progress"></div>
<div class="problem">
<div>
<pre>
# assume Python 2
data = [ 2, 4, 6 ]
total = 0
average = 0
n = 0
for value in data:
n += 1
total += value
average = total / n
print "average:", average
</pre>
<p>What averages are printed?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-cumulative-average-1_2_1">
<fieldset aria-describedby="status_01-cumulative-average-1_2_1">
<div class="field">
<input type="radio" name="input_01-cumulative-average-1_2_1" id="input_01-cumulative-average-1_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-cumulative-average-1_2_1-choice_0-label" for="input_01-cumulative-average-1_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-1_2_1"> 1, 1, 1
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-1_2_1" id="input_01-cumulative-average-1_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-cumulative-average-1_2_1-choice_1-label" for="input_01-cumulative-average-1_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-1_2_1"> 1, 2, 3
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-1_2_1" id="input_01-cumulative-average-1_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-cumulative-average-1_2_1-choice_2-label" for="input_01-cumulative-average-1_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-1_2_1"> 2, 3, 4
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-1_2_1" id="input_01-cumulative-average-1_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="01-cumulative-average-1_2_1-choice_3-label" for="input_01-cumulative-average-1_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-1_2_1"> 2, 4, 6
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-1_2_1" id="input_01-cumulative-average-1_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="01-cumulative-average-1_2_1-choice_4-label" for="input_01-cumulative-average-1_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-1_2_1"> error before the program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-1_2_1" id="input_01-cumulative-average-1_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="01-cumulative-average-1_2_1-choice_5-label" for="input_01-cumulative-average-1_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-1_2_1"> error while the program is running
</label>
</div>
<span id="answer_01-cumulative-average-1_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-cumulative-average-1_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_01-cumulative-average-1_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="how types affect execution" />
<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_01-cumulative-average-1" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-cumulative-average-1">
<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="01-cumulative-average-1-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="01-cumulative-average-1-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="01-cumulative-average-1-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@01-cumulative-average-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-cumulative-average-2" class="problems-wrapper" role="group"
aria-labelledby="01-cumulative-average-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-2/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="01-cumulative-average-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-2-problem-progress" tabindex="-1">
how types affect execution, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-2-problem-progress"></div>
<div class="problem">
<div>
Here is the same program, with different starting values for the data list.
<pre>
# assume Python 2
data = [ 1, 2, 3 ]
total = 0
average = 0
n = 0
for value in data:
n += 1
total += value
average = total / n
print "average:", average
</pre>
<p>Now what averages are printed?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-cumulative-average-2_2_1">
<fieldset aria-describedby="status_01-cumulative-average-2_2_1">
<div class="field">
<input type="radio" name="input_01-cumulative-average-2_2_1" id="input_01-cumulative-average-2_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-cumulative-average-2_2_1-choice_0-label" for="input_01-cumulative-average-2_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-2_2_1"> 1, 1, 1
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-2_2_1" id="input_01-cumulative-average-2_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-cumulative-average-2_2_1-choice_1-label" for="input_01-cumulative-average-2_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-2_2_1"> 1, 1, 2
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-2_2_1" id="input_01-cumulative-average-2_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-cumulative-average-2_2_1-choice_2-label" for="input_01-cumulative-average-2_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-2_2_1"> 1, 1.5, 2
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-2_2_1" id="input_01-cumulative-average-2_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="01-cumulative-average-2_2_1-choice_3-label" for="input_01-cumulative-average-2_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-2_2_1"> 1, 2, 2
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-2_2_1" id="input_01-cumulative-average-2_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="01-cumulative-average-2_2_1-choice_4-label" for="input_01-cumulative-average-2_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-2_2_1"> error before the program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-2_2_1" id="input_01-cumulative-average-2_2_1_choice_5" class="field-input input-radio" value="choice_5"/><label id="01-cumulative-average-2_2_1-choice_5-label" for="input_01-cumulative-average-2_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-2_2_1"> error while the program is running
</label>
</div>
<span id="answer_01-cumulative-average-2_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-cumulative-average-2_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_01-cumulative-average-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="how types affect execution, part 2" />
<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_01-cumulative-average-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-cumulative-average-2">
<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="01-cumulative-average-2-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="01-cumulative-average-2-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="01-cumulative-average-2-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@01-cumulative-average-3">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-3">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-cumulative-average-3" class="problems-wrapper" role="group"
aria-labelledby="01-cumulative-average-3-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-3" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-3/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="01-cumulative-average-3-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-3-problem-progress" tabindex="-1">
how types affect execution, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-cumulative-average-3-problem-progress"></div>
<div class="problem">
<div>
Here is the same program one more time, again with different starting values for the data list.
<pre>
# assume Python 2
data = [ "1", "2", "3" ]
total = 0
average = 0
n = 0
for value in data:
n += 1
total += value
average = total / n
print "average:", average
</pre>
<p>Now what averages are printed?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-cumulative-average-3_2_1">
<fieldset aria-describedby="status_01-cumulative-average-3_2_1">
<div class="field">
<input type="radio" name="input_01-cumulative-average-3_2_1" id="input_01-cumulative-average-3_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-cumulative-average-3_2_1-choice_0-label" for="input_01-cumulative-average-3_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-3_2_1"> 1, 1.5, 2
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-3_2_1" id="input_01-cumulative-average-3_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-cumulative-average-3_2_1-choice_1-label" for="input_01-cumulative-average-3_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-3_2_1"> "1", "1.5", "2"
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-3_2_1" id="input_01-cumulative-average-3_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-cumulative-average-3_2_1-choice_2-label" for="input_01-cumulative-average-3_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-3_2_1"> 1, 6, 41
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-3_2_1" id="input_01-cumulative-average-3_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="01-cumulative-average-3_2_1-choice_3-label" for="input_01-cumulative-average-3_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-3_2_1"> error before the program starts
</label>
</div>
<div class="field">
<input type="radio" name="input_01-cumulative-average-3_2_1" id="input_01-cumulative-average-3_2_1_choice_4" class="field-input input-radio" value="choice_4"/><label id="01-cumulative-average-3_2_1-choice_4-label" for="input_01-cumulative-average-3_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_01-cumulative-average-3_2_1"> error while the program is running
</label>
</div>
<span id="answer_01-cumulative-average-3_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-cumulative-average-3_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_01-cumulative-average-3_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="how types affect execution, part 3" />
<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_01-cumulative-average-3" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-cumulative-average-3">
<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="01-cumulative-average-3-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="01-cumulative-average-3-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="01-cumulative-average-3-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-static-typing">
<h2 class="hd hd-2 unit-title">Static Typing</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bcc7e8991b83">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bcc7e8991b83">
<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_bcc7e8991b83"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bcc7e8991b83/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bcc7e8991b83/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bcc7e8991b83/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_bcc7e8991b83/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:Wpw_cGlxqLQ", "prioritizeHls": false, "duration": 641.27, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000800_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000800/MIT600512016-V000800.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_bcc7e8991b83"></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_bcc7e8991b83">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_bcc7e8991b83">
<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_bcc7e8991b83/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_bcc7e8991b83/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_aff9bbf61251">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_aff9bbf61251">
<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="static_typing">Static Typing</h2>
<div data-outline="static_typing">
<p>So a key difference between the Python and Java code is that the variable <code>n</code> has a <em>type declaration</em> saying that it's an <code>int</code>.</p>
<table>
<tbody>
<tr>
<td style="vertical-align: top; padding-right: 10px"><pre><code class="language-java hljs"><span class="hljs-comment">// Java</span>
<span class="hljs-keyword">int</span> n = <span class="hljs-number">3</span>;</code></pre></td>
<td style="vertical-align: top"><pre><code class="language-python hljs"><span class="hljs-comment"># Python</span>
n = <span class="hljs-number">3</span></code></pre></td>
</tr>
</tbody>
</table>
<p>Java is a <strong>statically-typed language</strong>. The types of all variables have to be known at compile time (before the program runs), and the compiler can therefore deduce the types of all expressions as well. If <code>a</code> and <code>b</code> are declared as <code>int</code>s, then the compiler concludes that <code>a+b</code> is also an <code>int</code>. The Eclipse environment does this while you're writing the code, in fact, so you find out about many errors while you're still typing.</p>
<p>In <strong>dynamically-typed languages</strong> like Python, this kind of checking is deferred until runtime (while the program is running).</p>
<p>Static typing is a particular kind of <strong>static checking</strong>, which means checking for bugs at compile time. Bugs are the bane of programming. Many of the ideas in this course are aimed at eliminating bugs from your code, and static checking is the first idea that we've seen for this. Static typing prevents a large class of bugs from infecting your program: to be precise, bugs caused by applying an operation to the wrong types of arguments. If you write a broken line of code like:</p><pre><code class="hljs bash"> <span class="hljs-string">"5"</span> * <span class="hljs-string">"6"</span></code></pre>
<p>that tries to multiply two strings, then static typing will catch this error while you're still programming, rather than waiting until the line is reached during execution.</p>
</div>
<h2 id="static_checking_dynamic_checking_no_checking">Static Checking, Dynamic Checking, No Checking</h2>
<div data-outline="static_checking_dynamic_checking_no_checking">
<p>It's useful to think about three kinds of automatic checking that a language can provide:</p>
<ul>
<li><strong>Static checking</strong>: the bug is found automatically before the program even runs.</li>
<li><strong>Dynamic checking</strong>: the bug is found automatically when the code is executed.</li>
<li><strong>No checking</strong>: the language doesn't help you find the error at all. You have to watch for it yourself, or end up with wrong answers.</li>
</ul>
<p>Needless to say, catching a bug statically is better than catching it dynamically, and catching it dynamically is better than not catching it at all.</p>
<p>Here are some rules of thumb for what errors you can expect to be caught at each of these times.</p>
<p><strong>Static checking</strong> can catch:</p>
<ul>
<li>syntax errors, like extra punctuation or spurious words. Even dynamically-typed languages like Python do this kind of static checking. If you have an indentation error in your Python program, you'll find out before the program starts running.</li>
<li>wrong names, like <code>Math.sine(2)</code>. (The right name is <code>sin</code>.)</li>
<li>wrong number of arguments, like <code>Math.sin(30, 20)</code>.</li>
<li>wrong argument types, like <code>Math.sin("30")</code>.</li>
<li>wrong return types, like <code>return "30";</code> from a function that's declared to return an <code>int</code>.</li>
</ul>
<p><strong>Dynamic checking</strong> can catch:</p>
<ul>
<li>illegal argument values. For example, the integer expression <code>x/y</code> is only erroneous when <code>y</code> is actually zero; otherwise it works. So in this expression, divide-by-zero is not a static error, but a dynamic error.</li>
<li>unrepresentable return values, i.e., when the specific return value can't be represented in the type.</li>
<li>out-of-range indexes, e.g., using a negative or too-large index on a string.</li>
<li>calling a method on a <code>null</code> object reference (<code>null</code> is like Python <code>None</code>).</li>
</ul>
<p>Static checking tends to be about types, errors that are independent of the specific value that a variable has. A type is a set of values. Static typing guarantees that a variable will have <em>some</em> value from that set, but we don't know until runtime exactly which value it has. So if the error would be caused only by certain values, like divide-by-zero or index-out-of-range then the compiler won't raise a static error about it.</p>
<p>Dynamic checking, by contrast, tends to be about errors caused by specific values.</p>
</div>
<h2 id="surprise_primitive_types_are_not_true_numbers">Surprise: Primitive Types Are Not True Numbers</h2>
<div data-outline="surprise_primitive_types_are_not_true_numbers">
<p>One trap in Java — and many other programming languages — is that its primitive numeric types have corner cases that do not behave like the integers and real numbers we're used to. As a result, some errors that really should be dynamically checked are not checked at all. Here are the traps:</p>
<ul>
<li>
<p><strong>Integer division</strong>. <code>5/2</code> does not return a fraction, it returns a truncated integer. So this is an example of where what we might have hoped would be a dynamic error (because a fraction isn't representable as an integer) frequently produces the wrong answer instead.</p>
</li>
<li>
<p><strong>Integer overflow</strong>. The <code>int</code> and <code>long</code> types are actually finite sets of integers, with maximum and minimum values. What happens when you do a computation whose answer is too positive or too negative to fit in that finite range? The computation quietly <em>overflows</em> (wraps around), and returns an integer from somewhere in the legal range but not the right answer.</p>
</li>
<li>
<p><strong>Special values in floating-point types</strong>. Floating-point types like <code>double</code> types have several special values that aren't real numbers: <code>NaN</code> (which stands for “Not a Number”), <code>POSITIVE_INFINITY</code>, and <code>NEGATIVE_INFINITY</code>. So when you apply certain operations to a <code>double</code> that you'd expect to produce dynamic errors, like dividing by zero or taking the square root of a negative number, you will get one of these special values instead. If you keep computing with it, you'll end up with a bad final answer.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_10de5987e0aa">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-kinds-of-checking-1">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-1">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-kinds-of-checking-1" class="problems-wrapper" role="group"
aria-labelledby="01-kinds-of-checking-1-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-1" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-1/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="01-kinds-of-checking-1-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-1-problem-progress" tabindex="-1">
kinds of error checking
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-1-problem-progress"></div>
<div class="problem">
<div>
<p>Let's try some examples of buggy code and see how they behave in Java.</p>
<p>Are these bugs caught statically, dynamically, or not at all?</p>
<pre>
int n = 5;
if (n) {
n = n + 1;
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-kinds-of-checking-1_2_1">
<fieldset aria-describedby="status_01-kinds-of-checking-1_2_1">
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-1_2_1" id="input_01-kinds-of-checking-1_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-kinds-of-checking-1_2_1-choice_0-label" for="input_01-kinds-of-checking-1_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-1_2_1"> static error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-1_2_1" id="input_01-kinds-of-checking-1_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-kinds-of-checking-1_2_1-choice_1-label" for="input_01-kinds-of-checking-1_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-1_2_1"> dynamic error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-1_2_1" id="input_01-kinds-of-checking-1_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-kinds-of-checking-1_2_1-choice_2-label" for="input_01-kinds-of-checking-1_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-1_2_1"> no error, wrong answer
</label>
</div>
<span id="answer_01-kinds-of-checking-1_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-kinds-of-checking-1_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_01-kinds-of-checking-1_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="kinds of error checking" />
<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_01-kinds-of-checking-1" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-kinds-of-checking-1">
<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="01-kinds-of-checking-1-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="01-kinds-of-checking-1-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="01-kinds-of-checking-1-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@01-kinds-of-checking-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-kinds-of-checking-2" class="problems-wrapper" role="group"
aria-labelledby="01-kinds-of-checking-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-2/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="01-kinds-of-checking-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-2-problem-progress" tabindex="-1">
kinds of error checking, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-2-problem-progress"></div>
<div class="problem">
<div>
<pre>
int big = 200000; // 200,000
big = big * big; // big should be 4 billion now
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-kinds-of-checking-2_2_1">
<fieldset aria-describedby="status_01-kinds-of-checking-2_2_1">
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-2_2_1" id="input_01-kinds-of-checking-2_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-kinds-of-checking-2_2_1-choice_0-label" for="input_01-kinds-of-checking-2_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-2_2_1"> static error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-2_2_1" id="input_01-kinds-of-checking-2_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-kinds-of-checking-2_2_1-choice_1-label" for="input_01-kinds-of-checking-2_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-2_2_1"> dynamic error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-2_2_1" id="input_01-kinds-of-checking-2_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-kinds-of-checking-2_2_1-choice_2-label" for="input_01-kinds-of-checking-2_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-2_2_1"> no error, wrong answer
</label>
</div>
<span id="answer_01-kinds-of-checking-2_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-kinds-of-checking-2_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_01-kinds-of-checking-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="kinds of error checking, part 2" />
<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_01-kinds-of-checking-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-kinds-of-checking-2">
<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="01-kinds-of-checking-2-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="01-kinds-of-checking-2-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="01-kinds-of-checking-2-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@01-kinds-of-checking-3">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-3">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-kinds-of-checking-3" class="problems-wrapper" role="group"
aria-labelledby="01-kinds-of-checking-3-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-3" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-3/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="01-kinds-of-checking-3-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-3-problem-progress" tabindex="-1">
kinds of error checking, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-3-problem-progress"></div>
<div class="problem">
<div>
<pre>
double probability = 1/5;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-kinds-of-checking-3_2_1">
<fieldset aria-describedby="status_01-kinds-of-checking-3_2_1">
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-3_2_1" id="input_01-kinds-of-checking-3_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-kinds-of-checking-3_2_1-choice_0-label" for="input_01-kinds-of-checking-3_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-3_2_1"> static error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-3_2_1" id="input_01-kinds-of-checking-3_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-kinds-of-checking-3_2_1-choice_1-label" for="input_01-kinds-of-checking-3_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-3_2_1"> dynamic error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-3_2_1" id="input_01-kinds-of-checking-3_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-kinds-of-checking-3_2_1-choice_2-label" for="input_01-kinds-of-checking-3_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-3_2_1"> no error, wrong answer
</label>
</div>
<span id="answer_01-kinds-of-checking-3_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-kinds-of-checking-3_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_01-kinds-of-checking-3_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="kinds of error checking, part 3" />
<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_01-kinds-of-checking-3" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-kinds-of-checking-3">
<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="01-kinds-of-checking-3-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="01-kinds-of-checking-3-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="01-kinds-of-checking-3-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@01-kinds-of-checking-4">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-4">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-kinds-of-checking-4" class="problems-wrapper" role="group"
aria-labelledby="01-kinds-of-checking-4-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-4" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-4/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="01-kinds-of-checking-4-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-4-problem-progress" tabindex="-1">
kinds of error checking, part 4
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-4-problem-progress"></div>
<div class="problem">
<div>
<pre>
int sum = 0;
int n = 0;
int average = sum/n;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-kinds-of-checking-4_2_1">
<fieldset aria-describedby="status_01-kinds-of-checking-4_2_1">
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-4_2_1" id="input_01-kinds-of-checking-4_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-kinds-of-checking-4_2_1-choice_0-label" for="input_01-kinds-of-checking-4_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-4_2_1"> static error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-4_2_1" id="input_01-kinds-of-checking-4_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-kinds-of-checking-4_2_1-choice_1-label" for="input_01-kinds-of-checking-4_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-4_2_1"> dynamic error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-4_2_1" id="input_01-kinds-of-checking-4_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-kinds-of-checking-4_2_1-choice_2-label" for="input_01-kinds-of-checking-4_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-4_2_1"> no error, wrong answer
</label>
</div>
<span id="answer_01-kinds-of-checking-4_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-kinds-of-checking-4_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_01-kinds-of-checking-4_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="kinds of error checking, part 4" />
<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_01-kinds-of-checking-4" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-kinds-of-checking-4">
<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="01-kinds-of-checking-4-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="01-kinds-of-checking-4-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="01-kinds-of-checking-4-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-5" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-5">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-5">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-kinds-of-checking-5" class="problems-wrapper" role="group"
aria-labelledby="01-kinds-of-checking-5-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-5" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-5/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="01-kinds-of-checking-5-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-5-problem-progress" tabindex="-1">
kinds of error checking, part 5
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-kinds-of-checking-5-problem-progress"></div>
<div class="problem">
<div>
<pre>
double sum = 7;
double n = 0;
double average = sum/n;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-kinds-of-checking-5_2_1">
<fieldset aria-describedby="status_01-kinds-of-checking-5_2_1">
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-5_2_1" id="input_01-kinds-of-checking-5_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-kinds-of-checking-5_2_1-choice_0-label" for="input_01-kinds-of-checking-5_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-5_2_1"> static error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-5_2_1" id="input_01-kinds-of-checking-5_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-kinds-of-checking-5_2_1-choice_1-label" for="input_01-kinds-of-checking-5_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-5_2_1"> dynamic error
</label>
</div>
<div class="field">
<input type="radio" name="input_01-kinds-of-checking-5_2_1" id="input_01-kinds-of-checking-5_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-kinds-of-checking-5_2_1-choice_2-label" for="input_01-kinds-of-checking-5_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-kinds-of-checking-5_2_1"> no error, wrong answer
</label>
</div>
<span id="answer_01-kinds-of-checking-5_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-kinds-of-checking-5_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_01-kinds-of-checking-5_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="kinds of error checking, part 5" />
<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_01-kinds-of-checking-5" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-kinds-of-checking-5">
<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="01-kinds-of-checking-5-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="01-kinds-of-checking-5-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="01-kinds-of-checking-5-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Arrays_and_Collections">
<h2 class="hd hd-2 unit-title">Arrays and Collections</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_86b0290c561a">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_86b0290c561a">
<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_86b0290c561a"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_86b0290c561a/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_86b0290c561a/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_86b0290c561a/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_86b0290c561a/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:aGP2dPK_MqA", "prioritizeHls": false, "duration": 730.52, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000500_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000500/MIT600512016-V000500.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_86b0290c561a"></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_86b0290c561a">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_86b0290c561a">
<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_86b0290c561a/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_86b0290c561a/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_d368c68bc069">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_d368c68bc069">
<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="arrays_and_collections">Arrays and Collections</h2>
<div data-outline="arrays_and_collections">
<p>Let's change our hailstone computation so that it stores the sequence in a data structure, instead of just printing it out. Java has two kinds of list-like types that we could use: arrays and Lists.</p>
<p>Arrays are fixed-length sequences of another type T. For example, here's how to declare an array variable and construct an array value to assign to it:</p><pre><code class="hljs cpp"> <span class="hljs-keyword">int</span>[] a = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">100</span>];</code></pre>
<p>The <code>int[]</code> array type includes all possible array values, but a particular array value, once created, can never change its length. Operations on array types include:</p>
<ul>
<li>indexing a[2]</li>
<li>assignment a[2]=0</li>
<li>length a.length (note that this is different syntax from String.length() — a.length is not a method call, so you don't put parentheses after it)</li>
</ul>
<p>Here's a crack at the hailstone code using an array. We start by constructing the array, and then use an index variable i to step through the array, storing values of the sequence as we generate them.</p><pre><code class="hljs cpp"><span class="hljs-keyword">int</span>[] a = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-number">100</span>]; <span class="hljs-comment">// <==== DANGER WILL ROBINSON</span>
<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;
<span class="hljs-keyword">int</span> n = <span class="hljs-number">3</span>;
<span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) {
a[i] = n;
i++; <span class="hljs-comment">// very common shorthand for i=i+1</span>
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
} <span class="hljs-keyword">else</span> {
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>;
}
}
a[i] = n;
i++;</code></pre>
<p>Something should immediately smell wrong in this approach. What's that magic number 100? What would happen if we tried an n that turned out to have a very long hailstone sequence? It wouldn't fit in a length-100 array. We have a bug. Would Java catch the bug statically, dynamically, or not at all? Incidentally, bugs like these — overflowing a fixed-length array, which are commonly used in less-safe languages like C and C++ that don't do automatic runtime checking of array accesses — have been responsible for a large number of network security breaches and internet worms.</p>
<p>Instead of a fixed-length array, let's use the List type. Lists are variable-length sequences of another type T. Here's how we can declare a List variable and make a list value:</p><pre><code class="hljs php"> <span class="hljs-keyword">List</span><Integer> <span class="hljs-keyword">list</span> = <span class="hljs-keyword">new</span> ArrayList<Integer>();</code></pre>
<p>And here are some of its operations:</p>
<ul>
<li>indexing list.get(2)</li>
<li>assignment list.set(2, 0)</li>
<li>length list.size()</li>
</ul>
<p>Note that List is an interface, a type that can't be constructed directly with new, but that instead specifies the operations that a List must provide. We'll talk about this notion in a future class on abstract data types. ArrayList is a class, a concrete type that provides implementations of those operations. ArrayList isn't the only implementation of the List type, though it's the most commonly used one. LinkedList is another. Check them out in the Java API documentation, which you can find by searching the web for “Java API”. Get to know the Java API docs, they're your friend. (“API” means “application programmer interface,” and is commonly used as a synonym for “library.”)</p>
<p>Note also that we wrote <code>List<Integer></code> instead of <code>List<int></code>. Unfortunately we can't write <code>List<int></code> in direct analog to <code>int[]</code>. Lists only know how to deal with object types, not primitive types. In Java, each of the primitive types (which are written in lowercase and often abbreviated, like <code>int</code>) has an equivalent object type (which is capitalized, and fully spelled out, like <code>Integer</code>). Java requires us to use these object type equivalents when we parameterize a type with angle brackets; as far as I know, the only reason for this requirement is to remind the programmer that the list actually contains objects, which use more memory than primitive values. But in other contexts, Java automatically converts between <code>int</code> and <code>Integer</code>, so we can write <code>Integer i = 5</code> without any type error.</p>
<p>Here's the hailstone code written with Lists:</p><pre><code class="hljs cpp">List<Integer> <span class="hljs-built_in">list</span> = <span class="hljs-keyword">new</span> ArrayList<Integer>();
<span class="hljs-keyword">int</span> n = <span class="hljs-number">3</span>;
<span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) {
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
} <span class="hljs-keyword">else</span> {
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>;
}
}
<span class="hljs-built_in">list</span>.add(n);</code></pre>
<p>Not only simpler but safer too, because the List automatically enlarges itself to fit as many numbers as you add to it (until you run out of memory, of course).</p>
</div>
<h2 id="iterating">Iterating</h2>
<div data-outline="iterating">
<p>A for loop steps through the elements of an array or a list, just as in Python, though the syntax looks a little different. For example:</p><pre><code class="hljs cpp"><span class="hljs-comment">// find the maximum point of a hailstone sequence stored in list</span>
<span class="hljs-keyword">int</span> max = <span class="hljs-number">0</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x : <span class="hljs-built_in">list</span>) {
max = Math.max(x, max);
}</code></pre>
<p>You can iterate through arrays as well as lists. The same code would work if the list were replaced by an array.</p>
<p><code>Math.max()</code> is a handy function from the Java API. The <code>Math</code> class is full of useful functions like this — search for “java 8 Math” on the web to find the Math class documentation for Java version 8.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_982dfa07e839">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-collections">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-collections" class="problems-wrapper" role="group"
aria-labelledby="01-collections-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections/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="01-collections-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-problem-progress" tabindex="-1">
list vs. array
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-problem-progress"></div>
<div class="problem">
<div>
<p>Rewrite these variable declarations using Lists instead of arrays.</p>
<p>We're only declaring the variables, not initializing them with any value.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="inputtype_01-collections_2_1" class=" capa_inputtype textline">
<div class="unanswered ">
<label class="problem-group-label" for="input_01-collections_2_1" id="label_01-collections_2_1">String[] names</label>
<input type="text" name="input_01-collections_2_1" id="input_01-collections_2_1" aria-describedby="status_01-collections_2_1" value="" size="20"/>
<span class="trailing_text" id="trailing_text_01-collections_2_1"/>
<span class="status unanswered" id="status_01-collections_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_01-collections_2_1" class="answer"/>
</div>
</div></div>
<div class="solution-span">
<span id="solution_01-collections_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="list vs. array" />
<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_01-collections" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-collections">
<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="01-collections-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="01-collections-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="01-collections-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@01-collections-1">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-1">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-collections-1" class="problems-wrapper" role="group"
aria-labelledby="01-collections-1-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-1" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-1/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="01-collections-1-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-1-problem-progress" tabindex="-1">
list vs. array, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-1-problem-progress"></div>
<div class="problem">
<div>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="inputtype_01-collections-1_2_1" class=" capa_inputtype textline">
<div class="unanswered ">
<label class="problem-group-label" for="input_01-collections-1_2_1" id="label_01-collections-1_2_1">int[] numbers</label>
<input type="text" name="input_01-collections-1_2_1" id="input_01-collections-1_2_1" aria-describedby="status_01-collections-1_2_1" value="" size="20"/>
<span class="trailing_text" id="trailing_text_01-collections-1_2_1"/>
<span class="status unanswered" id="status_01-collections-1_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_01-collections-1_2_1" class="answer"/>
</div>
</div></div>
<div class="solution-span">
<span id="solution_01-collections-1_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="list vs. array, part 2" />
<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_01-collections-1" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-collections-1">
<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="01-collections-1-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="01-collections-1-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="01-collections-1-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@01-collections-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-collections-2" class="problems-wrapper" role="group"
aria-labelledby="01-collections-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-2/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="01-collections-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-2-problem-progress" tabindex="-1">
list vs. array, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-collections-2-problem-progress"></div>
<div class="problem">
<div>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="inputtype_01-collections-2_2_1" class=" capa_inputtype textline">
<div class="unanswered ">
<label class="problem-group-label" for="input_01-collections-2_2_1" id="label_01-collections-2_2_1">char[][] grid</label>
<input type="text" name="input_01-collections-2_2_1" id="input_01-collections-2_2_1" aria-describedby="status_01-collections-2_2_1" value="" size="20"/>
<span class="trailing_text" id="trailing_text_01-collections-2_2_1"/>
<span class="status unanswered" id="status_01-collections-2_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_01-collections-2_2_1" class="answer"/>
</div>
</div></div>
<div class="solution-span">
<span id="solution_01-collections-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="list vs. array, part 3" />
<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_01-collections-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-collections-2">
<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="01-collections-2-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="01-collections-2-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="01-collections-2-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@01-x-marks-the-spot">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-x-marks-the-spot">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-x-marks-the-spot" class="problems-wrapper" role="group"
aria-labelledby="01-x-marks-the-spot-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-x-marks-the-spot" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-x-marks-the-spot/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="3"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="01-x-marks-the-spot-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-x-marks-the-spot-problem-progress" tabindex="-1">
x marks the spot
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-x-marks-the-spot-problem-progress"></div>
<div class="problem">
<div>
<p>Java Maps work like Python dictionaries.</p>
<p>After we run this code:</p>
<pre>
Map&lt;String, Double&gt; treasures = new HashMap&lt;&gt;();
String x = "palm";
treasures.put("beach", 25.);
treasures.put("palm", 50.);
treasures.put("cove", 75.);
treasures.put("x", 100.);
treasures.put("palm", treasures.get("palm") + treasures.size());
treasures.remove("beach");
double found = 0;
for (double treasure : treasures.values()) {
found += treasure;
}
</pre>
<p>What is the value of...</p>
<p>treasures.get(x)</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div id="formulaequationinput_01-x-marks-the-spot_2_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_01-x-marks-the-spot_2_1" id="input_01-x-marks-the-spot_2_1" data-input-id="01-x-marks-the-spot_2_1" value="" aria-describedby="status_01-x-marks-the-spot_2_1" size="20"/>
<span class="trailing_text" id="trailing_text_01-x-marks-the-spot_2_1"/>
<span class="status unanswered" id="status_01-x-marks-the-spot_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_01-x-marks-the-spot_2_1" class="answer"/>
<div id="input_01-x-marks-the-spot_2_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<p>treasures.get("x")</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div id="formulaequationinput_01-x-marks-the-spot_3_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_01-x-marks-the-spot_3_1" id="input_01-x-marks-the-spot_3_1" data-input-id="01-x-marks-the-spot_3_1" value="" aria-describedby="status_01-x-marks-the-spot_3_1" size="20"/>
<span class="trailing_text" id="trailing_text_01-x-marks-the-spot_3_1"/>
<span class="status unanswered" id="status_01-x-marks-the-spot_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_01-x-marks-the-spot_3_1" class="answer"/>
<div id="input_01-x-marks-the-spot_3_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<p>found</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div id="formulaequationinput_01-x-marks-the-spot_4_1" class="inputtype formulaequationinput">
<div class="unanswered">
<input type="text" name="input_01-x-marks-the-spot_4_1" id="input_01-x-marks-the-spot_4_1" data-input-id="01-x-marks-the-spot_4_1" value="" aria-describedby="status_01-x-marks-the-spot_4_1" size="20"/>
<span class="trailing_text" id="trailing_text_01-x-marks-the-spot_4_1"/>
<span class="status unanswered" id="status_01-x-marks-the-spot_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
<p id="answer_01-x-marks-the-spot_4_1" class="answer"/>
<div id="input_01-x-marks-the-spot_4_1_preview" class="equation">
\(\)
<img src="/static/images/spinner.bc34f953403f.gif" class="loading" alt="Loading"/>
</div>
</div>
<div class="script_placeholder" data-src="/static/js/capa/src/formula_equation_preview.b1967ab28c31.js"/>
</div></div>
<div class="solution-span">
<span id="solution_01-x-marks-the-spot_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="x marks the spot" />
<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_01-x-marks-the-spot" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-x-marks-the-spot">
<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="01-x-marks-the-spot-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="01-x-marks-the-spot-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="01-x-marks-the-spot-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Methods">
<h2 class="hd hd-2 unit-title">Methods</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_9f5dfb6b5b7a">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_9f5dfb6b5b7a">
<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_9f5dfb6b5b7a"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_9f5dfb6b5b7a/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_9f5dfb6b5b7a/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_9f5dfb6b5b7a/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_9f5dfb6b5b7a/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:m5H0hKdwPiI", "prioritizeHls": false, "duration": 204.24, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000200_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000200/MIT600512016-V000200.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_9f5dfb6b5b7a"></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_9f5dfb6b5b7a">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_9f5dfb6b5b7a">
<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_9f5dfb6b5b7a/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_9f5dfb6b5b7a/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_5f29e710b009">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_5f29e710b009">
<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="methods">Methods</h2>
<div data-outline="methods">
<p>In Java, statements generally have to be inside a method, and every method has to be in a class, so the simplest way to write our hailstone program looks like this:</p><pre><code class="hljs cpp"> <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Hailstone {
<span class="hljs-comment">/**
* Compute a hailstone sequence.
* @param n Starting number for sequence. Assumes n > 0.
* @return hailstone sequence starting with n and ending with 1.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> List<Integer> hailstoneSequence(<span class="hljs-keyword">int</span> n) {
List<Integer> <span class="hljs-built_in">list</span> = <span class="hljs-keyword">new</span> ArrayList<Integer>();
<span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) {
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
} <span class="hljs-keyword">else</span> {
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>;
}
}
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">return</span> <span class="hljs-built_in">list</span>;
}
}</code></pre>
<p>Let's explain a few of the new things here.</p>
<p><code>public</code> means that any code, anywhere in your program, can refer to the class or method. Other access modifiers, like private, are used to get more safety in a program, and to guarantee immutability for immutable types. We'll talk more about them in an upcoming class.</p>
<p><code>static</code> means that the method doesn't take a self parameter — which in Java is implicit anyway, you won't ever see it as a method parameter. Static methods can't be called on an object. Contrast that with the List add() method or the String length() method, for example, which require an object to come first. Instead, the right way to call a static method uses the class name instead of an object reference:</p><pre><code class="hljs cpp"> Hailstone.hailstoneSequence(<span class="hljs-number">83</span>)</code></pre>
<p>Take note also of the comment before the method, because it's very important. This comment is a specification of the method, describing the inputs and outputs of the operation. The specification should be concise and clear and precise. The comment provides information that is not already clear from the method types. It doesn't say, for example, that n is an integer, because the int n declaration just below already says that. But it does say that n must be positive, which is not captured by the type declaration but is very important for the caller to know. </p>
<p>We'll have a lot more to say about how to write good specifications in a few classes, but you'll have to start reading them and using them right away. </p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_2f6e44a9bf62">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-reading-javadocs">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-reading-javadocs">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-reading-javadocs" class="problems-wrapper" role="group"
aria-labelledby="01-reading-javadocs-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-reading-javadocs" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-reading-javadocs/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="01-reading-javadocs-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-reading-javadocs-problem-progress" tabindex="-1">
reading javadocs
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-reading-javadocs-problem-progress"></div>
<div class="problem">
<div>
<p>After we run this code:</p>
<pre>
Map&lt;String, Integer&gt; treasures = new HashMap&lt;&gt;();
treasures.put("beach", 25);
Integer result = treasures.putIfAbsent("beach", 75);
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-reading-javadocs_2_1">
<fieldset aria-describedby="status_01-reading-javadocs_2_1">
<legend id="01-reading-javadocs_2_1-legend" class="response-fieldset-legend field-group-hd">What is result?</legend>
<div class="field">
<input type="radio" name="input_01-reading-javadocs_2_1" id="input_01-reading-javadocs_2_1_choice_0" class="field-input input-radio" value="choice_0"/><label id="01-reading-javadocs_2_1-choice_0-label" for="input_01-reading-javadocs_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-reading-javadocs_2_1"> 25
</label>
</div>
<div class="field">
<input type="radio" name="input_01-reading-javadocs_2_1" id="input_01-reading-javadocs_2_1_choice_1" class="field-input input-radio" value="choice_1"/><label id="01-reading-javadocs_2_1-choice_1-label" for="input_01-reading-javadocs_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-reading-javadocs_2_1"> 75
</label>
</div>
<div class="field">
<input type="radio" name="input_01-reading-javadocs_2_1" id="input_01-reading-javadocs_2_1_choice_2" class="field-input input-radio" value="choice_2"/><label id="01-reading-javadocs_2_1-choice_2-label" for="input_01-reading-javadocs_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-reading-javadocs_2_1"> another integer
</label>
</div>
<div class="field">
<input type="radio" name="input_01-reading-javadocs_2_1" id="input_01-reading-javadocs_2_1_choice_3" class="field-input input-radio" value="choice_3"/><label id="01-reading-javadocs_2_1-choice_3-label" for="input_01-reading-javadocs_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-reading-javadocs_2_1"> null
</label>
</div>
<span id="answer_01-reading-javadocs_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-reading-javadocs_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>To answer this question, you will have to search the web for Map's putIfAbsent method, and read its specification carefully.</p>
<div class="solution-span">
<span id="solution_01-reading-javadocs_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="reading javadocs" />
<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_01-reading-javadocs" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-reading-javadocs">
<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="01-reading-javadocs-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="01-reading-javadocs-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="01-reading-javadocs-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-mutating-values-vs-reassigning-variables">
<h2 class="hd hd-2 unit-title">Mutating Values Vs. Reassigning Variables</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_81ac25a03210">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_81ac25a03210">
<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_81ac25a03210"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_81ac25a03210/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_81ac25a03210/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_81ac25a03210/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_81ac25a03210/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:OqsgNdCVMgE", "prioritizeHls": false, "duration": 430.66, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000700_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000700/MIT600512016-V000700.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_81ac25a03210"></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_81ac25a03210">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_81ac25a03210">
<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_81ac25a03210/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_81ac25a03210/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_bbef140e5cb4">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_bbef140e5cb4">
<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="mutating_values_vs_reassigning_variables">Mutating Values vs. Reassigning Variables</h2>
<div data-outline="mutating_values_vs_reassigning_variables">
<p>It will be useful for us to draw pictures of what's happening at runtime in order to understand subtle questions. <strong>Snapshot diagrams</strong> represent the internal state of a program at runtime.</p>
<div class="panel panel-figure pull-right pull-margin"><img src="/assets/courseware/v1/babf99aaf4f0dc3423d6ccd29458b2b0/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/01-hailstone-snapshot.png" alt="snapshot diagram of a point in the hailstone sequence program where n is now 5 and list now contains 3, 10, and 5" width="300"></div>
<p>In its simplest form, a snapshot diagram shows each variable with an arrow pointing to its value. A value might be a primitive type like <code>int</code>, which is written simply as a number, or it might be an object type like <code>String</code> or <code>ArrayList</code>, which is shown as a circle with the name of the type in it. An object value often has its own internal variables with arrows pointing to their values as well. For example, at a particular point in the hailstone code starting from n=3, a snapshot diagram might look as shown on the right.</p>
<div class="clearfix"></div>
<p>Although the diagrams in this course will all use examples from Java, the notation can be applied to any modern programming language, e.g., Python, Javascript, C++, Ruby.</p>
<p>Snapshot diagrams give us a way to visualize the distinction between changing a variable and changing a value:</p>
<ul>
<li>
<p>When you assign to a variable, you're changing where the variable's arrow points. You can point it to a different value.</p>
<div class="panel panel-figure pull-right pull-margin" style="padding-right:100px"><img src="/assets/courseware/v1/e7c375a492efb17d29e3e9e75abf822e/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/01-snapshot-reassignment.png" alt="snapshot diagram showing n assigned to 3, then reassigned to 10, then reassigned to 5" width="100"></div>
<pre><code class="language-java hljs"><span class="hljs-keyword">int</span> n = <span class="hljs-number">3</span>;
n = 3*n + 1;
n = n/2;</code></pre>
<div class="clearfix"></div>
</li>
<li>
<p>When you change the contents of a mutable value — such as an array or list — you're changing references inside that value. This is called <strong>mutating</strong> the value.</p>
<div class="panel panel-figure pull-right pull-margin"><img src="/assets/courseware/v1/8c304ce222ab34d11943e5974a05521d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/01-snapshot-mutation.png" alt="snapshot diagram showing a list being created, having 3 and 5 added to it, then the 3 is replaced with 9" width="200"></div>
<pre><code class="language-java hljs">List<Integer> <span class="hljs-built_in">list</span> = <span class="hljs-keyword">new</span> ArrayList<Integer>();
<span class="hljs-built_in">list</span>.add(3);
<span class="hljs-built_in">list</span>.add(5);
<span class="hljs-built_in">list</span>.set(0, 9);</code></pre>
<div class="clearfix"></div>
</li>
</ul>
<h4>Reassignment and immutable values</h4>
<div class="panel panel-figure pull-right pull-margin"><img src="/assets/courseware/v1/2e2c4228d673d9fec24036674e5c999e/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/01-string-reassignment.png" alt="reassigning a variable" width="200"></div>
<p>For example, if we have a <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/String.html"><code>String</code></a> variable <code>s</code>, we can reassign it from a value of <code>"a"</code> to <code>"ab"</code>.</p>
<pre><code class="language-java hljs">String s = <span class="hljs-string">"a"</span>;
s = s + <span class="hljs-string">"b"</span>;</code></pre>
<p><code>String</code> is an example of an <em>immutable</em> type, a type whose values can never change once they have been created. Immutability (immunity from change) is a major design principle in this course. We'll talk much more about it in future readings.</p>
<p>Immutable objects (intended by their designer to always represent the same value) are denoted in a snapshot diagram by a double border, like the <code>String</code> objects in our diagram.</p>
<h4>Mutable values</h4>
<div class="panel panel-figure pull-right pull-margin"><img src="/assets/courseware/v1/c3bb51dde8aa4712756b9d2889a43a35/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/01-stringbuilder-mutation.png" alt="mutating an object" width="200"></div>
<p>By contrast, <a href="http://docs.oracle.com/javase/8/docs/api/?java/lang/StringBuilder.html"><code>StringBuilder</code></a> (another built-in Java class) is a <em>mutable</em> object that represents a string of characters. It has methods that change the value of the object:</p>
<pre><code class="language-java hljs">StringBuilder sb = <span class="hljs-keyword">new</span> StringBuilder(<span class="hljs-string">"a"</span>);
sb.append(<span class="hljs-string">"b"</span>);</code></pre>
<p>These two snapshot diagrams look very different, which is good: the difference between mutability and immutability will play an important role in making our code <em>safe from bugs</em>.</p>
<h4>Immutable references</h4>
<p>Java also gives us immutable references: variables that are assigned once and never reassigned. To make a reference immutable, declare it with the keyword <code>final</code>:</p>
<div class="panel panel-figure pull-right pull-margin" style="padding-right:100px"><img src="/assets/courseware/v1/b8a3cb2a35811375cc7013a34281a107/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/01-snapshot-final.png" alt="final reference is a double arrow" width="100"></div>
<pre><code class="language-java hljs"><span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> n = <span class="hljs-number">5</span>;</code></pre>
<p>If the Java compiler isn't convinced that your <code>final</code> variable will only be assigned once at runtime, then it will produce a compiler error. So <code>final</code> gives you static checking for immutable references.</p>
<p>In a snapshot diagram, an immutable reference (<code>final</code>) is denoted by a double arrow.</p>
<p>Notice that we can have an <em>immutable reference</em> to a <em>mutable value</em>. For example: <code>final StringBuilder sb</code>. This means that the StringBuilder value can change, but the <code>sb</code> variable's arrow can't change: it must always point to the same <code>StringBuilder</code> object.</p>
<p>We can also have a <em>mutable reference</em> to an <em>immutable value</em>, like <code>String s</code>. In this case, the value of the <code>s</code> variable can change because it can be re-pointed to a different object.</p>
<p>It's good practice to use <code>final</code> for declaring the parameters of a method and as many local variables as possible. Like the type of the variable, these declarations are important documentation, useful to the reader of the code and statically checked by the compiler. </p>
<p>There are two variables in our hailstoneSequence method: can we declare them <code>final</code>, or not?</p><pre><code class="hljs php"> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">List</span><Integer> hailstoneSequence(<span class="hljs-keyword yellow-highlight">final</span> int n) {
<span class="hljs-keyword yellow-highlight">final</span> <span class="hljs-keyword">List</span><Integer> <span class="hljs-keyword">list</span> = <span class="hljs-keyword">new</span> ArrayList<Integer>();
<span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) {
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
} <span class="hljs-keyword">else</span> {
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>;
}
}
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">return</span> <span class="hljs-built_in">list</span>;
}
}</code></pre></div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_eb86009d61e6">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-final">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-final">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-final" class="problems-wrapper" role="group"
aria-labelledby="01-final-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-final" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-final/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="01-final-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-final-problem-progress" tabindex="-1">
snapshot diagrams and final
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-final-problem-progress"></div>
<div class="problem">
<div>
<p>Suppose we want to add <code>final</code> to both variable declarations in the <code>hailstoneSequence</code> method, as shown:</p>
<pre><code class="hljs php"><span class="hljs-keyword">public </span><span class="hljs-keyword">static </span><span class="hljs-keyword">List</span>&lt;Integer&gt; hailstoneSequence(<span class="hljs-keyword yellow-highlight">final</span> int n) {
<span class="hljs-keyword yellow-highlight">final</span> <span class="hljs-keyword">List</span>&lt;Integer&gt; <span class="hljs-keyword">list</span> = <span class="hljs-keyword">new</span> ArrayList&lt;Integer&gt;();
<span class="hljs-keyword">while</span> (n != <span class="hljs-number">1</span>) {
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {
n = n / <span class="hljs-number">2</span>;
} <span class="hljs-keyword">else</span> {
n = <span class="hljs-number">3</span> * n + <span class="hljs-number">1</span>;
}
}
<span class="hljs-built_in">list</span>.add(n);
<span class="hljs-keyword">return</span> <span class="hljs-built_in">list</span>;
}</code></pre>
<p>Which of the following are true statements about putting <code>final</code> on <code>n</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-final_2_1">
<fieldset aria-describedby="status_01-final_2_1">
<div class="field">
<input type="checkbox" name="input_01-final_2_1[]" id="input_01-final_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="01-final_2_1-choice_0-label" for="input_01-final_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-final_2_1"> final can't be used on n because n is a parameter of a method
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-final_2_1[]" id="input_01-final_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="01-final_2_1-choice_1-label" for="input_01-final_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-final_2_1"> final can't be used on n because its type is int and int is already immutable
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-final_2_1[]" id="input_01-final_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="01-final_2_1-choice_2-label" for="input_01-final_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-final_2_1"> final can't be used on n because n is reassigned to other integer values in the body of the method
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-final_2_1[]" id="input_01-final_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="01-final_2_1-choice_3-label" for="input_01-final_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-final_2_1"> final can be used on n, and it prevents n from being reassigned
</label>
</div>
<span id="answer_01-final_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-final_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<p>Which of the following are true statements about putting <code>final</code> on <code>list</code>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-final_3_1">
<fieldset aria-describedby="status_01-final_3_1">
<div class="field">
<input class="field-input input-checkbox" name="input_01-final_3_1[]" id="input_01-final_3_1_choice_0" type="checkbox" value="choice_0"/><label class="response-label field-label label-inline" for="input_01-final_3_1_choice_0" id="01-final_3_1-choice_0-label" aria-describedby="status_01-final_3_1"> final can't be used on list because list is a local variable
</label>
</div>
<div class="field">
<input class="field-input input-checkbox" name="input_01-final_3_1[]" id="input_01-final_3_1_choice_1" type="checkbox" value="choice_1"/><label class="response-label field-label label-inline" for="input_01-final_3_1_choice_1" id="01-final_3_1-choice_1-label" aria-describedby="status_01-final_3_1"> final can't be used on list because its type is List<integer> and List<integer> is a mutable type
</integer></integer></label>
</div>
<div class="field">
<input class="field-input input-checkbox" name="input_01-final_3_1[]" id="input_01-final_3_1_choice_2" type="checkbox" value="choice_2"/><label class="response-label field-label label-inline" for="input_01-final_3_1_choice_2" id="01-final_3_1-choice_2-label" aria-describedby="status_01-final_3_1"> final can't be used on list because list.add() is used to change the list in the body of the method
</label>
</div>
<div class="field">
<input class="field-input input-checkbox" name="input_01-final_3_1[]" id="input_01-final_3_1_choice_3" type="checkbox" value="choice_3"/><label class="response-label field-label label-inline" for="input_01-final_3_1_choice_3" id="01-final_3_1-choice_3-label" aria-describedby="status_01-final_3_1"> final can be used on list, and it prevents the list variable from being reassigned
</label>
</div>
<span id="answer_01-final_3_1"/>
</fieldset>
<div class="indicator-container">
<span data-tooltip="Not yet answered." class="status unanswered" id="status_01-final_3_1">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_01-final_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="snapshot diagrams and final" />
<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_01-final" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-final">
<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="01-final-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="01-final-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="01-final-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Documenting_Assumptions">
<h2 class="hd hd-2 unit-title">Documenting Assumptions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b2cced620e63">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b2cced620e63">
<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_b2cced620e63"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b2cced620e63/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b2cced620e63/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b2cced620e63/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b2cced620e63/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:mACnFWnRbao", "prioritizeHls": false, "duration": 100.03, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000400_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000400/MIT600512016-V000400.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_b2cced620e63"></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_b2cced620e63">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_b2cced620e63">
<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_b2cced620e63/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_b2cced620e63/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_43a8788b70fd">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_43a8788b70fd">
<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="documenting_assumptions">Documenting Assumptions</h2>
<div data-outline="documenting_assumptions">
<p>Writing the type of a variable down documents an assumption about it: <code>int n</code> means that <code>n</code> will always refer to an integer, never to a string or list or any other type. Java actually checks this assumption at compile time and guarantees that there's no place in your program where you violated this assumption. </p>
<p>Declaring a variable <code>final</code> is also a form of documentation. <code>final List<Integer> list</code> is a claim that the <code>list</code> variable will always point to the same <code>List</code> object after its initial assignment. Java checks that too, statically.</p>
<p>In the specifcation comment above the hailstoneSequence method, we documented another assumption that Java (unfortunately) doesn't check automatically:</p>
<pre><code class="hljs cpp"> <span class="hljs-comment">/**
* Compute a hailstone sequence.
* @param n Starting number for sequence. <span class="yellow-highlight">Assumes n > 0.</span>
* @return hailstone sequence starting with n and ending with 1.
*/</span></code></pre>
<p>Why do we need to write down our assumptions? Because programming is full of assumptions. If we don't write them down, we won't remember them, and other people who need to read or change our programs later won't know them. They'll have to guess.</p>
<p>Programs have to be written with two goals in mind: </p>
<ul>
<li>communicating with the computer. First persuading the compiler that your program is sensible — syntactically correct and type-correct — and then getting the logic right so that it gives the right results at runtime. </li>
<li>communicating with other people. Making the program easy to understand, so that when somebody has to fix it, improve it, or adapt it in the future, they can do so.</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_5d3ecd560317">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-documenting-assumptions">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-documenting-assumptions" class="problems-wrapper" role="group"
aria-labelledby="01-documenting-assumptions-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions/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="01-documenting-assumptions-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-problem-progress" tabindex="-1">
documenting assumptions
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-problem-progress"></div>
<div class="problem">
<div>
<p>Consider the following simple Python function:</p>
<pre>
from math import sqrt
def funFactAbout(person):
if sqrt(person.age) == person.age:
print("The age of " + person.name + " is a perfect square: " + str(person.age))
</pre>
<p>Which of the following are assumptions made by this code, which must be true in order for it to run without errors?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-documenting-assumptions_2_1">
<fieldset aria-describedby="status_01-documenting-assumptions_2_1">
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions_2_1[]" id="input_01-documenting-assumptions_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="01-documenting-assumptions_2_1-choice_0-label" for="input_01-documenting-assumptions_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions_2_1"> <code>person</code> must be an object with <code>age</code> and <code>name</code> instance variables
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions_2_1[]" id="input_01-documenting-assumptions_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="01-documenting-assumptions_2_1-choice_1-label" for="input_01-documenting-assumptions_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions_2_1"> <code>person</code> is not <code>None</code>
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions_2_1[]" id="input_01-documenting-assumptions_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="01-documenting-assumptions_2_1-choice_2-label" for="input_01-documenting-assumptions_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions_2_1"> <code>person.age</code> must be a nonnegative number
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions_2_1[]" id="input_01-documenting-assumptions_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="01-documenting-assumptions_2_1-choice_3-label" for="input_01-documenting-assumptions_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions_2_1"> <code>person.age</code> must be an integer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions_2_1[]" id="input_01-documenting-assumptions_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="01-documenting-assumptions_2_1-choice_4-label" for="input_01-documenting-assumptions_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions_2_1"> <code>person.name</code> must be a string
</label>
</div>
<span id="answer_01-documenting-assumptions_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-documenting-assumptions_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_01-documenting-assumptions_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="documenting assumptions" />
<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_01-documenting-assumptions" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-documenting-assumptions">
<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="01-documenting-assumptions-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="01-documenting-assumptions-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="01-documenting-assumptions-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@01-documenting-assumptions-2">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-2">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-documenting-assumptions-2" class="problems-wrapper" role="group"
aria-labelledby="01-documenting-assumptions-2-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-2" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-2/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="01-documenting-assumptions-2-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-2-problem-progress" tabindex="-1">
documenting assumptions, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-documenting-assumptions-2-problem-progress"></div>
<div class="problem">
<div>
<p>If you were writing Java instead of Python, and your Java code needed to make <em>all</em> the assumptions below, then which of them could be documented by type declarations and statically checked by the Java compiler?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-documenting-assumptions-2_2_1">
<fieldset aria-describedby="status_01-documenting-assumptions-2_2_1">
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions-2_2_1[]" id="input_01-documenting-assumptions-2_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="01-documenting-assumptions-2_2_1-choice_0-label" for="input_01-documenting-assumptions-2_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions-2_2_1"> <code>person</code> must be an object with <code>age</code> and <code>name</code> instance variables
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions-2_2_1[]" id="input_01-documenting-assumptions-2_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="01-documenting-assumptions-2_2_1-choice_1-label" for="input_01-documenting-assumptions-2_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions-2_2_1"> <code>person</code> is not <code>null</code>
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions-2_2_1[]" id="input_01-documenting-assumptions-2_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="01-documenting-assumptions-2_2_1-choice_2-label" for="input_01-documenting-assumptions-2_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions-2_2_1"> <code>person.age</code> must be a nonnegative number
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions-2_2_1[]" id="input_01-documenting-assumptions-2_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="01-documenting-assumptions-2_2_1-choice_3-label" for="input_01-documenting-assumptions-2_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions-2_2_1"> <code>person.age</code> must be an integer
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-documenting-assumptions-2_2_1[]" id="input_01-documenting-assumptions-2_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="01-documenting-assumptions-2_2_1-choice_4-label" for="input_01-documenting-assumptions-2_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_01-documenting-assumptions-2_2_1"> <code>person.name</code> must be a string
</label>
</div>
<span id="answer_01-documenting-assumptions-2_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-documenting-assumptions-2_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_01-documenting-assumptions-2_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="documenting assumptions, part 2" />
<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_01-documenting-assumptions-2" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-documenting-assumptions-2">
<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="01-documenting-assumptions-2-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="01-documenting-assumptions-2-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="01-documenting-assumptions-2-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@The_Goal_of_6.005">
<h2 class="hd hd-2 unit-title">The Goal of 6.005</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_2726129da475">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="video" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_2726129da475">
<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_2726129da475"
class="video closed"
data-metadata='{"saveStateEnabled": false, "speed": null, "ytMetadataEndpoint": "", "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_2726129da475/handler/transcript/available_translations", "captionDataDir": null, "completionEnabled": false, "poster": null, "generalSpeed": 1.0, "transcriptLanguages": {"en": "English"}, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_2726129da475/handler/transcript/translation/__lang__", "start": 0.0, "end": 0.0, "savedVideoPosition": 0.0, "lmsRootURL": "https://openlearninglibrary.mit.edu", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_2726129da475/handler/xmodule_handler/save_user_state", "ytApiUrl": "https://www.youtube.com/iframe_api", "autoAdvance": false, "ytTestTimeout": 1500, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_2726129da475/handler/publish_completion", "showCaptions": "true", "autohideHtml5": false, "streams": "1.00:KF5oeHzEr9g", "prioritizeHls": false, "duration": 257.19, "transcriptLanguage": "en", "autoplay": false, "completionPercentage": 0.95, "recordedYoutubeIsAvailable": true, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT60051/MIT600512016-V000600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V000600/MIT600512016-V000600.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_2726129da475"></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_2726129da475">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_2726129da475">
<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_2726129da475/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_2726129da475/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_e27bd9f699b6">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_e27bd9f699b6">
<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="the_goal_of_6005">The Goal of 6.005</h2>
<div data-outline="the_goal_of_6005">
<p>Our primary goal in this course is learning how to produce software that is:</p>
<ul>
<li><strong>Safe from bugs</strong>. Correctness (correct behavior right now) and defensiveness (correct behavior in the future).</li>
<li><strong>Easy to understand</strong>. Has to communicate to future programmers who need to understand it and make changes in it (fixing bugs or adding new features). That future programmer might be you, months or years from now. You'll be surprised how much you forget if you don't write it down, and how much it helps your own future self to have a good design.</li>
<li><strong>Ready for change</strong>. Software always changes. Some designs make it easy to make changes; others require throwing away and rewriting a lot of code. </li>
</ul>
<p>There are other important properties of software (like performance, usability, security), and they may trade off against these three. But these are the Big Three that we care about in 6.005, and that software developers generally put foremost in the practice of building software. It's worth considering every language feature, every programming practice, every design pattern that we study in this course, and understanding how they relate to the Big Three.</p>
</div>
<h2 id="why_we_use_java_in_this_course">Why we use Java in this course</h2>
<div data-outline="why_we_use_java_in_this_course">
<p>Why is this course taught in Java, rather than Python or Javascript?</p>
<p><strong>Safety</strong> is the first reason. Java has static checking (primarily type checking, but other kinds of static checks too, like that your code returns values from methods declared to do so). We're studying software engineering in this course, and safety from bugs is a key tenet of that approach. Java dials safety up to 11, which makes it a good language for learning about good software engineering practices. It's certainly possible to write safe code in dynamic languages like Python, but it's easier to understand what you need to do if you learn how in a safe, statically-checked language.</p>
<p><strong>Ubiquity</strong> is another reason. Java is widely used in research, education, and industry. Java runs on many platforms, not just Windows/Mac/Linux. Java can be used for web programming, both on the server and in the client, and native Android programming is done in Java. Although other programming languages are far better suited to teaching programming (Scheme and ML come to mind), regrettably these languages aren't as widespread in the real world. Java on your resume will be recognized as a marketable skill. But don't get us wrong: the real skills you'll get from this course are not Java-specific, but carry over to any language that you might program in. The most important lessons from this course will survive language fads: safety, clarity, abstraction, and engineering instincts.</p>
<p>In any case, a good programmer must be <strong>multilingual</strong>. Programming languages are tools, and you have to use the right tool for the job. If you are a practicing programmer, you will certainly have to pick up other programming languages in the future, so get started now by learning a second one.</p>
<p>As a result of its ubiquity, Java has a wide array of interesting and useful <strong>libraries</strong> (both its enormous built-in library, and other libraries out on the net), and excellent free <strong>tools</strong> for development (IDEs like Eclipse, editors, compilers, test frameworks, profilers, code coverage, style checkers). Even Python is still behind Java in the richness of its ecosystem.</p>
<p>There are some reasons to regret using Java. It's wordy, which makes examples longer to write and harder to read. It's large, having accumulated many features over the years. It's internally inconsistent (e.g. the <code>final</code> keyword means different things in different contexts, and the <code>static</code> keyword in Java has nothing to do with static checking). It's weighted with the baggage of older languages like C/C++ (the primitive types and the <code>switch</code> statement are good examples). It has no interpreter like Python's where you can learn by playing with small bits of code.</p>
<p>But on the whole, Java is a reasonable choice of language right now to learn how to write code that is safe from bugs, easy to understand, and ready for change. And that's our goal.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_2db79abfb338">
<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-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<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@01-big-three">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="True" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="problem" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-big-three">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_01-big-three" class="problems-wrapper" role="group"
aria-labelledby="01-big-three-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-big-three" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-big-three/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="3"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="01-big-three-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-big-three-problem-progress" tabindex="-1">
the big three goals
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@01-big-three-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following ideas discussed in this reading help make your code more <strong>safe from bugs</strong>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-big-three_2_1">
<fieldset aria-describedby="status_01-big-three_2_1">
<div class="field">
<input type="checkbox" name="input_01-big-three_2_1[]" id="input_01-big-three_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="01-big-three_2_1-choice_0-label" for="input_01-big-three_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-big-three_2_1"> dynamic checking
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_2_1[]" id="input_01-big-three_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="01-big-three_2_1-choice_1-label" for="input_01-big-three_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-big-three_2_1"> final variables
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_2_1[]" id="input_01-big-three_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="01-big-three_2_1-choice_2-label" for="input_01-big-three_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-big-three_2_1"> integer overflow
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_2_1[]" id="input_01-big-three_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="01-big-three_2_1-choice_3-label" for="input_01-big-three_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-big-three_2_1"> static typing
</label>
</div>
<span id="answer_01-big-three_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-big-three_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_01-big-three_solution_1"/>
</div><p>Which of the following ideas discussed in this reading help make your code more <strong>easy to understand</strong>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-big-three_3_1">
<fieldset aria-describedby="status_01-big-three_3_1">
<div class="field">
<input type="checkbox" name="input_01-big-three_3_1[]" id="input_01-big-three_3_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="01-big-three_3_1-choice_0-label" for="input_01-big-three_3_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-big-three_3_1"> documented assumptions in comments
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_3_1[]" id="input_01-big-three_3_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="01-big-three_3_1-choice_1-label" for="input_01-big-three_3_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-big-three_3_1"> final variables
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_3_1[]" id="input_01-big-three_3_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="01-big-three_3_1-choice_2-label" for="input_01-big-three_3_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-big-three_3_1"> overloading
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_3_1[]" id="input_01-big-three_3_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="01-big-three_3_1-choice_3-label" for="input_01-big-three_3_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_01-big-three_3_1"> static typing
</label>
</div>
<span id="answer_01-big-three_3_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-big-three_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_01-big-three_solution_2"/>
</div><p>Which of the following ideas discussed in this reading help make your code more <strong>ready for change</strong>?</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="choicegroup capa_inputtype" id="inputtype_01-big-three_4_1">
<fieldset aria-describedby="status_01-big-three_4_1">
<div class="field">
<input type="checkbox" name="input_01-big-three_4_1[]" id="input_01-big-three_4_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="01-big-three_4_1-choice_0-label" for="input_01-big-three_4_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_01-big-three_4_1"> documented assumptions in comments
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_4_1[]" id="input_01-big-three_4_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="01-big-three_4_1-choice_1-label" for="input_01-big-three_4_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_01-big-three_4_1"> fixed-length arrays
</label>
</div>
<div class="field">
<input type="checkbox" name="input_01-big-three_4_1[]" id="input_01-big-three_4_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="01-big-three_4_1-choice_2-label" for="input_01-big-three_4_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_01-big-three_4_1"> methods
</label>
</div>
<span id="answer_01-big-three_4_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_01-big-three_4_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_01-big-three_solution_3"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="the big three goals" />
<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_01-big-three" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_01-big-three">
<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="01-big-three-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="01-big-three-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="01-big-three-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-class="LmsRuntime" data-init="VerticalStudentView" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="vertical" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_1_Summary">
<h2 class="hd hd-2 unit-title">Reading 1 Summary</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_2497ea73e273">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-class="LmsRuntime" data-init="XBlockToXModuleShim" data-graded="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-request-token="5c382639683111ef8c7a0e199eb5def5" data-runtime-version="1" data-block-type="html" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_2497ea73e273">
<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="summary">Summary</h2>
<div data-outline="summary">
<p>The main idea we introduced today is <strong>static checking</strong>. Here's how this idea relates to the goals of the course:</p>
<ul>
<li>
<p><strong>Safe from bugs.</strong> Static checking helps with safety by catching type errors and other bugs before runtime. </p>
</li>
<li>
<p><strong>Easy to understand.</strong> It helps with understanding, because types are explicitly stated in the code.</p>
</li>
<li>
<p><strong>Ready for change.</strong> Static checking makes it easier to change your code by identifying other places that need to change in tandem. For example, when you change the name or type of a variable, the compiler immediately displays errors at all the places where that variable is used, reminding you to update them as well.</p>
</li>
</ul>
</div>
<div class="license">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>