How to... (key techniques for labs and homework)

Installing required software

For completing labs in this course, you will need to install the following two pieces of software on your computer (or if you are on campus, you can use the computers in the Tome building, which already have the necessary software installed):

Install the two tools above before continuing.

Accessing Eclipse remotely

If you are unable to install your own software as described above, a good alternative is to access a computer at Dickinson remotely and run eclipse on the Dickinson computer. This is achieved through the following website:

https://remotelab.dickinson.edu/remotelab/

Choose a computer in the General Pool—these are the ones that have Eclipse on them. When you start up Eclipse, put your workspace inside your OneDrive. That way, the next time you access the General Pool you can get at the same workspace even if you are assigned a different computer.

Obtaining starter code and creating Git repository

Unless stated otherwise, the following instructions apply to each lab:

  1. Copy the starter code link, available from the course "Labs" webpage.
  2. Log into your GitHub account and paste the starter code link in the address bar of the same window you used to log in to GitHub.
  3. You will see a message to accept an assignment. Once you accept the assignment, GitHub will provide you with a link that you can use to access the code repository for the lab.
  4. On the GitHub webpage for the starter code repository, click the green Code button, and copy the web URL for "clone with HTTPS".
  5. Open Eclipse.
  6. Go to Window | Show View | Other | Git | Git Repositories. A Git Repositories view should appear below the Package Explorer.
  7. In the Git Repositories view, click on "Clone a Git Repository and add the clone to this view". In the Window that opens up, paste the URL for the starter code repository (already copied above) into the URI field. Enter your GitHub username and password. Click Next and Finish. A Git clone should appear in the Git Repositories window.
  8. Right click on the Git Repository and select Import Projects | Finish.
  9. A Java project should now appear in the Package Explorer tab in the left part of the Eclipse window.

Committing and pushing your changes to GitHub for backup and grading

In order to push your changes to the GitHub repository from Eclipse, right click on the relevant Java project in the Git Repositories view and click on Commit. Find the newly-appeared "Git Staging" window and click on the double-plus symbol to move all of your changes from "Unstaged Changes" to "Staged Changes". Type a descriptive commit message into the Commit Message box (this can be useful later to remind you of what changes you have made). Finally, click "Commit and Push" followed by "Preview", "Push", "Close". This saves all the changes on your computer and also pushes them to the GitHub servers. This is a good way to back up your progress, and it is also the way you will submit code for grading. The instructor will grade the most recently-committed version.

Pulling graded files from GitHub and sharing with lab partner

You can always view the instructor's grade and comments on your Java source code on the GitHub website. But to share these comments with your lab partner, you will need to pull the instructor's changes back to your own machine. To do this, right click on the relevant Java project in the Git Repositories view and click on Pull. Then email the .java files to your lab partner.

Finding your source files to share with a lab partner

Note that Eclipse stores your .java files within a directory on your computer. In most labs, you will be working with a lab partner. Most often, you will work simultaneously with your partner via screen sharing—so you will be working together on one set of files. However, sometimes you will want to work individually and share your progress with each other. There are many sophisticated ways of collaborating on shared files, but in this course we use the simplest possible approach: it is recommended that you simply email your .java files to your partner (or share the files by any other filesharing technique that you are comfortable with).

Before you can do this, you will need to find your .java files within the local file system on your computer. Within Eclipse, this is achieved by right-clicking on the relevant .java file in Package Explorer, then choose Show In | System Explorer.

Submitting lab report, including self-assessment report, to Moodle

For every lab, the source code will be submitted to a GitHub repository as described above. In addition, however, for every lab you will also be required to submit a lab report to Moodle. Some labs require you to collect and write about some results as part of the lab itself, in which case those results would form part of your lab report. However, whether or not the lab explicitly requires you to report on results, every lab report must contain a self-assessment report. The self-assessment report is a very brief summary of which parts of the lab you successfully completed, together with descriptions of any parts that are incomplete or not working correctly. Typically, the self-assessment report will be between one and five sentences in length. It should take at most 10 minutes to write the report. The main purpose of the self-assessment report is to help the instructor grade your work efficiently and accurately while also creating the best possible opportunity for the instructor to help you with problems you encountered. If you believe you completed and tested all parts of the lab successfully, you can state this in a single sentence. If not, briefly describe the parts that are incomplete or not working correctly. Submit your lab report in PDF, Word or another common format to Moodle.

