Lab 0:  Getting started with Java and Eclipse 

CSCI 151
Spring, 2018
Due:  11:59 pm, Sunday February 11, 2018

Assignment Description

This lab is intended to be a bit of a review of the things that you've done in CS 150 and to introduce you to some new tools.  At the end of this lab you should be able to:

  1. Get yourself into the labs after hours without climbing through the windows,
  2. Log in to the lab machines,
  3. Use eclipse and some of its bells n' whistles,
  4. Remember some of the stuff you learned in 150.

NOTE: This lab exists outside of the usual late deadlines.  If you run into problems you can get help with the rest of it during the lab times next week without expending one of your late days.


Part 1 - Getting started

Lab access, computer accounts

If you weren't registered for a CS class at the start of the semester, you will need to go and see Jackie Fortino in King 223 about getting building access for the semester.  You should do this now, before you forget!

If you don't have a CS account, see Chris Mohler in the OCTET office (Mudd 052) about getting one.

Go to one of the CS computer labs and log in to a computer.  The labs are in King 201 and King 135. You should have gotten the door code from your CS professor on the first day of class. For the most part we will use room 201 unless the class becomes too large.

Create a directory for this course and lab

Once you have logged in, make yourself a directory to put your class work in.  Using a Terminal application:

% cd
% mkdir cs151
% cd cs151
% mkdir lab0
% cd lab0

You should do this for each lab, not only because it organizes your files, but also because it causes less problems with handin. And you don't want any problems with handin.


Part 2 - Eclipse

Now you should start eclipse.  Eclipse is a Java IDE (Integrated Development Environment) that is used world-wide.  It has a number of very useful features including automatic compilation, error detection, built-in debugger, autocomplete, and many other features.

Eclipse is also available for free for your home machine, should you want some alone-time with it.  Just go to the eclipse website for download.

Start eclipse

Just run it from the command line.

% eclipse &

Alternatively, choose it from the graphical Development menu.

When eclipse starts up, it will ask you for the name of a workspace.  You should just accept the default for  workspace (usually ~/eclipse-workspace/).  Don't select your csci151 folder as your default workspace.   It'll take a little time to start up.

Do tutorial

From the Eclipse Welcome page, click Tutorials > Create a Hello World Application.  Follow the instructions to make your very first eclipse Java program.  Enjoy.

Customize Eclipse preferences

