<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_9_Objectives" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Reading 9 Objectives</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b5a37d36981f">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b5a37d36981f" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_b5a37d36981f"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:W0dFlQqMYwI", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b5a37d36981f/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 31.04, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b5a37d36981f/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b5a37d36981f/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_b5a37d36981f/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005600/MIT600512016-V005600.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_b5a37d36981f"></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_b5a37d36981f">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_b5a37d36981f">
<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_b5a37d36981f/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_b5a37d36981f/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_bd047a2efbcc">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_bd047a2efbcc" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h4>Software in 6.005</h4>
<table class="table table-striped no-markdown">
<tbody>
<tr>
<th width="33%">Safe from bugs</th>
<th>Easy to understand</th>
<th>Ready for change</th>
</tr>
<tr>
<td>
Correct today and correct in the unknown future
</td>
<td>
Communicating clearly with future programmers, including future you
</td>
<td>
Designed to accommodate change without rewriting
</td>
</tr>
</tbody>
</table>
<h4>Objectives for Today</h4>
<p>Today's reading introduces two ideas:</p>
<ul>
<li>Abstract data types</li>
<li>Representation independence</li>
</ul>
<p>In this reading, we look at a powerful idea called abstract data types, which enable us to separate how we use a data structure in a program from the particular form of the data structure itself. </p>
<p>Abstract data types address a particularly dangerous problem: clients making assumptions about the type's internal representation. We'll see why this is dangerous and how it can be avoided. We'll also discuss the classification of operations and some principles of good design for abstract data types.</p>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_c7078225bfe5" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-26">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-26" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-26" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-26-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-26" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-26/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="09-access-control-26-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-26-problem-progress" tabindex="-1">
access control
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-26-problem-progress"></div>
<div class="problem">
<div>
<p>In order to understand abstract data types, you will need to be comfortable with access control in Java -- the <code>public</code> and <code>private</code> attributes. If you have trouble with these questions, you may want to start with this week's Java Tutor assignment and then come back to this reading.</p>
<p>The following questions about access control use the code below. Study it first, then answer the questions.</p>
<pre class="code">
class Wallet {
private int amount;
public void loanTo(Wallet that) {
// put all of this wallet's money into that wallet
/*A*/ that.amount += this.amount;
/*B*/ amount = 0;
}
public static void main(String[] args) {
/*C*/ Wallet w = new Wallet();
/*D*/ w.amount = 100;
/*E*/ w.loanTo(w);
}
}
class Person {
private Wallet w;
public int getNetWorth() {
/*F*/ return w.amount;
}
public boolean isBroke() {
/*G*/ return Wallet.amount == 0;
}
}
</pre>
<p>Which of the following statements are true about the line marked <code>/*A*/</code>? Check all that apply.</p>
<pre class="code">
that.amount += this.amount;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-26_2_1">
<fieldset aria-describedby="status_09-access-control-26_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-26_2_1[]" id="input_09-access-control-26_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-26_2_1-choice_0-label" for="input_09-access-control-26_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-26_2_1"> The reference to this.amount is allowed by Java.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-26_2_1[]" id="input_09-access-control-26_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-26_2_1-choice_1-label" for="input_09-access-control-26_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-26_2_1"> The reference to this.amount is not allowed by Java because it uses this to access a private field.
</label>
</div>
<span id="answer_09-access-control-26_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-26_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_09-access-control-26_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control" />
<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_09-access-control-26" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-26">
<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="09-access-control-26-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="09-access-control-26-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="09-access-control-26-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@09-access-control-27">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-27" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-27" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-27-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-27" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-27/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="09-access-control-27-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-27-problem-progress" tabindex="-1">
access control, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-27-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*A*/</code>? Check all that apply.</p>
<pre class="code">
that.amount += this.amount;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-27_2_1">
<fieldset aria-describedby="status_09-access-control-27_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-27_2_1[]" id="input_09-access-control-27_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-27_2_1-choice_0-label" for="input_09-access-control-27_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-27_2_1"> The reference to that.amount is allowed by Java.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-27_2_1[]" id="input_09-access-control-27_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-27_2_1-choice_1-label" for="input_09-access-control-27_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-27_2_1"> The reference to that.amount is not allowed by Java because that.amount is a private field in a different object.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-27_2_1[]" id="input_09-access-control-27_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-27_2_1-choice_2-label" for="input_09-access-control-27_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-27_2_1"> The reference to that.amount is not allowed by Java because it writes to a private field.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-27_2_1[]" id="input_09-access-control-27_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-27_2_1-choice_3-label" for="input_09-access-control-27_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-27_2_1"> The illegal access(es) are caught statically.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-27_2_1[]" id="input_09-access-control-27_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-access-control-27_2_1-choice_4-label" for="input_09-access-control-27_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-access-control-27_2_1"> The illegal access(es) are caught dynamically.
</label>
</div>
<span id="answer_09-access-control-27_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-27_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_09-access-control-27_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, 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_09-access-control-27" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-27">
<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="09-access-control-27-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="09-access-control-27-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="09-access-control-27-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@09-access-control-28">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-28" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-28" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-28-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-28" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-28/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="09-access-control-28-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-28-problem-progress" tabindex="-1">
access control, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-28-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*B*/</code>? Check all that apply.</p>
<pre class="code">
amount = 0;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-28_2_1">
<fieldset aria-describedby="status_09-access-control-28_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-28_2_1[]" id="input_09-access-control-28_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-28_2_1-choice_0-label" for="input_09-access-control-28_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-28_2_1"> The reference to amount is allowed by Java.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-28_2_1[]" id="input_09-access-control-28_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-28_2_1-choice_1-label" for="input_09-access-control-28_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-28_2_1"> The reference to amount is not allowed by Java because it doesn't use this.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-28_2_1[]" id="input_09-access-control-28_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-28_2_1-choice_2-label" for="input_09-access-control-28_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-28_2_1"> The illegal access is caught statically.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-28_2_1[]" id="input_09-access-control-28_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-28_2_1-choice_3-label" for="input_09-access-control-28_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-28_2_1"> The illegal access is caught dynamically.
</label>
</div>
<span id="answer_09-access-control-28_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-28_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_09-access-control-28_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, 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_09-access-control-28" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-28">
<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="09-access-control-28-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="09-access-control-28-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="09-access-control-28-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@09-access-control-29">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-29" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-29" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-29-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-29" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-29/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="09-access-control-29-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-29-problem-progress" tabindex="-1">
access control, part 4
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-29-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*C*/</code>? Check all that apply.</p>
<pre class="code">
Wallet w = new Wallet();
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-29_2_1">
<fieldset aria-describedby="status_09-access-control-29_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-29_2_1[]" id="input_09-access-control-29_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-29_2_1-choice_0-label" for="input_09-access-control-29_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-29_2_1"> The call to the Wallet() constructor is allowed by Java.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-29_2_1[]" id="input_09-access-control-29_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-29_2_1-choice_1-label" for="input_09-access-control-29_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-29_2_1"> The call to the Wallet() constructor is not allowed by Java because there is no public Wallet() constructor declared.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-29_2_1[]" id="input_09-access-control-29_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-29_2_1-choice_2-label" for="input_09-access-control-29_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-29_2_1"> The illegal access is caught statically.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-29_2_1[]" id="input_09-access-control-29_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-29_2_1-choice_3-label" for="input_09-access-control-29_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-29_2_1"> The illegal access is caught dynamically.
</label>
</div>
<span id="answer_09-access-control-29_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-29_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_09-access-control-29_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, 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_09-access-control-29" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-29">
<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="09-access-control-29-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="09-access-control-29-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="09-access-control-29-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@09-access-control-30">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-30" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-30" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-30-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-30" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-30/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="09-access-control-30-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-30-problem-progress" tabindex="-1">
access control, part 5
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-30-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*D*/</code>? Check all that apply.</p>
<pre class="code">
w.amount = 100;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-30_2_1">
<fieldset aria-describedby="status_09-access-control-30_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-30_2_1[]" id="input_09-access-control-30_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-30_2_1-choice_0-label" for="input_09-access-control-30_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-30_2_1"> The access to w.amount is allowed by Java.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-30_2_1[]" id="input_09-access-control-30_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-30_2_1-choice_1-label" for="input_09-access-control-30_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-30_2_1"> The access to w.amount is not allowed by Java because amount is private.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-30_2_1[]" id="input_09-access-control-30_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-30_2_1-choice_2-label" for="input_09-access-control-30_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-30_2_1"> The illegal access is caught statically.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-30_2_1[]" id="input_09-access-control-30_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-30_2_1-choice_3-label" for="input_09-access-control-30_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-30_2_1"> The illegal access is caught dynamically.
</label>
</div>
<span id="answer_09-access-control-30_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-30_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_09-access-control-30_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, 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_09-access-control-30" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-30">
<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="09-access-control-30-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="09-access-control-30-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="09-access-control-30-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-6" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-31">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-31" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-31" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-31-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-31" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-31/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="09-access-control-31-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-31-problem-progress" tabindex="-1">
access control, part 6
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-31-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*E*/</code>? Check all that apply.</p>
<pre class="code">
w.loanTo(w);
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-31_2_1">
<fieldset aria-describedby="status_09-access-control-31_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-31_2_1-choice_0-label" for="input_09-access-control-31_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> The call to loanTo() is allowed by Java.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-31_2_1-choice_1-label" for="input_09-access-control-31_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> The call to loanTo() is not allowed by Java because this and that will be aliases to the same object.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-31_2_1-choice_2-label" for="input_09-access-control-31_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> The problem will be found by a static check.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-31_2_1-choice_3-label" for="input_09-access-control-31_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> The problem will be found by a dynamic check.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-access-control-31_2_1-choice_4-label" for="input_09-access-control-31_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> After this line, the Wallet object pointed to by w will have amount 0.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="09-access-control-31_2_1-choice_5-label" for="input_09-access-control-31_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> After this line, the Wallet object pointed to by w will have amount 100.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-31_2_1[]" id="input_09-access-control-31_2_1_choice_6" class="field-input input-checkbox" value="choice_6"/><label id="09-access-control-31_2_1-choice_6-label" for="input_09-access-control-31_2_1_choice_6" class="response-label field-label label-inline" aria-describedby="status_09-access-control-31_2_1"> After this line, the Wallet object pointed to by w will have amount 200.
</label>
</div>
<span id="answer_09-access-control-31_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-31_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_09-access-control-31_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, part 6" />
<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_09-access-control-31" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-31">
<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="09-access-control-31-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="09-access-control-31-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="09-access-control-31-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-7" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-32">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-32" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-32" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-32-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-32" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-32/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="09-access-control-32-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-32-problem-progress" tabindex="-1">
access control, part 7
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-32-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*F*/</code>? Check all that apply.</p>
<pre class="code">
return w.amount;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-32_2_1">
<fieldset aria-describedby="status_09-access-control-32_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-32_2_1[]" id="input_09-access-control-32_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-32_2_1-choice_0-label" for="input_09-access-control-32_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-32_2_1"> The reference to w.amount is allowed by Java because both w and amount are private variables.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-32_2_1[]" id="input_09-access-control-32_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-32_2_1-choice_1-label" for="input_09-access-control-32_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-32_2_1"> The reference to w.amount is allowed by Java because amount is a primitive type, even though it's private.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-32_2_1[]" id="input_09-access-control-32_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-32_2_1-choice_2-label" for="input_09-access-control-32_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-32_2_1"> The reference to w.amount is not allowed by Java because amount is a private field in a different class.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-32_2_1[]" id="input_09-access-control-32_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-32_2_1-choice_3-label" for="input_09-access-control-32_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-32_2_1"> The illegal access is caught statically.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-32_2_1[]" id="input_09-access-control-32_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-access-control-32_2_1-choice_4-label" for="input_09-access-control-32_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-access-control-32_2_1"> The illegal access is caught dynamically.
</label>
</div>
<span id="answer_09-access-control-32_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-32_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_09-access-control-32_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, part 7" />
<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_09-access-control-32" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-32">
<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="09-access-control-32-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="09-access-control-32-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="09-access-control-32-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-8" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-33">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-33" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-access-control-33" class="problems-wrapper" role="group"
aria-labelledby="09-access-control-33-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-33" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-33/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="09-access-control-33-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-33-problem-progress" tabindex="-1">
access control, part 8
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-access-control-33-problem-progress"></div>
<div class="problem">
<div>
<p>Which of the following statements are true about the line marked <code>/*G*/</code>? Check all that apply.</p>
<pre class="code">
return Wallet.amount == 0;
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-access-control-33_2_1">
<fieldset aria-describedby="status_09-access-control-33_2_1">
<div class="field">
<input type="checkbox" name="input_09-access-control-33_2_1[]" id="input_09-access-control-33_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-access-control-33_2_1-choice_0-label" for="input_09-access-control-33_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-access-control-33_2_1"> The reference to Wallet.amount is allowed by Java because Wallet has permission to access its own private field amount.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-33_2_1[]" id="input_09-access-control-33_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-access-control-33_2_1-choice_1-label" for="input_09-access-control-33_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-access-control-33_2_1"> The reference to Wallet.amount is allowed by Java because amount is a static variable.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-33_2_1[]" id="input_09-access-control-33_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-access-control-33_2_1-choice_2-label" for="input_09-access-control-33_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-access-control-33_2_1"> The reference to Wallet.amount is not allowed by Java because amount is a private field.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-33_2_1[]" id="input_09-access-control-33_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-access-control-33_2_1-choice_3-label" for="input_09-access-control-33_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-access-control-33_2_1"> The reference to Wallet.amount is not allowed by Java because amount is an instance variable.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-33_2_1[]" id="input_09-access-control-33_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-access-control-33_2_1-choice_4-label" for="input_09-access-control-33_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-access-control-33_2_1"> The illegal access is caught statically.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-access-control-33_2_1[]" id="input_09-access-control-33_2_1_choice_5" class="field-input input-checkbox" value="choice_5"/><label id="09-access-control-33_2_1-choice_5-label" for="input_09-access-control-33_2_1_choice_5" class="response-label field-label label-inline" aria-describedby="status_09-access-control-33_2_1"> The illegal access is caught dynamically.
</label>
</div>
<span id="answer_09-access-control-33_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-access-control-33_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_09-access-control-33_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="access control, part 8" />
<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_09-access-control-33" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-access-control-33">
<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="09-access-control-33-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="09-access-control-33-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="09-access-control-33-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@What_Abstraction_Means" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">What Abstraction Means</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_adacc1397286">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_adacc1397286" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_adacc1397286"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:TIn7LtnyTIA", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_adacc1397286/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 271.86, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_adacc1397286/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_adacc1397286/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_adacc1397286/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005000_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005000/MIT600512016-V005000.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_adacc1397286"></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_adacc1397286">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_adacc1397286">
<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_adacc1397286/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_adacc1397286/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_5f06754dd3d1">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_5f06754dd3d1" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="what_abstraction_means">What Abstraction Means</h2>
<div data-outline="what_abstraction_means">
<p>Abstract data types are an instance of a general principle in software engineering, which goes by many names with slightly different shades of meaning. Here are some of the names that are used for this idea:</p>
<ul>
<li><strong>Abstraction.</strong> Omitting or hiding low-level details with a simpler, higher-level idea.</li>
<li><strong>Modularity.</strong> Dividing a system into components or modules, each of which can be designed, implemented, tested, reasoned about, and reused separately from the rest of the system.</li>
<li><strong>Encapsulation.</strong> Building walls around a module (a hard shell or capsule) so that the module is responsible for its own internal behavior, and bugs in other parts of the system can't damage its integrity. </li>
<li><strong>Information hiding.</strong> Hiding details of a module's implementation from the rest of the system, so that those details can be changed later without changing the rest of the system.</li>
<li><strong>Separation of concerns.</strong> Making a feature (or “concern”) the responsibility of a single module, rather than spreading it across multiple modules. </li>
</ul>
<p>As a software engineer, you should know these terms, because you will run into them frequently. The fundamental purpose of all of these ideas is to help achieve the three important properties that we care about in 6.005: safety from bugs, ease of understanding, and readiness for change.</p>
<h3 id="user-defined_types">User-Defined Types</h3>
<div data-outline="user-defined_types">
<p>In the early days of computing, a programming language came with built-in types (such as integers, booleans, strings, etc.) and built-in procedures, e.g., for input and output. Users could define their own procedures: that's how large programs were built.</p>
<p>A major advance in software development was the idea of abstract types: that one could design a programming language to allow user-defined types, too. This idea came out of the work of many researchers, notably Dahl (the inventor of the Simula language), Hoare (who developed many of the techniques we now use to reason about abstract types), Parnas (who coined the term information hiding and first articulated the idea of organizing program modules around the secrets they encapsulated), and here at MIT, Barbara Liskov and John Guttag, who did seminal work in the specification of abstract types, and in programming language support for them — and developed the original 6.170, the predecessor to 6.005. Barbara Liskov earned the Turing Award, computer science's equivalent of the Nobel Prize, for her work on abstract types.</p>
<p>The key idea of data abstraction is that a type is characterized by the operations you can perform on it. A number is something you can add and multiply; a string is something you can concatenate and take substrings of; a boolean is something you can negate, and so on. In a sense, users could already define their own types in early programming languages: you could create a record type date, for example, with integer fields for day, month, and year. But what made abstract types new and different was the focus on operations: the user of the type would not need to worry about how its values were actually stored, in the same way that a programmer can ignore how the compiler actually stores integers. All that matters is the operations.</p>
<p>In Java, as in many modern programming languages, the separation between built-in types and user-defined types is a bit blurry. The classes in java.lang, such as Integer and Boolean are built-in; whether you regard all the collections of java.util as built-in is less clear (and not very important anyway). Java complicates the issue by having primitive types that are not objects. The set of these types, such as int and boolean, cannot be extended by the user.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_b2e0e769cb86" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-adts">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-adts" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-adts" class="problems-wrapper" role="group"
aria-labelledby="09-adts-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-adts" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-adts/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="09-adts-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-adts-problem-progress" tabindex="-1">
abstract data types
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-adts-problem-progress"></div>
<div class="problem">
<div>
<p>Consider an abstract data type <code>Bool</code>. The type has the following operations:</p>
<pre>true : void &#8594; Bool
false : void &#8594; Bool
and : Bool &#215; Bool &#8594; Bool
or : Bool &#215; Bool &#8594; Bool
not : Bool &#8594; Bool
</pre>
<p>...where the first two operations construct the two values of the type, and last three operations have the usual meanings of logical <em>and</em>, logical <em>or</em>, and logical <em>not</em> on those values.</p>
<p>Which of the following are possible ways that <code>Bool</code> might be implemented, and still be able to satisfy the specs of the operations? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-adts_2_1">
<fieldset aria-describedby="status_09-adts_2_1">
<div class="field">
<input type="checkbox" name="input_09-adts_2_1[]" id="input_09-adts_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-adts_2_1-choice_0-label" for="input_09-adts_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-adts_2_1"> As a single bit, where 1 means true and 0 means false.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-adts_2_1[]" id="input_09-adts_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-adts_2_1-choice_1-label" for="input_09-adts_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-adts_2_1"> As an <code>int</code> value where 5 means true and 8 means false.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-adts_2_1[]" id="input_09-adts_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-adts_2_1-choice_2-label" for="input_09-adts_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-adts_2_1"> As a reference to a <code>String</code> object where <code>"false"</code> to mean true and <code>"true"</code> to mean false.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-adts_2_1[]" id="input_09-adts_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-adts_2_1-choice_3-label" for="input_09-adts_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-adts_2_1"> As a <code>long</code> value in which all possible values mean true.
</label>
</div>
<span id="answer_09-adts_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-adts_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_09-adts_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="abstract data types" />
<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_09-adts" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-adts">
<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="09-adts-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="09-adts-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="09-adts-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Classifying_Types_and_Operations" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Classifying Types and Operations</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_554dc804dd58">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_554dc804dd58" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_554dc804dd58"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:a3q3roBoCjI", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_554dc804dd58/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 503.41, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_554dc804dd58/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_554dc804dd58/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_554dc804dd58/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V004900_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V004900/MIT600512016-V004900.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_554dc804dd58"></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_554dc804dd58">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_554dc804dd58">
<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_554dc804dd58/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_554dc804dd58/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_af9c8c9dab8d">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_af9c8c9dab8d" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="classifying_types_and_operations">Classifying Types and Operations</h2>
<div data-outline="classifying_types_and_operations">
<p>Types, whether built-in or user-defined, can be classified as <strong>mutable</strong> or <strong>immutable</strong>. The objects of a mutable type can be changed: that is, they provide operations which when executed cause the results of other operations on the same object to give different results. So <code>Date</code> is mutable, because you can call <code>setMonth</code> and observe the change with the <code>getMonth</code> operation. But <code>String</code> is immutable, because its operations create new <code>String</code> objects rather than changing existing ones. Sometimes a type will be provided in two forms, a mutable and an immutable form.
<code>StringBuilder</code>, for example, is a mutable version of <code>String</code> (although the two are certainly not the same Java type, and are not interchangeable).</p>
<p>The operations of an abstract type are classified as follows:</p>
<ul>
<li><strong>Creators</strong> create new objects of the type. A creator may take an object as an argument, but not an object of the type being constructed.</li>
<li><strong>Producers</strong> create new objects from old objects of the type. The <code>concat</code> method of <code>String</code>, for example, is a producer: it takes two strings and produces a new one representing their concatenation.</li>
<li><strong>Observers</strong> take objects of the abstract type and return objects of a different type. The <code>size</code> method of <code>List</code>, for example, returns an <code>int</code>.</li>
<li><strong>Mutators</strong> change objects. The <code>add</code> method of <code>List</code>, for example, mutates a list by adding an element to the end.</li>
</ul>
<p>We can summarize these distinctions schematically like this (explanation to follow):</p>
<ul>
<li>creator: t* → T </li>
<li>producer: T+, t* → T</li>
<li>observer: T+, t* → t</li>
<li>mutator: T+, t* → void | t | T</li>
</ul>
<p>These show informally the shape of the signatures of operations in the various classes. Each <em>T</em> is the abstract type itself; each <em>t</em> is some other type. The <code>+</code> marker indicates that the type may occur one or more times in that part of the signature, and the <code>*</code> marker indicates that it occurs zero or more times. <code>|</code> indicates or. For example, a producer may take two values of the abstract type <em>T</em>, like <code>String.concat()</code> does:</p>
<pre><code> concat : String × String → String</code></pre>
<p>Some observers take zero arguments of other types <em>t</em>, such as:</p>
<pre><code> size : List → int</code></pre>
<p>... and others take several:</p>
<pre><code> regionMatches : String × boolean × int × String × int × int → boolean</code></pre>
<p>A creator operation is often implemented as a <em>constructor</em>, like <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList--"><code>new ArrayList()</code></a>. But a creator can simply be a static method instead, like <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-"><code>Arrays.asList()</code></a>. A creator implemented as a static method is often called a <strong>factory method</strong>. The various <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#valueOf-boolean-"><code>String.valueOf</code></a> methods in Java are other examples of creators implemented as factory methods.</p>
<p>Mutators are often signaled by a <code>void</code> return type. A method that returns void <em>must</em> be called for some kind of side-effect, since otherwise it doesn't return anything. But not all mutators return void. For example, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Set.html#add-E-"><code>Set.add()</code></a> returns a boolean that indicates whether the set was actually changed. In Java's graphical user interface toolkit, <a href="http://docs.oracle.com/javase/8/docs/api/java/awt/Container.html#add-java.awt.Component-"><code>Component.add()</code></a> returns the object itself, so that multiple <code>add()</code> calls can be <a href="http://en.wikipedia.org/wiki/Method_chaining">chained together</a>. </p>
<h3 id="abstract_data_type_examples">Abstract Data Type Examples</h3>
<div data-outline="abstract_data_type_examples">
<p>Here are some examples of abstract data types, along with some of their operations, grouped by kind.</p>
<p><strong><code>int</code></strong> is Java's primitive integer type.
<code>int</code> is immutable, so it has no mutators.</p>
<ul>
<li>creators: the numeric literals 0, 1, 2, …</li>
<li>producers: arithmetic operators +, -, ×, ÷</li>
<li>observers: comparison operators ==, !=, <, > </li>
<li>mutators: none (it's immutable)</li>
</ul>
<p><strong><code>List</code></strong> is Java's list type.
<code>List</code> is mutable.
<code>List</code> is also an interface, which means that other classes provide the actual implementation of the data type. These classes include <code>ArrayList</code> and <code>LinkedList</code>.</p>
<ul>
<li>creators: <code>ArrayList</code> and <code>LinkedList</code> constructors, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#singletonList-T-"><code>Collections.singletonList</code></a></li>
<li>producers: <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-T-"><code>Collections.unmodifiableList</code></a></li>
<li>observers: <code>size</code>, <code>get</code></li>
<li>mutators: <code>add</code>, <code>remove</code>, <code>addAll</code>, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-"><code>Collections.sort</code></a></li>
</ul>
<p><strong><code>String</code></strong> is Java's string type.
<code>String</code> is immutable.</p>
<ul>
<li>creators: <code>String</code> constructors</li>
<li>producers: <code>concat</code>, <code>substring</code>, <code>toUpperCase</code></li>
<li>observers: <code>length</code>, <code>charAt</code></li>
<li>mutators: none (it's immutable)</li>
</ul>
<p>This classification gives some useful terminology, but it's not perfect. In complicated data types, there may be an operation that is both a producer and a mutator, for example. Some people reserve the term <em>producer</em> only for operations that do no mutation.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_d19a12b757a6" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-operations-36">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-operations-36" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-operations-36" class="problems-wrapper" role="group"
aria-labelledby="09-operations-36-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-operations-36" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-operations-36/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="7"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="09-operations-36-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-operations-36-problem-progress" tabindex="-1">
operations
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-operations-36-problem-progress"></div>
<div class="problem">
<div>
<p>Each of the methods below is an operation on an abstract data type from the Java library. Click on the link to look at its documentation, then classify the operation.</p>
<p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#valueOf-java.lang.String-" target="javadoc">Integer.valueOf()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_2_1" id="input_09-operations-36_2_1" aria-describedby="status_09-operations-36_2_1">
<option value="option_09-operations-36_2_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_2_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_1"/>
</div><p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#mod-java.math.BigInteger-" target="javadoc">BigInteger.mod()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_3_1" id="input_09-operations-36_3_1" aria-describedby="status_09-operations-36_3_1">
<option value="option_09-operations-36_3_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_2"/>
</div><p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/util/List.html#addAll-java.util.Collection-" target="javadoc">List.addAll()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_4_1" id="input_09-operations-36_4_1" aria-describedby="status_09-operations-36_4_1">
<option value="option_09-operations-36_4_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_3"/>
</div><p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-" target="javadoc">Collections.unmodifiableList()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_5_1" id="input_09-operations-36_5_1" aria-describedby="status_09-operations-36_5_1">
<option value="option_09-operations-36_5_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_5_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_4"/>
</div><p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toUpperCase--" target="javadoc">String.toUpperCase()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 5" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_6_1" id="input_09-operations-36_6_1" aria-describedby="status_09-operations-36_6_1">
<option value="option_09-operations-36_6_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_6_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_6_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_5"/>
</div><p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/util/Set.html#contains-java.lang.Object-" target="javadoc">Set.contains()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 6" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_7_1" id="input_09-operations-36_7_1" aria-describedby="status_09-operations-36_7_1">
<option value="option_09-operations-36_7_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_7_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_7_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_6"/>
</div><p>
<a href="http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#readLine--" target="javadoc">BufferedReader.readLine()</a>
</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 7" role="group"><div class="inputtype option-input ">
<select name="input_09-operations-36_8_1" id="input_09-operations-36_8_1" aria-describedby="status_09-operations-36_8_1">
<option value="option_09-operations-36_8_1_dummy_default">Select an option</option>
<option value="creator"> creator</option>
<option value="producer"> producer</option>
<option value="observer"> observer</option>
<option value="mutator"> mutator</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-operations-36_8_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-operations-36_8_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-operations-36_solution_7"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="operations" />
<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_09-operations-36" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-operations-36">
<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="09-operations-36-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="09-operations-36-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="09-operations-36-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-designing-an-abstract-type" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Designing an Abstract Type</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6218bb47dce2">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6218bb47dce2" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_6218bb47dce2"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:swHlG0FK6k8", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6218bb47dce2/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 554.89, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6218bb47dce2/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6218bb47dce2/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_6218bb47dce2/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005300_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V005300/MIT600512016-V005300.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_6218bb47dce2"></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_6218bb47dce2">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_6218bb47dce2">
<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_6218bb47dce2/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_6218bb47dce2/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_e0c4977ab816">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_e0c4977ab816" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="designing_an_abstract_type">Designing an Abstract Type</h2>
<div data-outline="designing_an_abstract_type">
<p>Designing an abstract data type involves choosing good operations and determining how they should behave. Here are a few rules of thumb.</p>
<p>It's better to have <strong>a few, simple operations</strong> that can be combined in powerful ways, rather than lots of complex operations.</p>
<p>Each operation should have a well-defined purpose and should have a <strong>coherent</strong> behavior rather than a panoply of special cases. We probably shouldn't add a <code>sum</code> operation to <code>List</code>, for example. It might help clients who work with lists of integers, but what about lists of strings? Or nested lists? All these special cases would make <code>sum</code> a hard operation to understand and use.</p>
<p>The set of operations should be <strong>adequate</strong> in the sense that there must be enough to do the kinds of computations clients are likely to want to do. A good test is to check that every property of an object of the type can be extracted. For example, if there were no get operation, we would not be able to find out what the elements of a list are. Basic information should not be inordinately difficult to obtain. For example, the size method is not strictly necessary for List, because we could apply get on increasing indices until we get a failure, but this is inefficient and inconvenient.</p>
<p>The type may be generic: a list or a set, or a graph, for example. Or it may be domain-specific: a street map, an employee database, a phone book, etc. But <strong>it should not mix generic and domain-specific features.</strong> A <code>Deck</code> type intended to represent a sequence of playing cards shouldn't have a generic <code>add</code> method that accepts arbitrary objects like integers or strings. Conversely, it wouldn't make sense to put a domain-specific method like <code>dealCards</code> into the generic type <code>List</code>.</p>
</div>
<h2 id="representation_independence">Representation Independence</h2>
<div data-outline="representation_independence">
<p>Critically, a good abstract data type (ADT) should be <strong>representation independent</strong>. This means that the use of an abstract type is independent of its representation (the actual data structure or data fields used to implement it), so that changes in representation have no effect on code outside the abstract type itself. For example, the operations offered by List are independent of whether the list is represented as a linked list or as an array.</p>
<p>You won't be able to change the representation of an ADT at all unless its operations are fully specified with preconditions and postconditions, so that clients know what to depend on, and you know what you can safely change.</p>
<h3 id="example_different_representations_for_strings">Example: Different Representations for Strings</h3>
<div data-outline="example_different_representations_for_strings">
<p>Let's look at a simple abstract data type to see what representation independence means and why it's useful. The <code>MyString</code> type below has far fewer operations than the real Java <code>String</code>, and their specs are a little different, but it's still illustrative. Here are the specs for the ADT:</p><pre><code class="language-java hljs"><span class="hljs-comment handout-javadoc-comment">/** MyString represents an immutable sequence of characters. */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyString</span> </span>{
<span class="hljs-comment">//////////////////// Example of a creator operation ///////////////</span>
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@param</span> b a boolean value
* <span class="hljs-doctag">@return</span> string representation of b, either "true" or "false" */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> MyString <span class="hljs-title">valueOf</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> b)</span> </span>{ ... }
<span class="hljs-comment">//////////////////// Examples of observer operations ///////////////</span>
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@return</span> number of characters in this string */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">length</span><span class="hljs-params">()</span> </span>{ ... }
<span class="hljs-comment handout-javadoc-comment">/** <span class="hljs-doctag">@param</span> i character position (requires 0 <= i < string length)
* <span class="hljs-doctag">@return</span> character at position i */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">char</span> <span class="hljs-title">charAt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{ ... }
<span class="hljs-comment">//////////////////// Example of a producer operation /////////////// </span>
<span class="hljs-comment handout-javadoc-comment">/** Get the substring between start (inclusive) and end (exclusive).
* <span class="hljs-doctag">@param</span> start starting index
* <span class="hljs-doctag">@param</span> end ending index. Requires 0 <= start <= end <= string length.
* <span class="hljs-doctag">@return</span> string consisting of charAt(start)...charAt(end-1) */</span>
<span class="hljs-function"><span class="hljs-keyword">public</span> MyString <span class="hljs-title">substring</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span> </span>{ ... }
}</code></pre>
<p>These public operations and their specifications are the only information that a client of this data type is allowed to know. Following the test-first programming paradigm, in fact, the first client we should create is a test suite that exercises these operations according to their specs. At the moment, however, writing test cases that use <code>assertEquals</code> directly on <code>MyString</code> objects wouldn't work, because we don't have an equality operation defined on <code>MyString</code>. We'll talk about how to implement equality carefully in a later reading. For now, the only operations we can perform with MyStrings are the ones we've defined above: <code>valueOf</code>, <code>length</code>, <code>charAt</code>, and <code>substring</code>. Our tests have to limit themselves to those operations. For example, here's one test for the <code>valueOf</code> operation:</p><pre><code class="language-java hljs">MyString s = MyString.valueOf(<span class="hljs-keyword">true</span>);
assertEquals(<span class="hljs-number">4</span>, s.length());
assertEquals(<span class="hljs-string">'t'</span>, s.charAt(<span class="hljs-number">0</span>));
assertEquals(<span class="hljs-string">'r'</span>, s.charAt(<span class="hljs-number">1</span>));
assertEquals(<span class="hljs-string">'u'</span>, s.charAt(<span class="hljs-number">2</span>));
assertEquals(<span class="hljs-string">'e'</span>, s.charAt(<span class="hljs-number">3</span>));</code></pre>
<p>We'll come back to the question of testing ADTs at the end of this reading.</p>
<p>For now, let's look at a simple representation for <code>MyString</code>: just an array of characters, which is exactly the length of the string with no extra room at the end. Here's how that internal representation would be declared, as an instance variable within the class:</p><pre><code class="language-java hljs"><span class="hljs-keyword">private</span> <span class="hljs-keyword">char</span>[] a;</code></pre>
<p>With that choice of representation, the operations would be implemented in a straightforward way:</p><pre><code class="language-java hljs"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> MyString <span class="hljs-title">valueOf</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> b)</span> </span>{
MyString s = <span class="hljs-keyword">new</span> MyString();
s.a = b ? <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'t'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'e'</span> }
: <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'f'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'e'</span> };
<span class="hljs-keyword">return</span> s;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">length</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> a.length;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">char</span> <span class="hljs-title">charAt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{
<span class="hljs-keyword">return</span> a[i];
}
<span class="hljs-function"><span class="hljs-keyword">public</span> MyString <span class="hljs-title">substring</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span> </span>{
MyString that = <span class="hljs-keyword">new</span> MyString();
that.a = <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[end - start];
System.arraycopy(<span class="hljs-keyword">this</span>.a, start, that.a, <span class="hljs-number">0</span>, end - start);
<span class="hljs-keyword">return</span> that;
}</code></pre>
<p>Question to ponder: Why don't <code>charAt</code> and <code>substring</code> have to check whether their parameters are within the valid range? What do you think will happen if the client calls these implementations with illegal inputs?</p>
<p>One problem with this implementation is that it's passing up an opportunity for performance improvement. Because this data type is immutable, the <code>substring</code> operation doesn't really have to copy characters out into a fresh array. It could just point to the original <code>MyString</code> object's character array and keep track of the start and end that the new substring object represents. The String implementation in some versions of Java do this.</p>
<p>To implement this optimization, we could change the internal representation of this class to:</p><pre><code class="language-java hljs"><span class="hljs-keyword">private</span> <span class="hljs-keyword">char</span>[] a;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> start;
<span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> end;</code></pre>
<p>With this new representation, the operations are now implemented like this:</p><pre><code class="language-java hljs"> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> MyString <span class="hljs-title">valueOf</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> b)</span> </span>{
MyString s = <span class="hljs-keyword">new</span> MyString();
s.a = b ? <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'t'</span>, <span class="hljs-string">'r'</span>, <span class="hljs-string">'u'</span>, <span class="hljs-string">'e'</span> }
: <span class="hljs-keyword">new</span> <span class="hljs-keyword">char</span>[] { <span class="hljs-string">'f'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'l'</span>, <span class="hljs-string">'s'</span>, <span class="hljs-string">'e'</span> };
s.start = <span class="hljs-number">0</span>;
s.end = s.a.length;
<span class="hljs-keyword">return</span> s;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">length</span><span class="hljs-params">()</span> </span>{
<span class="hljs-keyword">return</span> end - start;
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">char</span> <span class="hljs-title">charAt</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span> </span>{
<span class="hljs-keyword">return</span> a[start + i];
}
<span class="hljs-function"><span class="hljs-keyword">public</span> MyString <span class="hljs-title">substring</span><span class="hljs-params">(<span class="hljs-keyword">int</span> start, <span class="hljs-keyword">int</span> end)</span> </span>{
MyString that = <span class="hljs-keyword">new</span> MyString();
that.a = <span class="hljs-keyword">this</span>.a;
that.start = <span class="hljs-keyword">this</span>.start + start;
that.end = <span class="hljs-keyword">this</span>.start + end;
<span class="hljs-keyword">return</span> that;
}</code></pre>
<p>Because <code>MyString</code>'s existing clients depend only on the specs of its public methods, not on its private fields, we can make this change without having to inspect and change all that client code. That's the power of representation independence.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_b8cff034a92c" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-37">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-37" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-representations-37" class="problems-wrapper" role="group"
aria-labelledby="09-representations-37-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-37" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-37/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="09-representations-37-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-37-problem-progress" tabindex="-1">
representations
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-37-problem-progress"></div>
<div class="problem">
<div>
<p>Consider the following abstract data type.</p>
<pre class="code">
/**
* Represents a family that lives in a household together.
* A family always has at least one person in it.
* Families are mutable.
*/
class Family {
// the people in the family, sorted from oldest to youngest, with no duplicates.
public List&lt;Person&gt; people;
/**
* @return a list containing all the members of the family, with no duplicates.
*/
public List&lt;Person&gt; getMembers() {
return people;
}
}
</pre>
<p>Here is a client of this abstract data type:</p>
<pre class="code">
void client1(Family f) {
// get youngest person in the family
Person baby = f.people.get(f.people.size()-1);
...
}
</pre>
<p>Assume all this code works correctly (both Family and client1) and passes all its tests.</p>
<p>Now Family's representation is changed from a List to Set, as shown:</p>
<pre class="code">
/**
* Represents a family that lives in a household together.
* A family always has at least one person in it.
* Families are mutable.
*/
class Family {
// the people in the family
public Set&lt;Person&gt; people;
/**
* @return a list containing all the members of the family, with no duplicates.
*/
public List&lt;Person&gt; getMembers() {
return new ArrayList&lt;&gt;(people);
}
}
</pre>
<p>Assume that Family compiles correctly after the change. </p>
<p>Which of the following statements are true about client1 after Family is changed? Check all that apply. You may find it useful to look at the online Java API documentation for List and Set to understand how client1 will be affected.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-representations-37_2_1">
<fieldset aria-describedby="status_09-representations-37_2_1">
<div class="field">
<input type="checkbox" name="input_09-representations-37_2_1[]" id="input_09-representations-37_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-representations-37_2_1-choice_0-label" for="input_09-representations-37_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-representations-37_2_1"> client1 is independent of Family's representation, so it keeps working correctly.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-37_2_1[]" id="input_09-representations-37_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-representations-37_2_1-choice_1-label" for="input_09-representations-37_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-representations-37_2_1"> client1 depends on Family's representation, and the dependency would be caught as a static error.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-37_2_1[]" id="input_09-representations-37_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-representations-37_2_1-choice_2-label" for="input_09-representations-37_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-representations-37_2_1"> client1 depends on Family's representation, and the dependency would be caught as a dynamic error.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-37_2_1[]" id="input_09-representations-37_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-representations-37_2_1-choice_3-label" for="input_09-representations-37_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-representations-37_2_1"> client1 depends on Family's representation, and the dependency would not be caught but would produce a wrong answer at runtime.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-37_2_1[]" id="input_09-representations-37_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-representations-37_2_1-choice_4-label" for="input_09-representations-37_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-representations-37_2_1"> client1 depends on Family's representation, and the dependency would not be caught but would (luckily) still produce the same answer.
</label>
</div>
<span id="answer_09-representations-37_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-37_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_09-representations-37_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="representations" />
<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_09-representations-37" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-representations-37">
<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="09-representations-37-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="09-representations-37-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="09-representations-37-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@09-representations-38">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-38" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-representations-38" class="problems-wrapper" role="group"
aria-labelledby="09-representations-38-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-38" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-38/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="09-representations-38-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-38-problem-progress" tabindex="-1">
representations, part 2
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-38-problem-progress"></div>
<div class="problem">
<div>
<p>Now consider client2:</p>
<pre class="code">
void client2(Family f) {
// get size of the family
int familySize = f.people.size();
...
}
</pre>
<p>Which of the following statements are true about client2 after Family is changed? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-representations-38_2_1">
<fieldset aria-describedby="status_09-representations-38_2_1">
<div class="field">
<input type="checkbox" name="input_09-representations-38_2_1[]" id="input_09-representations-38_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-representations-38_2_1-choice_0-label" for="input_09-representations-38_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-representations-38_2_1"> client2 is independent of Family's representation, so it keeps working correctly.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-38_2_1[]" id="input_09-representations-38_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-representations-38_2_1-choice_1-label" for="input_09-representations-38_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-representations-38_2_1"> client2 depends on Family's representation, and the dependency would be caught as a static error.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-38_2_1[]" id="input_09-representations-38_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-representations-38_2_1-choice_2-label" for="input_09-representations-38_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-representations-38_2_1"> client2 depends on Family's representation, and the dependency would be caught as a dynamic error.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-38_2_1[]" id="input_09-representations-38_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-representations-38_2_1-choice_3-label" for="input_09-representations-38_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-representations-38_2_1"> client2 depends on Family's representation, and the dependency would not be caught but would produce a wrong answer at runtime.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-38_2_1[]" id="input_09-representations-38_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-representations-38_2_1-choice_4-label" for="input_09-representations-38_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-representations-38_2_1"> client2 depends on Family's representation, and the dependency would not be caught but would (luckily) still produce the same answer.
</label>
</div>
<span id="answer_09-representations-38_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-38_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_09-representations-38_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="representations, 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_09-representations-38" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-representations-38">
<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="09-representations-38-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="09-representations-38-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="09-representations-38-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@09-representations-39">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-39" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-representations-39" class="problems-wrapper" role="group"
aria-labelledby="09-representations-39-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-39" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-39/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="09-representations-39-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-39-problem-progress" tabindex="-1">
representations, part 3
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-39-problem-progress"></div>
<div class="problem">
<div>
<p>Now consider client3:</p>
<pre class="code">
void client3(Family f) {
// get any person in the family
Person anybody = f.getMembers().get(0);
...
}
</pre>
<p>Which of the following statements are true about client3 after Family is changed? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-representations-39_2_1">
<fieldset aria-describedby="status_09-representations-39_2_1">
<div class="field">
<input type="checkbox" name="input_09-representations-39_2_1[]" id="input_09-representations-39_2_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-representations-39_2_1-choice_0-label" for="input_09-representations-39_2_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-representations-39_2_1"> client3 is independent of Family's representation, so it keeps working correctly.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-39_2_1[]" id="input_09-representations-39_2_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-representations-39_2_1-choice_1-label" for="input_09-representations-39_2_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-representations-39_2_1"> client3 depends on Family's representation, and the dependency would be caught as a static error.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-39_2_1[]" id="input_09-representations-39_2_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-representations-39_2_1-choice_2-label" for="input_09-representations-39_2_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-representations-39_2_1"> client3 depends on Family's representation, and the dependency would be caught as a dynamic error.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-39_2_1[]" id="input_09-representations-39_2_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-representations-39_2_1-choice_3-label" for="input_09-representations-39_2_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-representations-39_2_1"> client3 depends on Family's representation, and the dependency would not be caught but would produce a wrong answer at runtime.
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-representations-39_2_1[]" id="input_09-representations-39_2_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-representations-39_2_1-choice_4-label" for="input_09-representations-39_2_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-representations-39_2_1"> client3 depends on Family's representation, and the dependency would not be caught but would (luckily) still produce the same answer.
</label>
</div>
<span id="answer_09-representations-39_2_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-39_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_09-representations-39_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="representations, 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_09-representations-39" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-representations-39">
<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="09-representations-39-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="09-representations-39-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="09-representations-39-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@09-representations-40">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-40" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-representations-40" class="problems-wrapper" role="group"
aria-labelledby="09-representations-40-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-40" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-40/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="7"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="09-representations-40-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-40-problem-progress" tabindex="-1">
representations, part 4
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-representations-40-problem-progress"></div>
<div class="problem">
<div>
<pre class="code">
1 /**
2 * Represents a family that lives in a household together.
3 * A family always has at least one person in it.
4 * Families are mutable.
5 */
6 public class Family {
7 // the people in the family, sorted from oldest to youngest, with no duplicates.
8 private List&lt;Person&gt; people;
9
10 /**
11 * @return a list containing all the members of the family, with no duplicates.
12 */
13 public List&lt;Person&gt; getMembers() {
14 return people;
15 }
16 }
</pre>
<p>Which lines of code are part of the data type's specification, which are part of its representation, and which are part of its implementation?</p>
<p>lines 1-5</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_2_1" id="input_09-representations-40_2_1" aria-describedby="status_09-representations-40_2_1">
<option value="option_09-representations-40_2_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_2_1"/>
</div></div>
<p>line 6</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_3_1" id="input_09-representations-40_3_1" aria-describedby="status_09-representations-40_3_1">
<option value="option_09-representations-40_3_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_3_1"/>
</div></div>
<p>line 7</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_4_1" id="input_09-representations-40_4_1" aria-describedby="status_09-representations-40_4_1">
<option value="option_09-representations-40_4_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_4_1"/>
</div></div>
<p>line 8</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_5_1" id="input_09-representations-40_5_1" aria-describedby="status_09-representations-40_5_1">
<option value="option_09-representations-40_5_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_5_1"/>
</div></div>
<p>lines 10-12</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 5" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_6_1" id="input_09-representations-40_6_1" aria-describedby="status_09-representations-40_6_1">
<option value="option_09-representations-40_6_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_6_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_6_1"/>
</div></div>
<p>line 13</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 6" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_7_1" id="input_09-representations-40_7_1" aria-describedby="status_09-representations-40_7_1">
<option value="option_09-representations-40_7_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_7_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_7_1"/>
</div></div>
<p>line 14</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 7" role="group"><div class="inputtype option-input ">
<select name="input_09-representations-40_8_1" id="input_09-representations-40_8_1" aria-describedby="status_09-representations-40_8_1">
<option value="option_09-representations-40_8_1_dummy_default">Select an option</option>
<option value="specification"> specification</option>
<option value="representation"> representation</option>
<option value="implementation"> implementation</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-representations-40_8_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-representations-40_8_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-representations-40_solution_1"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="representations, 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_09-representations-40" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-representations-40">
<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="09-representations-40-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="09-representations-40-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="09-representations-40-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-realizing-adt-concepts-in-java" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Realizing ADT Concepts in Java</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3addd3d71705">
<div class="xblock xblock-public_view xblock-public_view-video xmodule_display xmodule_VideoBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3addd3d71705" data-block-type="video" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Video"}
</script>
<h3 class="hd hd-2">Video</h3>
<div
id="video_video_3addd3d71705"
class="video closed"
data-metadata='{"transcriptLanguages": {"en": "English"}, "lmsRootURL": "https://openlearninglibrary.mit.edu", "captionDataDir": null, "ytTestTimeout": 1500, "saveStateEnabled": false, "start": 0.0, "autoAdvance": false, "ytApiUrl": "https://www.youtube.com/iframe_api", "ytMetadataEndpoint": "", "recordedYoutubeIsAvailable": true, "speed": null, "generalSpeed": 1.0, "streams": "1.00:Q6XchWzix3Y", "completionEnabled": false, "end": 0.0, "transcriptTranslationUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3addd3d71705/handler/transcript/translation/__lang__", "prioritizeHls": false, "duration": 305.46, "publishCompletionUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3addd3d71705/handler/publish_completion", "completionPercentage": 0.95, "transcriptAvailableTranslationsUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3addd3d71705/handler/transcript/available_translations", "showCaptions": "true", "saveStateUrl": "/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@video+block@video_3addd3d71705/handler/xmodule_handler/save_user_state", "poster": null, "autoplay": false, "transcriptLanguage": "en", "autohideHtml5": false, "savedVideoPosition": 0.0, "sources": ["https://d2f1egay8yehza.cloudfront.net/MIT600512016-V004600_DTH.mp4", "https://d2f1egay8yehza.cloudfront.net/MIT600512016-V004600/MIT600512016-V004600.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_3addd3d71705"></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_3addd3d71705">Downloads and transcripts</h3>
<div class="wrapper-downloads" role="region" aria-labelledby="video-download-transcripts_video_3addd3d71705">
<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_3addd3d71705/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_3addd3d71705/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_ef0618bbff20">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_ef0618bbff20" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="realizing_adt_concepts_in_java">Realizing ADT Concepts in Java</h2>
<div data-outline="realizing_adt_concepts_in_java">
<p>Let's summarize some of the general ideas we've discussed in this reading, which are applicable in general to programming in any language, and their specific realization using Java language features. The point is that there are several ways to do it, and it's important to both understand the big idea, like a creator operation, and different ways to achieve that idea in practice.</p>
<p>The only item in this table that hasn't yet been discussed in this reading is the use of a constant object as a creator operation. This pattern is commonly seen in immutable types, where the simplest or emptiest value of the type is simply a public constant, and producers are used to build up more complex values from it.</p>
<table class="adts-in-java-table">
<tbody>
<tr>
<th>
<p>ADT concept</p>
</th>
<th>
<p>Ways to do it in Java</p>
</th>
<th>
<p>Examples</p>
</th>
</tr>
<tr class="new-row">
<td>
<p>Creator operation</p>
</td>
<td>
<p>Constructor</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#ArrayList--"><code>ArrayList()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static (factory) method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#singletonList-T-"><code>Collections.singletonList()</code></a>, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-"><code>Arrays.toList()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Constant</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#ZERO"><code>BigInteger.ZERO</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Observer operation</p>
</td>
<td>
<p>Instance method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/List.html#get-int-"><code>List.get()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#max-java.util.Collection-"><code>Collections.max()</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Producer operation</p>
</td>
<td>
<p>Instance method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#trim--"><code>String.trim()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-"><code>Collections.unmodifiableList()</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Mutator operation</p>
</td>
<td>
<p>Instance method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/List.html#add-E-"><code>List.add()</code></a></p>
</td>
</tr>
<tr>
<td></td>
<td>
<p>Static method</p>
</td>
<td>
<p><a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#copy-java.util.List-java.util.List-"><code>Collections.copy()</code></a></p>
</td>
</tr>
<tr class="new-row">
<td>
<p>Representation</p>
</td>
<td>
<p><code>private</code> fields</p>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<h2 id="testing_an_abstract_data_type">Testing an Abstract Data Type</h2>
<div data-outline="testing_an_abstract_data_type">
<p>We build a test suite for an abstract data type by creating tests for each of its operations. These tests inevitably interact with each other. The only way to test creators, producers, and mutators is by calling observers on the objects that result, and likewise, the only way to test observers is by creating objects for them to observe.</p>
<p>Here's how we might partition the input spaces of the four operations in our <code>MyString</code> type:</p><pre><code class="language-java hljs"><span class="hljs-comment">// testing strategy for each operation of MyString:</span>
<span class="hljs-comment">//</span>
<span class="hljs-comment">// valueOf():</span>
<span class="hljs-comment">// true, false</span>
<span class="hljs-comment">// length(): </span>
<span class="hljs-comment">// string len = 0, 1, n</span>
<span class="hljs-comment">// string = produced by valueOf(), produced by substring()</span>
<span class="hljs-comment">// charAt(): </span>
<span class="hljs-comment">// string len = 1, n</span>
<span class="hljs-comment">// i = 0, middle, len-1</span>
<span class="hljs-comment">// string = produced by valueOf(), produced by substring()</span>
<span class="hljs-comment">// substring():</span>
<span class="hljs-comment">// string len = 0, 1, n</span>
<span class="hljs-comment">// start = 0, middle, len</span>
<span class="hljs-comment">// end = 0, middle, len</span>
<span class="hljs-comment">// end-start = 0, n</span>
<span class="hljs-comment">// string = produced by valueOf(), produced by substring()</span></code></pre>
<p>Then a compact test suite that covers all these partitions might look like:</p><pre><code class="language-java hljs"><span class="hljs-annotation">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testValueOfTrue</span><span class="hljs-params">()</span> </span>{
MyString s = MyString.valueOf(<span class="hljs-keyword">true</span>);
assertEquals(<span class="hljs-number">4</span>, s.length());
assertEquals(<span class="hljs-string">'t'</span>, s.charAt(<span class="hljs-number">0</span>));
assertEquals(<span class="hljs-string">'r'</span>, s.charAt(<span class="hljs-number">1</span>));
assertEquals(<span class="hljs-string">'u'</span>, s.charAt(<span class="hljs-number">2</span>));
assertEquals(<span class="hljs-string">'e'</span>, s.charAt(<span class="hljs-number">3</span>));
}
<span class="hljs-annotation">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testValueOfFalse</span><span class="hljs-params">()</span> </span>{
MyString s = MyString.valueOf(<span class="hljs-keyword">false</span>);
assertEquals(<span class="hljs-number">5</span>, s.length());
assertEquals(<span class="hljs-string">'f'</span>, s.charAt(<span class="hljs-number">0</span>));
assertEquals(<span class="hljs-string">'a'</span>, s.charAt(<span class="hljs-number">1</span>));
assertEquals(<span class="hljs-string">'l'</span>, s.charAt(<span class="hljs-number">2</span>));
assertEquals(<span class="hljs-string">'s'</span>, s.charAt(<span class="hljs-number">3</span>));
assertEquals(<span class="hljs-string">'e'</span>, s.charAt(<span class="hljs-number">4</span>));
}
<span class="hljs-annotation">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testEndSubstring</span><span class="hljs-params">()</span> </span>{
MyString s = MyString.valueOf(<span class="hljs-keyword">true</span>).substring(<span class="hljs-number">2</span>, <span class="hljs-number">4</span>);
assertEquals(<span class="hljs-number">2</span>, s.length());
assertEquals(<span class="hljs-string">'u'</span>, s.charAt(<span class="hljs-number">0</span>));
assertEquals(<span class="hljs-string">'e'</span>, s.charAt(<span class="hljs-number">1</span>));
}
<span class="hljs-annotation">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testMiddleSubstring</span><span class="hljs-params">()</span> </span>{
MyString s = MyString.valueOf(<span class="hljs-keyword">false</span>).substring(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>);
assertEquals(<span class="hljs-number">1</span>, s.length());
assertEquals(<span class="hljs-string">'a'</span>, s.charAt(<span class="hljs-number">0</span>));
}
<span class="hljs-annotation">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSubstringIsWholeString</span><span class="hljs-params">()</span> </span>{
MyString s = MyString.valueOf(<span class="hljs-keyword">false</span>).substring(<span class="hljs-number">0</span>, <span class="hljs-number">5</span>);
assertEquals(<span class="hljs-number">5</span>, s.length());
assertEquals(<span class="hljs-string">'f'</span>, s.charAt(<span class="hljs-number">0</span>));
assertEquals(<span class="hljs-string">'a'</span>, s.charAt(<span class="hljs-number">1</span>));
assertEquals(<span class="hljs-string">'l'</span>, s.charAt(<span class="hljs-number">2</span>));
assertEquals(<span class="hljs-string">'s'</span>, s.charAt(<span class="hljs-number">3</span>));
assertEquals(<span class="hljs-string">'e'</span>, s.charAt(<span class="hljs-number">4</span>));
}
<span class="hljs-annotation">@Test</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testSubstringOfEmptySubstring</span><span class="hljs-params">()</span> </span>{
MyString s = MyString.valueOf(<span class="hljs-keyword">false</span>).substring(<span class="hljs-number">1</span>, <span class="hljs-number">1</span>).substring(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
assertEquals(<span class="hljs-number">0</span>, s.length());
}</code></pre>
<p>Notice that each test case typically calls a few operations that <em>make</em> or <em>modify</em> objects of the type (creators, producers, mutators) and some operations that <em>inspect</em> objects of the type (observers). As a result, each test case covers parts of several operations.</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical_Questions_405465fe3de5" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Questions</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_fff67ba8819a" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<script src="/assets/courseware/v1/3e6443fb95285d2dc06e8ffc448b9bdf/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism.js"></script>
<script src="/assets/courseware/v1/2ba76f8bdfef4e2d45e31a3dc5208470/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/edx-script-v1.js"></script>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-testing-an-adt-41">
<div class="xblock xblock-public_view xblock-public_view-problem xmodule_display xmodule_ProblemBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-testing-an-adt-41" data-block-type="problem" data-graded="True" data-has-score="True" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "Problem"}
</script>
<div id="problem_09-testing-an-adt-41" class="problems-wrapper" role="group"
aria-labelledby="09-testing-an-adt-41-problem-title"
data-problem-id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-testing-an-adt-41" data-url="/courses/course-v1:MITx+6.005.1x+3T2016/xblock/block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-testing-an-adt-41/handler/xmodule_handler"
data-problem-score="0"
data-problem-total-possible="4"
data-attempts-used="0"
data-content="
<h3 class="hd hd-3 problem-header" id="09-testing-an-adt-41-problem-title" aria-describedby="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-testing-an-adt-41-problem-progress" tabindex="-1">
testing an ADT
</h3>
<div class="problem-progress" id="block-v1:MITx+6.005.1x+3T2016+type@problem+block@09-testing-an-adt-41-problem-progress"></div>
<div class="problem">
<div>
<p>Consider the following datatype:</p>
<pre class="code">
/** Immutable datatype representing a student's progress through school. */
class Student {
/** make a freshman */
public Student() { ... }
/** @return a student promoted to the next year, i.e.
freshman returns a sophomore,
sophomore returns a junior,
junior returns a senior,
senior returns an alum,
alum stays an alum and can't be promoted further. */
public Student promote() { ... }
/** @return number of years of school completed, i.e.
0 for a freshman, 4 for an alum */
public int getYears() { ... }
}
</pre>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 1" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_09-testing-an-adt-41_2_1" id="label_09-testing-an-adt-41_2_1">How many parts are there in a reasonable input-space partition of the Student() constructor?</label>
<select name="input_09-testing-an-adt-41_2_1" id="input_09-testing-an-adt-41_2_1" aria-describedby="status_09-testing-an-adt-41_2_1">
<option value="option_09-testing-an-adt-41_2_1_dummy_default">Select an option</option>
<option value="0"> 0</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-testing-an-adt-41_2_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-testing-an-adt-41_2_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-testing-an-adt-41_solution_1"/>
</div><div class="wrapper-problem-response" tabindex="-1" aria-label="Question 2" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_09-testing-an-adt-41_3_1" id="label_09-testing-an-adt-41_3_1">How many parts are there in a reasonable, but not exhaustive, input-space partition of promote()?</label>
<select name="input_09-testing-an-adt-41_3_1" id="input_09-testing-an-adt-41_3_1" aria-describedby="status_09-testing-an-adt-41_3_1">
<option value="option_09-testing-an-adt-41_3_1_dummy_default">Select an option</option>
<option value="0"> 0</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-testing-an-adt-41_3_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-testing-an-adt-41_3_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-testing-an-adt-41_solution_2"/>
</div><div class="wrapper-problem-response" tabindex="-1" aria-label="Question 3" role="group"><div class="inputtype option-input ">
<label class="problem-group-label" for="input_09-testing-an-adt-41_4_1" id="label_09-testing-an-adt-41_4_1">How many parts are there in a reasonable, but not exhaustive, input-space partition of getYears()?</label>
<select name="input_09-testing-an-adt-41_4_1" id="input_09-testing-an-adt-41_4_1" aria-describedby="status_09-testing-an-adt-41_4_1">
<option value="option_09-testing-an-adt-41_4_1_dummy_default">Select an option</option>
<option value="0"> 0</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3</option>
<option value="4"> 4</option>
</select>
<div class="indicator-container">
<span class="status unanswered" id="status_09-testing-an-adt-41_4_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
<p class="answer" id="answer_09-testing-an-adt-41_4_1"/>
</div></div>
<div class="solution-span">
<span id="solution_09-testing-an-adt-41_solution_3"/>
</div><p>Each of the following method call sequences is a single test case. (Note that we omit the detail about which object each method is called on.) Which of these test cases would cover all the partitions above? Check all that apply.</p>
<div class="wrapper-problem-response" tabindex="-1" aria-label="Question 4" role="group"><div class="choicegroup capa_inputtype" id="inputtype_09-testing-an-adt-41_5_1">
<fieldset aria-describedby="status_09-testing-an-adt-41_5_1">
<div class="field">
<input type="checkbox" name="input_09-testing-an-adt-41_5_1[]" id="input_09-testing-an-adt-41_5_1_choice_0" class="field-input input-checkbox" value="choice_0"/><label id="09-testing-an-adt-41_5_1-choice_0-label" for="input_09-testing-an-adt-41_5_1_choice_0" class="response-label field-label label-inline" aria-describedby="status_09-testing-an-adt-41_5_1"> promote(), promote(), promote(), promote()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-testing-an-adt-41_5_1[]" id="input_09-testing-an-adt-41_5_1_choice_1" class="field-input input-checkbox" value="choice_1"/><label id="09-testing-an-adt-41_5_1-choice_1-label" for="input_09-testing-an-adt-41_5_1_choice_1" class="response-label field-label label-inline" aria-describedby="status_09-testing-an-adt-41_5_1"> Student(), promote(), promote(), promote(), promote(), getYears()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-testing-an-adt-41_5_1[]" id="input_09-testing-an-adt-41_5_1_choice_2" class="field-input input-checkbox" value="choice_2"/><label id="09-testing-an-adt-41_5_1-choice_2-label" for="input_09-testing-an-adt-41_5_1_choice_2" class="response-label field-label label-inline" aria-describedby="status_09-testing-an-adt-41_5_1"> Student(), promote(), getYears(), promote(), getYears(), promote(), getYears(), promote(), getYears()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-testing-an-adt-41_5_1[]" id="input_09-testing-an-adt-41_5_1_choice_3" class="field-input input-checkbox" value="choice_3"/><label id="09-testing-an-adt-41_5_1-choice_3-label" for="input_09-testing-an-adt-41_5_1_choice_3" class="response-label field-label label-inline" aria-describedby="status_09-testing-an-adt-41_5_1"> Student(), getYears(), promote(), getYears(), promote(), promote(), promote(), promote(), getYears()
</label>
</div>
<div class="field">
<input type="checkbox" name="input_09-testing-an-adt-41_5_1[]" id="input_09-testing-an-adt-41_5_1_choice_4" class="field-input input-checkbox" value="choice_4"/><label id="09-testing-an-adt-41_5_1-choice_4-label" for="input_09-testing-an-adt-41_5_1_choice_4" class="response-label field-label label-inline" aria-describedby="status_09-testing-an-adt-41_5_1"> Student(), getYears(), promote(), getYears(), promote(), getYears(), promote(), getYears(), promote(), getYears(), promote(), getYears()
</label>
</div>
<span id="answer_09-testing-an-adt-41_5_1"/>
</fieldset>
<div class="indicator-container">
<span class="status unanswered" id="status_09-testing-an-adt-41_5_1" data-tooltip="Not yet answered.">
<span class="sr">unanswered</span><span class="status-icon" aria-hidden="true"/>
</span>
</div>
</div></div>
<div class="solution-span">
<span id="solution_09-testing-an-adt-41_solution_4"/>
</div></div>
<div class="action">
<input type="hidden" name="problem_id" value="testing an ADT" />
<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_09-testing-an-adt-41" >
<span class="submit-label">Submit</span>
</button>
<div class="submission-feedback" id="submission_feedback_09-testing-an-adt-41">
<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="09-testing-an-adt-41-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="09-testing-an-adt-41-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="09-testing-an-adt-41-problem-title">Answers are displayed within the problem
</span>
<div class="notification-btn-wrapper">
<button type="button" class="btn btn-default btn-small notification-btn review-btn sr">Review</button>
</div>
</div>
</div>
"
data-graded="True">
<p class="loading-spinner">
<i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i>
<span class="sr">Loading…</span>
</p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@Reading_9_Summary" data-block-type="vertical" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="VerticalStudentView">
<h2 class="hd hd-2 unit-title">Reading 9 Summary</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_505e0cbab4f9">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-runtime-version="1" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_505e0cbab4f9" data-block-type="html" data-graded="True" data-has-score="False" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-runtime-class="LmsRuntime" data-request-token="307ce7e04c3911ef89500e6e8f39bb05" data-init="XBlockToXModuleShim">
<script type="json/xblock-args" class="xblock-json-init-args">
{"xmodule-type": "HTMLModule"}
</script>
<link href="/assets/courseware/v1/9fab367942e94f98d32835b3ef4df11d/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/prism-edx-v1.css" rel="stylesheet" type="text/css" />
<h2 id="summary">Summary</h2>
<div data-outline="summary">
<ul>
<li>Abstract data types are characterized by their operations.</li>
<li>Operations can be classified into creators, producers, observers, and mutators.</li>
<li>An ADT's specification is its set of operations and their specs.</li>
<li>A good ADT is simple, coherent, adequate, and representation-independent.</li>
<li>An ADT is tested by generating tests for each of its operations, but using the creators, producers, mutators, and observers together in the same tests.</li>
</ul>
<p>These ideas connect to our three key properties of good software as follows:</p>
<ul>
<li>
<p><strong>Safe from bugs.</strong> A good ADT offers a well-defined contract for a data type, so that clients know what to expect from the data type, and implementors have well-defined freedom to vary.</p>
</li>
<li>
<p><strong>Easy to understand.</strong> A good ADT hides its implementation behind a set of simple operations, so that programmers using the ADT only need to understand the operations, not the details of the implementation.</p>
</li>
<li>
<p><strong>Ready for change.</strong> Representation independence allows the implementation of an abstract data type to change without requiring changes from its clients.</p>
</li>
</ul>
</div>
<div class="license">This reading was collaboratively authored with contributions from: Saman Amarasinghe, Adam Chlipala, Srini Devadas, Michael Ernst, Max Goldman, John Guttag, Daniel Jackson, Rob Miller, Martin Rinard, and Armando Solar-Lezama. This work is licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.</div>
</div>
</div>
</div>
</div>