Lab 0

Welcome back, Earthlings.
Due by 8pm, Sunday 9 Sep 2012

In this lab you will learn a new programming tool that will blow the socks off jEdit, as well as catch up with some old Java friends. 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. Use eclipse and some of its bells n' whistles,
  3. Remember all the stuff you learned in 150.

Part 1 - Getting started

Lab access & computer account

First things first, if you are already registered for the course, you should have building access with your ID card, and the course staff will tell you the code to get into the lab itself. Remember this number so that you can come in and work in the lab at crazy crazy hours and bond with other crazies.

If you are a late regsitrer, or if your ID card does not let you in for whatever reason, you need to go see Jackie Fortino in King 223 between 8:30am and 3:30pm about getting you access.

If you don't have a CS account, see Chris Mohler in the OCTET office (King 125) during regular business hours about getting one. If you're not a late registrer, he should have something ready-to-go. Otherwise it may just take him a few minutes.

For those of you new to Oberlin, and new to the CS accounts, you may be interested in checking out our CS 150's first lab, where some of the account setup is explained. Yes, I would recommend this. Feel free to skip the actual programming component.

Create a directory for this course and lab

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

    % 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 no problems with handin.

Download the starter files

Here are some files to start you out for this lab: lab0.jar.

You can either save this file to your machine and then move it into the lab0 directory, or you can (more easily) just copy the URL and then download the files from the command line using wget.

    % wget http://www.cs.oberlin.edu/~asharp/cs151/labs/lab00/lab0.jar

Now unpack the files using jar.

    % pwd                # be sure you are in ~/cs151/lab0/
    % jar xvf lab0.jar
    % rm lab0.jar        # don't need this anymore

You should now see the file Plyaer.java in your current directory. (Ignore spelling.)


Part 2 - Eclipse

Now, eclipse! For our purposes, Eclipse is a Java IDE (Integrated Development Environment) that is used world-wide. It's like jEdit, BlueJ, NetBeans, DrJava, etc., but bigger. It has a number of very useful features including automatic compilation, error detection, built-in debugging, autocompletion, among others.

The reason we're switching away from jEdit is that the projects in this course are going to be a lot bigger and convoluted than the ones from 150, and some file management help will be necessary. Eclipse will be a useful tool to keep things organized and bug-free. Plus, Eclipse compiles your code as you are writing it allowing you to quickly spot mistakes and it'll offer suggestions on how you can fix them!

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

Start 'er up!

To start eclipse, just run it from the command line.

    % eclipse &

You can accept the default for workspace. It'll take a bit to start up.

Do a tutorial

From the Eclipse Welcome page, click Tutorials > Create a Hello World Application. Follow the instructions to make your very first eclipse Java program. Good times. When you're done with that, go back to the Welcome page, then click Overview > Workbench Basics. Peruse some of these topics to familiarize yourself with some of eclipse's features.

You should also read through this Eclipse Tutorial 3 - Useful Programming Functions and this Eclipse Tutorial 5: Running and Debugging. You'll be glad you did. No, really, do it.

You'll get more time to practice with eclipse momentarily. But first...

Customize your Java digs

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.

Cursor Settings

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

The first section on the Typing page controls whether eclipse automatically inserts closing braces, quotes, and the like by default; I recommend you keep these selected. It will take some getting used to, but prevents annoying little bugs.

The second section has some nifty things you should probably enable. Namely, if your cursor isn't quite at the end of the line and you enter a semicolon or brace, it will jump to the right spot to insert it for you, most of the time. How cool is that. Try it out!

Indentation

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

Javadoc

If you take advantage of the auto-Javadoc comments (and who knows? maybe you will!), you'll probably want to also change the code listed under Java  > Code style  > Code Templates  >  Comments  >  Overriding methods to match those under Methods. (Just copy from Methods and paste to Overriding methods, using the Edit button.)

Change the default build path

In recent versions, Eclipse likes to put the *.class files in a different folder than your *.java files. However, this makes things a bit harder to go between eclipse and a normal editor. To change it back to "normal", select Java  >  Build Path and change the value of 'source and output folder' to be "Project". If you make only 2 changes, make this the second one!

Edit-away!

You are now ready to get going on some serious projects! While you are working on stuff, you'll probably find that there are lots of 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.

As a reference guide for some such shortcuts and nifty things, I'll refer you back to the eclipse tutorial, and this list here:


Part 3 - Practice your new Skillz

In order to get familiar with eclipse and its features, you will create a project from existing source like we'll be doing in many of the labs. You are going to be creating a version of the dice game called "Pig".

The rules are pretty simple, you've got two players and a single six-sided die. The goal is to be the first player to score 100 points. Players take turns rolling the die:

  • If it is a 1, the player gets a score of 0 for the round and play passes to the other player.
  • If it is a 2, 3, 4, 5, or 6, then that value is added to that players turn total and they can decide if they want to roll or hold.
    • If they hold, their turn score is added to their total score and it is the other players turn.
    • If they roll again, the player is gambling the points earned so far that turn

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

Create a project

To get started, you should create a project from an existing source like you'll be doing in many of the labs.

  1. Select File  >  New  >  Java Project.
  2. Call the project "PigDiceGame" (or whatever your heart desires).
  3. Choose to create the project from an existing source. Use the Browse button to select the lab0 folder you created earlier in the default location box.
  4. Now just select Finish.