Until you get more familiar with eclipse and figure things out on your own, please make the following changes to your preferences to make life easier.  Start by selecting the menu Window  >  Preferences (On a Mac, it is located in Eclipse  >  Preferences.

Set preferences

First, let's deal with auto-completion and cursors.   Click on the triangles in front of Java, Editor and click on Typing.

Notice that Eclipse will automatically insert closing braces, quotes, and the like by default.  I recommend that you keep these enabled.  It will take a little getting used to, but prevents annoying little bugs.

The second section "automatically insert at correct position" has things that I really like to enable.  When you type either ; or a brace, it'll jump to the right spot for it most of the time.  It's pretty nifty, try it out!

Fixing output to be more sane

By default, Eclipse uses tabs for indentation. However, it displays them as being only 4 spaces, when most other applications will display them as 8.  A minor detail, but changing this will make things much more readable outside of Eclipse.  The easiest way is to go to Java > Code Style > Formatter and then select Java conventions as the Active profile. If you change only one setting, make it this one!

Code Style settings
          screen

You can also get Eclipse to reformat your code when you are working on it in a number of ways. You can highlight a section of text and then hit "ctrl-I" or use the menu "Source", "Correct Indentation".  You can also use the "Source", "Format" command to make similar changes.  You can also have it automatically reformat your code each time you save.  Go to Java > Editor >Save Actions and check the boxes marked "Perform the selected actions on save" and "Format source code".  You can have it just reformat the sections of code you have been working on, or you can have it take care of the entire file.

Javadoc

If you take advantage of the auto-Javadoc comments, you'll probably want to also change the pattern under Java > Code style > Code Templates > Comments > Overriding methods to match the one for Methods.  (Just copy the Pattern window contents from "Methods" to "Overriding methods".)

Change the default build path

Eclipse likes to put the *.class files in a different folder than your *.java files.  However, this makes it a bit harder to go between Eclipse and a normal editor.  Change the preferences by selecting Java > Build Path and change the value of the Source and output folder radio button to be Project instead of Folders.  Do not change the value of Source folder from src or Output folder from bin -- change the radio button only.  If you change only two settings, do this one too!

Build Path settings
          screen

Do some editing

While you are working in eclipse, you'll probably find that there are some functions you might want to use via the pull down menus.  Note that the keyboard shortcuts are listed on the right hand side.  Learning to use them will speed up your work.

Here's a list of a few such features and shortcuts:


Part 3 - Writing a Java program in Eclipse

Create a project

Now you should create a project in your lab0 folder as you'll be doing in many of the labs.

  1. Select File > New > Java Project
  2. Call it "Hello 151" (or "Lab0" or whatever you like)
  3. Uncheck the box for use default location
  4. Use the Browse button to select the lab0 folder you created earlier
  5. Now just click on Finish

If you want, you can close the Task List and Outline panes on the right side.  You won't be using them for this lab.

Now, write a program that will tell the user a little bit about yourself.  In eclipse, you start by creating a new class, as follows:

  1. Select File > New > Class

  2. Don't change the project or source folder

  3. Leave Package blank

  4. Set the Name of class to be Hello151

  5. Check the box that will create a main method stub

  6. Now select Finish

You should now see Hello151.java in the Package Explorer window.  If you do an ls on the command line in your lab0 folder, you'll see that the files Hello151.java and Hello151.class are now in this folder. Eclipse compiles as you work!

Comments!

The Hello151.java file should be open in the editor.  If not, just double click on it in the Package Explorer window.  Let's add some comments to the top of the file.

  1. Go to the top of the file and start a Javadoc comment.  They begin with /**
  2. Put in a short description of what the program is supposed to do.  In this case, you are going to introduce yourself to the graders.
  3. On another line, but still in the comment block, start a line with @author followed by your name
  4. Close the comment block with a */. It is likely that eclipse has already done this for you.

Run your program

Just in case you haven't actually tried to run your program yet, now would be a great time to check that it works.  Here's a line you can add to the main method that will give you some output

    System.out.println("I love Java.");

You can run it in Eclipse by just selecting Run > Run from the pull-down menus.  Or right-click and select Run As > Java Application. You should see the program output in the Console panel at the bottom of the Eclipse screen.

You can also run your program outside of Eclipse, by running it from a command line, as follows:

  1. Go to a Terminal window
  2. Type "cd ~/cs151/lab0" (this takes you to your lab0 folder -- '~' is a shortcut for your home folder)
  3. Run the program by typing java Hello151

If that didn't work, and you see that you have bin and src folders in your cs151/lab0 folder, you missed one of the important steps above.

Note:The graders will be running your program from the command line, so it's important that you run your program this way before submitting it.

Share with the graders

Now let's add some print statements to introduce yourself to the graders.  Inside the main() method, add in a number of System.out.println messages that will share:

  1. Your name as you want to be called
  2. Your CS username
  3. Your year/major (if known)
  4. Something you think is awesome about Oberlin

Get on with your life

If you've made it this far, and actually changed preferences and done the steps I listed above, that's enough Java programming for this week.


Part 4 - Lab tools

Running Java

For this class, we'll be exercising more than the usual amount of memory in Java programs.  You will become familiar with the -X family of arguments to use with Java.

% java -Xmx1g MyProg     # use up to 1 gigabyte of memory
% java -Xmx500m MyProg   # use up to 500 megabytes of memory

You probably won't need to do this on the lab machines, but you might on your own laptop.

Snapshots

The CS server will be making periodic backups of your files for you.  You can access these backups in the folder

~/.zfs/snapshot/

This is an exact copy of your files at the time the snapshot was made.  It'll be useful if you accidentally delete something in lab.  Currently only the daily snapshots are working (taken at midnight each night), but hopefully we'll have some that are more frequent as the semester progresses.

handin / lshand

handin is the right way to submit your work in this class.  If you talk to other students, they might tell you about a web based version of handin. Please don't use this as it is unsupported and has resulted in more than one student submission being discarded.

Also, always use lshand to verify that your submission was received.  If it does not show you your latest submission, then we don't have it.

Should handin be broken at the deadline, email us ASAP and stop editing stuff in your lab directory (or email the files in lieu of a submission).


Part 5 - Last things

Be sure to get grade reports

You should also add a rule into your OCMail so that mail from the grader doesn't go into your Spam folder.  (This seems to happen randomly throughout the semester.)

  1. Go to http://ocmail.oberlin.edu/ and log in to your Oberlin email.
  2. Do a search for from:gr151@cs.oberlin.edu
  3. After you've searched, on the right side of the search box, you should hit the gray downward facing triangle, and then select "Create filter with this search" in the bottom right.
  4. Check the box for the line "Never send it to Spam"
  5. Click on "Create Filter" in the blue button below, and you should be all set.

handin

Look through your source files and be sure you've included you name at the top of all of them.  If there are any known problems or incomplete features, document that also at the top of the file.  If you adhered to the honor code in this assignment, add the following statement as a comment at the top of your main java file.

I have adhered to the Honor Code in this assignment.

handin

You now just need to electronically handin all your files.  Quit Eclipse and delete the class files.  (Otherwise those will be handed in as well!)

% cd          # changes to your home directory
% cd cs151    # goes to your cs151 folder
% handin      # starts the handin program
                        # class is 151
                        # assignment is 0
                        # file/directory is lab0

% lshand      # should show that you've handed in something

You can also specify the options to handin from the command line

% cd ~/cs151                 # goes to your cs151 folder
% handin -c 151 -a 0 lab0

That's it!  You've completed your zeroeth lab.  Be sure to logout when you are done (icon is to the left of the terminal and globe icons).

Checklist

Be sure that you have

  1. Accepted the default workspace
  2. Changed settings to use "Java Conventions"
  3. Changed the settings for "Build Path"
  4. Added the filter for your OCMail account

Last Modified: January 2018 John Donaldson, based on material from Bob Geitz, Robert Hoyle, Benjamin Kuperman and Alexa Sharp