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:
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.
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.
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.)
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.
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.
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'll get more time to practice with eclipse momentarily. But first...
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.
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!
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!
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.)
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!
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:
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 you want, you can close the Task List and Outline panes on the right side. We won't be using them for this.
To get started, you should create a project from an existing source like you'll be doing in many of the labs.
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!
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.
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!
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.
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.
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.
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.
We'll next create a concrete instance of this abstract class.
You can use tab and shift-tab to jump between the boxes, but we don't need to do that.
Let's make that go away. Click on the comment in the body of the constructor and hit ctrl-d. Poof!
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!
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!
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.
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.
The CS server will be making periodic backups of your files for you. You can
access these backups in the folder
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.
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.
Might be worth looking at later especially when we do some group projects.
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.
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.