What you should see now is a new directory "PigDiceGame" (or whatever you called it) in the left-most pane of your eclipse window (the Package Explorer window). You can expand the folder by clicking on the neighbouring arrow. You can find the existing source file (Plyaer.java) under the (default package) folder. Double-click on a file if you want to see its contents displayed. Also, if you do an ls on the command line you'll see that there is both Plyaer.java and Plyaer.class in this folder. Eclipse compiles as you work!

Clean up the Plyaer

Click on the arrows (or double-click the name) PigDiceGame and (default package). If you click on the triangle in front of Plyaer.java you'll see all the variables and methods in the class. If you double click on the name, it'll open in the editor. Do that now.

Put your name up top where it belongs. Once you do that, we'll get to fixing things.

Fixing Formatting

You may notice that the indentation is a little funky. Poor coding style makes code harder to understand and hence debug, extend, etc. We can easily fix the indentation by just highlighting the offending lines (or, the whole file, if you like), and then pressing ctrl-shift-F. Wow, now it looks so purdy!

Block commenting

Now just for fun, select all the code in the Plyaer.java file, and hit ctrl-/. All the selected code is commented now. And if you hit ctrl-/ again, the new comments toggle off. Nifty.

Constructor

We'll want to be able to build one of these things, so go to Source  >  Generate Constructor using Fields. (Your cursor needs to be within the class if it gives you grief.) Uncheck score and select Generate constructor comments. Now select OK.

Tada! Add in a line setting the initial score to 0 (just to be explicit about things) and fill in the JavaDoc. You've noticed the misspelling by now, but just keep on letting it be. We'll be fixing that in just a bit.

Javadoc

While your cursor is in the constructor's javadoc, click on Javadoc below. Notice that it generates sample Javadoc on the fly. Click back onto Problems.

Accessors and Mutators

Time do so some stuff with the data. Select Source  >  Generate Getters and Setters from the menu. Check the box in front of score and use the triangle in front of name to select getName(). Select Generate method comments and then hit OK.

Implement a computer player

We'll next create a concrete instance of this abstract class.

  1. Go to File > New > Class. See the icon that is next to it in the dropdown? It is also on the toolbar and you can use that as well. Set the Name to be ComputerPlayer and set the Superclass to be Plyaer. Be sure "Inherited abstract methods" is checked.
  2. Uh, oh. We've got an Error and there is the angry red line at the top. Click on the light bulb icon to the left to see what Eclipse thinks we can do to fix things. Hmmm... The first suggestion to add a constructor looks good, so let's do that. Hey! It created it! (The shortcut key for seeing quick fixes is ctrl-1 [that's a one].)

    You can use tab and shift-tab to jump between the boxes, but we don't need to do that.

  3. See the little checklist icon in front of the commented out line? Eclipse adds one in anytime it sees TODO in a comment. Look on the right side of the editor window and you'll see a blue box corresponding to the same thing (or, you may view a Tasks bar at the bottom of the screen, or you can make it visible through the Window menu). The left side reflects what is in the window, while the right shows you what is in the file. (Red for errors, yellow for warnings, blue for TODO)

    Let's make that go away. Click on the comment in the body of the constructor and hit ctrl-d. Poof!

  4. OK, let's fix the other TODO here. For our purposes, the computer should "hold" if it has a current score of 20 or more and "roll" otherwise. (See The Game of Pig page for a link to a journal article describing what the optimal strategy is.) Well, we need the round score, so let's just add it in as a parameter to make it rollOrHold(int roundScore)

    Look, it's that red line again! If you just hover your mouse over it, you should get a pop-up explaining the problem. (Hit ctrl-1 to see what it thinks are fixes directly.) None of the choices is what we want to actually do which is to add in a parameter to the abstract method. Save the file ctrl-s and switch back to the Plyaer.java tab. Notice that the tab for ComputerPlayer.java shows a red X indicating an error. Go to the abstract method in Plyaer.java, add in that parameter, and save the file. Notice that the red X in the other file and tab goes away!

  5. Fix my typo: Ok, by now, you are probably getting bugged out by the name "Plyaer" (I know I am!). In the source code highlight the class name, right click on it, then select "Refactor" -> "Rename" and note how it changes the name in all Java files. Check it and see!

    If you used it in the comments, you could have opened the Refactor window via that pull-down arrow and changed it in those locations too!

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 good enough for this week. That is, if you feel you're back in the swing of Java, and you're feeling pretty good about eclipse, then I'm happy. However, if you need more practice, or if you'd just like to keep going for fun (and completeness), here are some remaining guidelines.


Part 4 - Lab tools

Running Java

For this class, we'll be exercising more than the usual amount of memory in Java programs. You should be 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

Last semester this wasn't needed on lab machines, but was on your own laptops.

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 in the near future. Go there now and poke around. It may save you some agony down the road to know this is here.

handin / lshand

For this class, you should use handin to submit your work. If you talk to other students, they might tell you about a web based version of handin. Please please don't use this as it is unsupported and results in lost submissions. Consider yourselves warned!

I'll probably give you instructions on how to use handin as shown below, but I suspect you've got the hang of it by now.

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.

Version Control

Might be worth looking at later especially when we do some group projects.


Part 5 - handin

Look through your source files and be sure you've included your 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, or in a README 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.


Last Modified: September 2, 2012 - Alexa Sharp - big thanks to Benjamin Kuperman for the original lab.VI Powered