Creating a new project in Eclipse

  1. Open the File menu and choose New.
  2. Choose Project... from the pop-up menu.
  3. In the New Project dialog, select Java Project and click Next.
  4. Enter the name for your project in the Project Name box.
  5. If, at any stage in this process, you are offered the opportunity to create a module, do not select that option. We do not want a module as part of our project.
  6. Click Finish.

A new project with the name that you specified will appear in the Package Explorer tab in the left part of the Eclipse window.

Opening a Closed project in Eclipse

Closed projects appears as a closed folder in the Package Explorer tab in the left part of the Eclipse window.

  1. Double click on the project's folder in the Package Explorer tab.
  2. Click Yes in the Question dialog asking about opening referenced projects.

Creating a new package in Eclipse

  1. Ensure that the project to which you want to add the package is open. See Opening a project, above.
  2. Right click on the src folder of the project and choose New.
  3. Select Package... from the pop-up menu.
  4. Enter the name of your package in the Name field of the New Java Package dialog.
  5. Click Finish.

A new package with the name that you specified will appear within the project's src folder in the Package Explorer tab in the left part of the Eclipse window.

Creating a new class/interface in Eclipse

  1. Right click on the package to which you want to add the class or interface and choose New.
  2. Select Class or Interface as appropriate.
  3. Enter the name of the new class or interface in the Name field of the dialog.
  4. Click Finish.

A new class (or interface) will appear within the package in the Package Explorer tab in the left part of the Eclipse window. An editor window will also open for the new class (or interface) with a stub implementation to be completed.

Running a Java program (i.e. main) in Eclipse

  1. Right click on the class containing the main method you wish to run.
  2. Select Run as and choose "Java Application" from the pop-up menu.

The program will execute and any output generated will appear in the Console tab at the bottom of the Eclipse window.

NOTE: Pressing Shift-Apple-F11 (Mac) or Ctrl-F11 (Windows) will re-run the last executed program.

Creating a new JUnit Test Case in Eclipse

  1. Right click on the class that you wish to test and choose New.
  2. Select JUnit Test Case from the pop-up menu.
  3. Choose the New JUnit 4 test radio button at the top of the New JUnit Test Case dialog.
  4. Click on Finish.
  5. If this is the first test case added to the project a second dialog box will appear stating Junit 4 is not on the build path. Click OK.

A new class will appear beneath the class being tested in the Package Explorer tab in the left part of the Eclipse window. The name of the new class will be the name of the class being tested with "Test" appended to the end. An editor window will also open for the new test class with a stub implementation to be completed.

Creating a new JUnit test method

  1. Find the editor window for the test class you wish to add to. Bring this window to the front.
  2. Create a stub for your test method as shown here:
    @Test 
    public void testSomething() { 
        fail("not implemented"); 


  3. Change the name of the method from testSomething to be descriptive of what you are testing.
  4. Replace the fail assertion in the body of the test method with your test.

The assertions that can be used in a JUnit test include the following. Note that for the purposes of this course it is acceptable to omit the "message" parameter in the assertions below.

Running JUnit Test Cases in Eclipse

  1. Right click the JUnit Test Case (or package, or project) containing the tests you want to run.
  2. Choose Run As and select JUnit Test from the pop-up menu. All of the test methods in the Test Case (or package, or project) that you selected will be executed. The JUnit tab will become active on the left side of the Eclipse window and will show you your test results.
  3. Click on any failed test to see the message from the failed assertion.
  4. Double click on any failed test to see the test method containing the assertion that failed.

Adding the JUnit4 library to a project in Eclipse

  1. Right click on the project in Package Explorer and choose Properties.
  2. Click on "Java Build Path" and select the Libraries tab.
  3. Click "Add library...", choose JUnit, then Next.
  4. Select JUnit library version "JUnit 4", then Finish, then Apply and Close.

Changing default font of the editor in Eclipse

  1. On Window menu, choose Preferences | General | Appearance | Colors and Fonts
  2. Choose Basic | Text Font | Edit...
  3. Select the font you wish and choose Apply and Close

Importing .java files into an Eclipse project

  1. Right click the project in Package Explorer.
  2. Choose Import | General | File System, hit Next.
  3. Browse to the folder containing your .java file(s) and click the Select Folder button (or it may be labelled "Open" on a Mac) to select this folder.
  4. Put checkmarks by every folder or file that you wish to import.
  5. Make sure the Into Folder setting is correct—usually this should be set to the src subdirectory of your project.
  6. Click Finish.