CS 261 Lab A - Hello Eclipse!
Due Wed Jan 23 at 9:00am
Overview
This lab will help you set up the Eclipse IDE (integrated development environment) for future work this semester, and familiarize you with some of the system's basic features. The programming task here is fairly simple: you'll create the beginnings of an Calculator class that models a simple text-based calculator.
Note that this first lab should be completed individually (not in pairs).
Objectives
- To install and become familiar with the Eclipse IDE
- To become familiar with the procedure for submitting your work
- To review creating classes and writing methods.
Necessary Files
You will need to download the Eclipse IDE. Open the Eclipse download page in a separate window and find the link for the version of Eclipse for your computer (if you get lost, you're looking for the "Eclipse IDE for Java Developers").
You will need to download the copy of the zipped source files. for you to import.
Details
-
Download the Eclipse IDE as described above. Unzip the downloaded file to a convenient folder where you'll be able to get to it regularly. On Windows, I put the Eclipse folder in "Program Files", and on a Mac a put it in "Applications". Inside that folder should be a number of files;
eclipse.exe
orEclipse.app
is the program to launch. Double-click this to start the IDE. - Eclipse will prompt you for a folder to make into your workspace. This is where Eclipse stores the .java and .class files you create. You can accept the default location, or choose a different one (I recommend default location, and select the "Use this as default" option to avoid being nagged in the future). In either case, remember where this folder is!! Eclipse will then open to a Welcome screen. Click the close icon on the Welcome tab to show the main project window. It should look something like this:
- First we need to create a Java project to work on. On the menu bar, click File > New > Java Project to open the New Java Project dialog box. Name the project "LabA" and then click "Finish" to create the project. Note that Eclipse has A LOT of customizable configuration options you can set when creating a project; in general, we'll be fine with defaults.
- On the left-hand side, in the Package Explorer, you should now see a blue "LabA" project folder. Click the plus or arrow symbol next to it to expand that project, then select the yellow "src" source folder. This is the folder that will contain your source code. Let's add something to it, then we can take a step back. With the "src" folder selected, select File > New > Class (alternatively, you can right-click on the "src" folder and select New > Class). This should bring up the New Class Dialog: This is just like the dialog you use to make a new class in BlueJ, but with more options! Under name, enter "Hello" to make a new Hello class, and just for fun select "public static void main(String[] args)" under "Which method stubs would you like to create?" Then hit Finish to create the class. Note that this class can now be found in the "default package" in your "src" folder, and its source code should show up in the center of the window, with a main method already included!
-
Your Eclipse window should now look something like this (note that you may have slightly different comment structures depending on your preferences):
Let's go over each piece in turn:
- Editor: This is your main editing window, where you type in your source code. Multiple files will be organized as tabs. Eclipse has a number of handy editing features, including code completion (the system will automatically fill in method arguments) and block collapsing (click the minus symbol to "hide" the source code for a block, making it easier to read and navigate large code packages). Note that Eclipse will automatically "compile" code for you as you type, marking syntactical errors as you go; however, you need to explicitly "save" a file to run it and to turn it in.
- Package Explorer: This sidebar will list all the source files and packages your project is using. It is how you can move between different files. Note that each file can be expanded so you can navigate to specific methods.
- Problems (and Console): The bottom of the screen contains "views" (a representation of some information about your project or code). For example, the "Problems" tab will list errors. The Console will also show up down here once we start filling in our program.
- Other Views: Eclipse has a wide variety of views you can include: the task list lets you include a "to do" list in your project, and the Outline view can help with navigating through large projects. If you select Window > Show View from the menu bar you can add in a number of additional views, or click the "close" button on each tab to hide the view. You can also drag and drop to reorganize how these views are positioned on the screen.
- Okay, let's complete our Hello class. The classic first program a student writes is "Hello World"--a program that prints out the text "Hello World." As a variant of this, modify your Hello class so its main method prints out "Hello CS261!". Also add a comment at the top of the file that include your name (using the Javadoc
@author
tag). -
Test your program by running it: select the Hello.java file in the Package Explorer, then click the green "play" button in the middle of the toolbar to run that class. (If you hit the "play" button without selecting the file, choose to run it as a "Java Application"). You can also right-click on the file in the Explorer and select "Run As > Java Application", select "Run > Run" from the Menu, or hit Cntl-Shift-F11. Eclipse has many different options for performing the same functionality!
Once you've run the program, you should see the "Console" view appear at the bottom of the screen with your printed message! - You'll now submit your new Hello class to test that you can submit files. Open the folder that contains your workspace (remember where that is!), then open the LabA folder, and then the "src" folder--this is where your Hello.java file is located.
-
You will need to copy this file to the submission folder on the hedwig server. You will need to
connect to the network drive
that contains this folder--follow the link for instructions. For Windows users, enter
\\hedwig\jross_cs261
for the folder name. For Mac users, entersmb://hedwig/jross_cs261
. Your submission folder for this lab is theLabA
folder, then the folder with your username. If you have any problems, ask for help! - Okay, Part 1 is done: let's look at a few more features of Eclipse and do a bit more programming. First, download AddingMachine.zip and unzip it into a convenient directory. This zip file contains some Java classes we're going to want to import.
- Now in Eclipse, select the "src" folder in the Package Explorer, then select "File > Import" from the main menu (you can also right-click on the "src" folder and select "Import"). In the dialog that pops up, select General > File System and click "Next".
- Click the "Browse" button and navigate to the folder that contains the AddingMachine files. Select this folder, and then click the check boxes next to both the AddingMachine.java and AddingMachineTester.java options. (You're selecting the folder to import, and then selecting which files to import from that folder). Click "Finish" and you should now see the two files added to your "src" folder The icon with the red x next to the AddingMachineTester indicates that this file contains errors and so will not compile. If you open the file in the editor (double-click on it), you can see a similar icon next to the lines that contains the errors, and the errors themselves should be underlined. Before proceeding, take a moment to diagnose this error. What's the problem? (Hint: if you mouse over the error icon in the editor window, a tooltip will explain the problem!)
-
Figure it out? There are two issues, and fixing them will demonstrate another set of handy Eclipse features:
- The first issue is that while we want to use the
Scanner
, we forgot to import the class fromjava.util
. We could type that in ourselves, or we can let Eclipse help us out! First, place the cursor on the underlined "Scanner" type. Then from the menu bar select "Source > Add Import". This will automatically add the correct import statement; if Eclipse doesn't know which version of the class to add (e.g.,javax.swing.Timer
orjava.util.Timer
), it will prompt you to select one. Alternatively, you can select "Source > Organize Imports" with the cursor anywhere in the file; this will not only add the correct imports for your program, it will delete any imports you don't need! There are handy shortcuts for these commands as well. -
The second issue is that our AddingMachine doesn't have a getValue() method! So let's give it one. Open up the AddingMachine class, and we can see that it has an attribute called
value
. What we want is a "getter" for that field. We could type this out quickly, or we can ask Eclipse for help. Place the mouse cursor inside the "value" field--it should become highlighted, indicating that the variable is selected. Now right-click on the variable, select "Source > Generate Getters and Setters...". This will bring up a dialog for you to specify which getters/setters to create! We don't need the setter, so uncheck that box. You can select where in the code this method should be added with the "Insertion point" drop-down (I'd put it after the constructor). Once you click OK, you'll see you now have a getter automatically generated for you! If you save the modified file, you'll see that the error goes away, and you can run the AddingMachineTester (using the same method you used to run Hello).- Also add a JavaDoc comment to your new method. Start by typing
/**
and hit Enter--you should see Eclipse auto-populate a tempate for a comment, complete with javadoc tags! Be sure and fill in the details.
- Also add a JavaDoc comment to your new method. Start by typing
Note that there are other handy "generation" tools inside the Source Menu; in fact, I highly encourage you to look through the menus of the IDE to get a sense for some of its capabilities.
- The first issue is that while we want to use the
-
For the next step, extend the AddingMachine class and give it a "subtract" method. This should be trivial. Make sure to modify the AddingMachineTester and test your addition! (You will want to add a way for the user to indicate whether a number should be added or subtracted... try using the Scanner's methods and an
if
statement). -
Now for the day's last piece of Eclipse magic (one of my favorite features): Since we've adding a subtract method, our class is not really an adding machine anymore--it's really a Calculator. So let's change it to be a Calculator class. Now we could go replace every instance of "AddingMachine" with "Calculator", but that's tedious. Instead, place the cursor over AddingMachine name in the class definition line to select it (it should be highlighted in gray). Right click the class name and select "Refactor > Rename". Replace "AddingMachine" with "Calculator" and hit enter. You should also refactor AddingMachineTester to be named CalculatorTester.
- This functionality can also be found in the "Refactor" menu--another good menu to explore for helpful Eclipse features.
- Finally, finish up the Calculator class by giving it the ability to multiple and divide. (Be careful not to divide by 0!) This should be very straightfoward. You're welcome to add additional mathematical functions as desired. Be sure to add a demonstration of each function to the CalculatorTester; you will likely want to use some kind of conditional so the user can pick an operation to perform.
- Once you have finished your Calculator, upload both the Calculator.java file and the CalculatorTester.java file to the LabA submission folder on hedwig (as described above) to receive full credit on this lab.
Part 2
One last thing: at the end of each lab, you'll need to fill out a "peer evaluation" on Moodle to give some feedback on your lab partner. But since you don't have a partner for this lab, you'll instead be filling out a different survey.
-
Log in to the Moodle page for this course--a link can be found on the main course page, or you can simple access it from
moodle.pugetsound.edu.
- This page is where I will put the lab partner evaluations, as well as upload source code and such from lectures.
- Under the "Surveys" heading you should see a survey called "Introduction." Click on that and fill in the short survey.
- That's it for this part. Note that you'll need to make sure to fill in your lab partner evaluations BEFORE the lab deadline--take 3 minutes to do it right after you turn in the lab.
Submitting
Submit the following files to the LabA submission folder on hedwig, following the instructions above: Hello.java, Calculator.java, CalculatorTester.java. Make sure you upload your work to the correct folder! The lab is due at the start of class on the day after the lab.
Notes
This lab is a warmup for this class and introduction to Eclipse. If you have any problems with the programming required, you need come see me ASAP (that means now!).
You may also be interested in Lars Vogel's Eclipse tutorial, which I have based portions of this lab on. It has a great deal of more detail about particular features and configurations of this extensive piece of software. There are many other good Eclipse guides, and we'll be looking at some more capabilities throughout the semester.
Grading
This assignment will be graded based on the following criteria:
- Submitted a working Hello.java file that prints "Hello CS261!" (20%)
- Submitted a working Calculator.java program that is able to add, subtract, multiple, and divide (40%)
- Submitted a working CalculatorTest.java program that demonstrates that the Calculator.java program works (25%)
- All code submitted is fully commented, with your name in the @author field and Javadoc tags added to each method (10%)
- Completed the Introduction survey (5%)