<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-ps1-beta" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-graded="True" data-runtime-version="1" data-init="VerticalStudentView" data-request-token="86a627ae022d11efa4f40afff417eba9">
<h2 class="hd hd-2 unit-title">Problem Set 1</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_31dcf7c08a0c">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_31dcf7c08a0c" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-graded="True" data-runtime-version="1" data-init="XBlockToXModuleShim" data-request-token="86a627ae022d11efa4f40afff417eba9">
<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" />
<h1 class="handout-title col-sm-8 col-sm-offset-2">About the Problem Sets</h1>
<div>
<p>
Every problem set has two submission points: a beta submission and a final submission. The initial release of the problem set (e.g., this page about Problem Set 1) includes tests for the beta submission. After the beta deadline passes, additional tests will be released for you to download from edX. You can then run these final tests against your solution, fix any new problems that are found, and then submit your revised program for the final deadline.
</p>
<p>
For the problem sets, all the test cases are given to you, and you'll run the tests on your own machine before uploading your problem set, so you'll always know which tests are passing and which are failing before you submit. There are no hidden tests for the problem sets. You'll get checked off for the beta submission if it passes most of the beta tests (typically at least 80-85%), and you'll get checked off for the final submission if it passes most of the beta + final tests.
</p>
</div>
</div>
</div>
<div class="vert vert-1" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_d60ee6b38772">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_d60ee6b38772" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-graded="True" data-runtime-version="1" data-init="XBlockToXModuleShim" data-request-token="86a627ae022d11efa4f40afff417eba9">
<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" />
<h1 class="handout-title col-sm-8 col-sm-offset-2" id="problem_set_0">Problem Set 1: Getting Started</h1>
<h2 id="config-eclipse">Overview</h2>
<div>
The purpose of this problem set is to:
<ul>
<li>introduce the tools we use in 6.005x, including Java, Eclipse, and JUnit;</li>
<li>introduce the process for 6.005x problem sets;</li>
<li>and practice reading a function specification and writing basic Java to implement it.</li>
</ul>
<p>
You should focus in this problem set on writing code that is safe from bugs and easy to understand, using the techniques we discuss in class. You won't be asked to write any test cases of your own on this problem set. Testing will happen in Problem Set 2.
</p>
<h2 id="config-eclipse">Configure Eclipse</h2>
<div data-outline="config-eclipse">
<p>Eclipse is a powerful, flexible, and occasionally frustrating set of tools for developing and debugging programs, especially in Java.</p>
<blockquote><em style="font-size: 70%">Note: if you prefer, you can do the problem sets in another Java development environment, or using command-line tools. The problem sets require compiling Java, running JUnit, and running Ant, so if you know how to do those things some other way, go for it. The rest of these instructions will talk only about using Eclipse.</em></blockquote>
<p>You should have already installed Eclipse and Java <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-install-java-tutor">when you set up the Java Tutor</a>.</p>
<p>When you run Eclipse, you will be prompted for a "workspace" directory, where Eclipse will store its configuration and metadata. The default location is a directory called <code>workspace</code> in your home directory. You should not run more than one copy of Eclipse at the same time with the same workspace.</p>
<p>On the left side of your Eclipse window is the Package Explorer, which shows you all the projects in your workspace.</p>
<ol>
<li>
<p>Open Eclipse preferences.</p>
<p><strong>Windows & Linux</strong>: go to <em>Window → Preferences</em>.</p>
<p><strong>OS X</strong>: go to <em>Eclipse → Preferences</em>.</p>
</li>
<li>
<p>Make sure Eclipse is configured to use <strong>Java 8</strong>.</p>
<ol style="list-style-type: lower-alpha;">
<li>
<p>In preferences, go to <em>Java → Installed JREs</em>. Ensure that "Java SE 8" or "JDK 1.8.0_71" is the only one checked. If it’s not listed, click Search.</p>
</li>
<li>
<p>Go to <em>Java → Compiler</em> and set "Compiler compliance level" to 1.8. Click OK and Yes on any prompts.</p>
</li>
</ol>
</li>
<li>
<p>Make sure <strong>assertions are always on</strong>. Assertions are a great tool for keeping your code safe from bugs, but Java has them off by default.</p>
<p>In preferences, go to <em>Java → Installed JREs</em>. Click "Java SE 8", click "Edit…", and in the "Default VM arguments" box enter: <code>-ea</code> (which stands for <em>enable assertions</em>).</p>
</li>
<li>
<p>Make sure your <strong>Eclipse workspace refreshes automatically</strong> whenever files are changed on the filesystem. This will be important when you run the grader script, so that you see the output files it generates.</p>
<p>In preferences, go to <em>General → Workspace</em>. Turn on both checkboxes that talk about refresh: "Refresh using native hooks or polling" and "Refresh on access."</p>
</li>
<li>
<p><strong>Tabs</strong>. If you configure the editor to use spaces instead of tabs, then your code will look the same in all editors regardless of how that editor displays tab characters.</p>
<p>In preferences, go to <em>Java → Code Style → Formatter</em>. Click the "Edit…" button next to the active profile. In the new window, change the Tab policy to "Spaces only." Keep the Indentation size and Tab size at 4. To save your changes, enter a new "Profile name" at the top of the window and click OK.</p>
</li>
</ol>
<div class="handout-solo alert alert-warning">
<p>The <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-eclipse-faq" class="alert-link">6.005 Eclipse FAQ</a> has some tips and tricks to help you make the most of Eclipse.</p>
</div>
</div>
<h2 id="import">Download and Import the Problem Set Code</h2>
<div data-outline="import">
<ol>
<li>
<p>Download the starting code for this problem set:</p>
<blockquote>
<a href="/assets/courseware/v1/8889871bd2567e771e4f8537adc073d8/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/ps1.zip">ps1.zip</a>
</blockquote>
</li>
<li>
<p>Import the code into Eclipse:</p>
<ol style="list-style-type: lower-alpha;">
<li>In the Eclipse menubar, choose File → Import...</li>
<li>In the Select dialog, open General and choose Existing Projects Into Workspace.</li>
<li>In the Import Projects dialog, choose Select archive file, then click Browse and use the
file dialog to find where you downloaded ps1.zip.</li>
<li>Still in the Import Projects dialog, make sure ps1-warmup is listed in the Projects, and that the checkbox next to it is checked</li>
<li>On "Import projects," make sure ps1-warmup is checked.
<blockquote>
If ps1-warmup is grayed and uncheckable, then the likely reason is that ps1-warmup already exists in your Package Explorer (perhaps because you are following these directions a second time). You need to cancel the import, delete or rename ps1-warmup in your Package Explorer, and try the import again.
</blockquote>
</li>
<li>Click Finish. ps1-warmup should now appear in your Package Explorer.</li>
</ol>
</li>
</ol>
</div>
<h2 id="warmup">Warm up with <code>Quadratic.roots()</code></h2>
<div data-outline="warmup">
<p>In the Eclipse Package Explorer, open up <code>ps1-warmup / src / ps1</code>, and you should find the file <code>Quadratic.java</code>. Your warm-up task is to implement the <code>roots()</code> method in this class, which finds the roots of a quadratic equation. The method's specification comment describes how it should behave. Pay close attention to this specification while you're implementing. The <a href="https://en.wikipedia.org/wiki/Quadratic_formula">quadratic formula</a> will be useful in your solution, but be careful! Many things can go wrong.</p>
<p><strong>Before you start writing code</strong>, read the rest of this handout, because it describes three ways that you can use to check your implementation: (1) a <code>main</code> method, (2) a JUnit test suite, and (3) an autograder. Read about and try running each of these first, using the unfinished <code>roots()</code> implementation that we provided you, and then come back and start working on your implementation.</a>
</div>
<h2 id="warmup">Running <code>main()</code></h2>
<div data-outline="running_main">
<p>The <code>Quadratic</code> class also contains a <code>main</code> method. The method <code>public static void main(String[] args)</code> is the standard entry point for a Java program. In this case, the <code>main</code> method calls <code>roots()</code> on a simple quadratic equation.</p>
<p>To run <code>main</code>, right-click on <code>Quadratic.java</code> in the Package Explorer, and choose <em>Run As → Java Application</em>. You will see the results printed in the Console tab at the bottom of the Eclipse window. It will say "not implemented yet" if you haven't started writing <code>roots()</code></p>
</div>
<h2 id="unit_testing">Run JUnit Tests</h2>
<div data-outline="unit_testing">
<p>A <code>main</code> method is not a great way to test a program. Much better is <a href="/courses/course-v1:MITx+6.005.1x+3T2016/jump_to_id/vertical-automated-testing-and-regression-testing">automated unit testing</a>, which runs a suite of tests to automatically test whether the implementations are correct.</p>
<p><a href="http://www.junit.org/">JUnit</a> is a widely-adopted Java unit testing library, and we will use it heavily in 6.005.</p>
<p>To find the tests for Quadratic, open up <code>ps1-warmup/<strong>test</strong>/ps1</code> in the Package Explorer, where you should find the file <code>QuadraticTest.java</code>. By convention, JUnit tests are put in a class whose name ends in Test, and 6.005 goes a step further by putting them in the <code>test/</code> folder rather than the <code>src/</code> folder.</p>
<p>To run the tests, right click on <code>QuadraticTest.java</code> in the Package Explorer, and choose <em>Run As → JUnit Test</em>. You should see the JUnit view appear.</p>
<p>If your implementation of <code>roots()</code> is correct, you should see a green bar, indicating that all the tests passed.</p>
<p>If your implementation is still broken or unfinished, you should see a red bar, and a list of the tests that were run. Clicking on a test shows a <em>stack trace</em> in the bottom box, which provides a brief explanation of what went wrong. Double-clicking on a line in the stack trace goes to the code for that frame in the trace. This is most useful for lines that correspond to your code; this stack trace will also contain lines for Java libraries or JUnit itself.</p>
<p><strong>Use these tests to help you implement roots().</strong></p>
</div>
<h2 id="autograder">Run the Autograder</h2>
<div data-outline="autograder">
<p>Once your implementation is passing all the JUnit tests, you need to run the automatic grader. The autograder is the file <code>grader.xml</code> in your project. It is written as an Ant script, where <a href="http://ant.apache.org/">Ant</a> is an example of a <em>build management tool</em>, a kind of tool commonly used in software development to compile code, run tests, and package up code for deployment. Our autograder script does all three of these things. Support for running Ant scripts is built into Eclipse.</p>
<p>To run the automatic grader, right click on <code>grader.xml</code> in the Package Explorer, and choose <em>Run As → Ant Build</em>. Choose the plain <em>Ant Build</em> rather than <em>Ant Build...</em>, because you don't need all the options that the Ant Build... dialog box will offer you.</p>
<p>The grading script will run and display output in the Console tab. At the end, it should tell you that it has created two new files: <code>my-grader-report.xml</code>, which is the result of running the grading tests, and <code>my-submission.zip</code>, which is your problem set code and grader output packaged up and ready for submission to edX.</p>
<blockquote><em style="font-size: 70%">Note: if automatic workspace refreshing is not working for some reason, then <code>my-grader-report.xml</code> and <code>my-submission.zip</code> may not immediately appear in the Eclipse Package Explorer. Try refreshing the project manually by right-clicking on <code>ps1-warmup</code> and choosing <em>Refresh</em>. Then you should see the files appear. If they still aren't there, check the Console tab for errors.</em></blockquote>
<p>To view your autograder results, double-click on <code>my-grader-report.xml</code>, and you will see the results in your JUnit pane. The <code>my-grader-report.xml</code> file is simply the output of running JUnit on the autograder's tests. For now (Problem Set Beta 1), the autograder tests are identical to the tests you've already been using in QuadraticTest.java, so you should see exactly the same tests passing or failing that you see when you ran JUnit yourself as in the previous section. On future problem sets, the autograder will run different tests.</p>
<p>You can change your code and rerun the autograder as often as you want. You have to double-click on <code>my-grader-report.xml</code> each time to see the newest results. You don't need to Refresh the project each time, however. Once <code>my-grader-report.xml</code> is visible in the Package Explorer, Eclipse will load the latest version whenever you click on it.</p>
</div>
<h2 id="submit">Submit the Beta version of your Problem Set</h2>
<div data-outline="submit">
<p>After the autograder runs, it produces <code>my-submission.zip</code> in your Eclipse project. This zip file contains your code and your grading report. To get credit for the problem set, you need to upload this zip file to edX before the deadline.</p>
<p>The Problem Set Beta 1 submission page is the last section of this handout. To find it, click the rightmost button on the section bar at the top of this page:</p>
<p style="text-align: center"><img style="width:50%" src="/assets/courseware/v1/9bc277bfebc84a58bf5ae1eef6779527/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/pset-submission-page.png" alt="problem set submission page"></p>
</div>
</div>
</div>
</div>
</div>
<div class="xblock xblock-public_view xblock-public_view-vertical" data-block-type="vertical" data-runtime-class="LmsRuntime" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@vertical+block@vertical-eclipse-faq" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-graded="True" data-runtime-version="1" data-init="VerticalStudentView" data-request-token="86a627ae022d11efa4f40afff417eba9">
<h2 class="hd hd-2 unit-title">Eclipse FAQ</h2>
<div class="vert-mod">
<div class="vert vert-0" data-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_bb72536d64c5">
<div class="xblock xblock-public_view xblock-public_view-html xmodule_display xmodule_HtmlBlock" data-block-type="html" data-runtime-class="LmsRuntime" data-usage-id="block-v1:MITx+6.005.1x+3T2016+type@html+block@html_bb72536d64c5" data-course-id="course-v1:MITx+6.005.1x+3T2016" data-has-score="False" data-graded="True" data-runtime-version="1" data-init="XBlockToXModuleShim" data-request-token="86a627ae022d11efa4f40afff417eba9">
<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" />
<h1 class="handout-title col-sm-8 col-sm-offset-2" id="eclipse_faq">Eclipse Tips</h1>
<h2 id="resources">Resources</h2>
<div data-outline="resources">
<p><a href="http://www.vogella.com/tutorials/EclipseDebugging/article.html"><strong>Java Debugging with Eclipse</strong></a> introduces Eclipse’s debug mode, a recommended tool for tracking down bugs using breakpoints. This step-by-step tutorial explains the <em>Debug perspective</em> and provides screenshots to decode the mysterious debugger icons in Eclipse.</p>
</div>
<h2 id="tips_tricks">Tips & Tricks</h2>
<div data-outline="tips_tricks">
<h3 id="navigating">Navigating</h3>
<div data-outline="navigating">
<p>Command-shift-R and command-shift-T bring up a dialog for quickly searching for and <strong>navigating to files</strong> and <strong>classes</strong>, respectively.</p>
</div>
<h3 id="editing">Editing</h3>
<div data-outline="editing">
<div class="panel panel-default panel-figure pull-right">
<div class="panel-body"><img width="329" src="/assets/courseware/v1/c540e352a448031989a4e2ab75152450/asset-v1:MITx+6.005.1x+3T2016+type@asset+block/rename.png" alt="Enter new name, press return to refactor"></div>
</div>
<p>Select an element and use <strong><em>Refactor → Rename</em></strong> (or its keyboard shortcut) to rename that variable, field, method, class, etc. wherever it appears.</p>
<p>Take advantage of <strong>code completion</strong> to help you use a new API: if you have an object <code>squiggle</code>, typing the name and then a period will bring up code completion options.</p>
<p>Use <strong>Source → Organize Imports</strong> or command- (or control-) -shift-O to automatically add and organize Java import statements. Just start using a new class, <em>Organize Imports</em>, and you’re good to go.</p>
<p>Use <strong>Source → Toggle Comment</strong> or keyboard shortcut Command - / to quickly toggle on or off some lines of code.</p>
<p>Add any tips you find helpful in your journey with Eclipse to the forum by clicking Show Discussion below and adding a post!</p>
</div>
</div>
</div>
</div>
</div>
</div>