|CPSC 329||Software Development||Fall 2017|
The development environment for this course includes the Linux operating system and Eclipse, an open-source IDE originally developed by IBM. IDEs (or integrated development environments) combine the basic tools of software development - an editor and a compiler - with other useful things such as automation of the build process, a debugger, code-generation tools, and support for quickly browsing and navigating the files in a large software project. We will also make use of several plugins for Eclipse that provide additional specialized functionality. Programming can certainly be done without an IDE, but a good IDE can help streamline the process and make development faster and easier. Eclipse is a real piece of software used in actual software development environments. The current version is Eclipse Oxygen.
Successful completion of this lab means that you:
Everyone needs to perform the setup steps for themselves (and know how to use Eclipse), but otherwise there are no limits on discussing and/or working with others.
Please complete the survey as soon as possible (and no later than Friday 9/1). The rest of the lab should be completed by Monday in order to be ready to begin on project 1. There is nothing to hand in (other than completing the survey) and nothing will be graded, but you'll need this stuff for the rest of the course!
Make sure you perform these tasks! If you have a laptop or another computer that you plan to use in this course, set up that computer in addition to your CS department account.
Eclipse projects go into a workspace directory. Many configuration options are associated with the workspace, so you can have different workspaces with different settings. It is recommended that you create a workspace specifically for this course:
Create a workspace directory ~/cs329/workspace. (Note that you will first need to create the parent directory ~/cs329.)
Eclipse Oxygen (the latest version) is available on the lab computers. If you have an older version on your computer, you are encouraged to upgrade. Eclipse is available here - choose "Eclipse IDE for Java Developers".
The first time you start Eclipse (or if you have used Eclipse before but have configured it to ask each time), you will be prompted to specify a workspace directory - choose ~/cs329/workspace. If you are only using Eclipse for this course, you can check the "Use this as the default and do not ask again" box - otherwise Eclipse will prompt you for the directory every time it starts up. To change to a different workspace once Eclipse is running, go to "File"->"Switch Workspace".
One useful feature of an IDE is the ability of the IDE to handle some of the grunt work for you - Eclipse can auto-format your code to adhere to a particular whitespace convention and can auto-generate some standard code (such as class headers and getters and setters) and comments. Eclipse stores these settings with the workspace so you can use different configurations for different sets of projects. (This also means that you will need to repeat these configuration steps for any new workspace you create, such as one on your own computer.)
From the "Window" menu, choose "Preferences". On the left side, expand "Java".
Specify how code should be formatted:
Expand the "Code Style" item under "Java", then click "Formatter".
On the right side of the window, click "Import..." and then navigate your way to /classes/cs329/eclipse (start with "File System" and then double-click on each directory in turn).
Highlight cs329-formatter.xml and click "OK".
Make sure that cs329 is shown as the "Active profile".
Specify options for code generation:
Back in the Preferences window, choose "Code Templates" under "Java" and "Code Style" on the left.
On the right side of the window, click "Import..." and then again navigate your way to /classes/cs329/eclipse.
Highlight cs329-codetemplates.xml and click "OK".
At the bottom of the right side of the window, click the "Automatically add comments for new methods and types" box. (It should be checked.)
Tell Eclipse about the convention of naming instance variables ending with _:
Back in the Preferences window, choose the main "Code Style" entry on the left.
On the right side of the window, click on "Fields" in the box under "Conventions for variable names:" to highlight it, then click the "Edit..." button. In the box that pops up, enter _ (an underscore) in the "Suffix list" box and click OK.
Tell Eclipse to enforce Java 1.8 syntax rules:
Back in the Preferences window, choose the "Compiler" entry under "Java".
On the right side of the window, set the compiler compliance level to 1.8.
Tell Eclipse to store compiled classes separately from source files:
Back in the Preferences window, choose the "Build Path" entry under "Java".
Make sure that the "Folders" option is selected under "Source and output folder", and that the source folder name is src and the output folder name is bin.
Finally, click "OK" at the bottom of the Preferences window to apply the new settings and close the window.
If you don't have a launcher icon for Eclipse Oxygen on your panel, you may wish to create one to make starting Eclipse easier. (Locate Eclipse Oxygen in the Applications menu and drag the Eclipse icon from the Applications menu to the panel, near the other launcher icons.)
Install Subversive via an update site:
In Eclipse, go to Help->Install New Software...
If you have not previously set up the desired update site (click on the down arrow at the end of the "Work with" box to see what has been set up), click "Add..." to the right of the "Work with" box.
In the "Add Repository" dialog, enter "subversive latest" for the name and http://download.eclipse.org/technology/subversive/4.0/update-site/ for the location. Click OK.
Select "Subversive Revision Graph" and "Subversive SVN Team Provider" (under "Subversive SVN Team Provider Plugin") as the items to install.
Repeatedly click "Next>" to complete the wizard, accepting the licenses as needed. Restart Eclipse when prompted to do so.
If you are prompted to install the Subversive SVN Connectors after Eclipse restarts, go through the dialogs to install "Subversive SVN Connectors" and "SVNKit 1.8.14 Implementation". Otherwise install the Subversive SVN Connectors via an update site:
Repeat the installation process that you did for Subversive (above), except enter "svn connectors" for the name and http://community.polarion.com/projects/subversive/download/eclipse/6.0/update-site for the location in the "Add Repository" dialog. Choose "Subversive SVN Connectors" and "SVNKit 1.8.14 Implementation" (under "Subversive SVN Connectors") as the items to install.
Install Papyrus via an update site:
Repeat the installation process that you did for Subversive and the SVN Connectors (above), except enter "papyrus" for the name and http://download.eclipse.org/modeling/mdt/papyrus/updates/releases/oxygen for the location in the "Add Repository" dialog. Choose "Papyrus for UML" (under "Papyrus") as the item to install.
Install PlantUML via an update site:
Repeat the installation process that you did for Subversive and the SVN Connectors (above), except enter "plantuml" for the name and http://files.idi.ntnu.no/publish/plantuml/repository/ for the location in the "Add Repository" dialog. Choose "PlantUML Feature" (under "PlantUML Eclipse support") and "PlantUML Library Feature" (under "PlantUML Library") as the items to install.
If you are setting things up on your own computer, you will likely also need to install graphviz in order for PlantUML to work. If you are running Linux, you may be able to use your package manager to install a graphviz package. For Windows and Mac, download graphviz here. Once graphviz has been installed, go to Window->Preferences in Eclipse, click on "PlantUML" on the left side of the window, and check the "Graphviz path" information - if the dot executable is not found and you don't get an "installation is OK" message, fill in the "Path to the dot executable of Graphviz" to tell Eclipse where it is.
Send an email to email@example.com with your answers to the following:
name you prefer to be called
CS courses you have completed (not including any others you may be taking this semester)
other CS courses you are taking this semester
on a scale of 1-4, indicate your level of comfort with each of the following (1 = know nothing/have never heard of it, 2 = vaguely familiar/have heard of it but don't remember the details, 3 = remember something/just need a quick refresher, 4 = feel confident)
What aspects of programming or developing programs do you feel most comfortable with?
What aspects of programming or developing programs do you find most difficult? What would you like to be better at?
If you are unfamiliar with Linux or with using Linux in the Math/CS department, check out Using Linux at HWS. There is information on how to get access to Linux outside of class, basic usage of the system (including commandline commands), and how to deal with some kinds of problems. See the SFTP page for information on transferring files between your computer and the department filesystem.
Please remember: Log out before you leave the lab, and if you use the Rosenberg lab, always reboot the computer to Windows before leaving!
Eclipse has an extensive built-help system accessible through the "Help" menu. If you are unfamiliar with Eclipse, start with "Help"->"Welcome" and check out:
As you carry out steps in the tutorial and type in code, you'll notice that Eclipse tries to be helpful - it pops up warnings and suggestions for code completion, and puts all sorts of little markers and icons in and next to your code indicating syntax errors and other things. It is very important to keep in mind that Eclipse is not always right - you should pay attention to the messages, but make sure you understand them before following the advice. In the case of the warning "The use of the default package is discouraged" when you create a new Java class, it is OK to ignore it for now.
Before you can work on a program, you'll need to create a project. You'll generally have a separate project for each assignment or program.
Select "File"->"New"->"Java Project" from the menu.
Enter a project name (name this project lab1) and verify that a Java 8 JRE is selected (it should be if your workspace preferences are set up correctly). Click "Finish".
Sometimes you'll want to include already-existing code in your project. To do this, you must import a resource:
In the Package Explorer tab, right-click on the folder you want to import the files into (choose src - all .java files will go under src or a subdirectory of it) and choose "Import...".
In the Import dialog box, reveal the "General" tab, highlight "File System", and click "Next>".
For the "From directory:", navigate to the directory containing the file(s) you wish to import. In this case, navigate to (or type in) /classes/cs329/lab1. Note that you are only selecting the directory containing the files to import in this step - you'll choose the files next!
Click on the checkboxes to select the files you wish to import (all of them - Builder.java, FindGuitarTester.java, Inventory.java, Type.java, Wood.java). If you don't see those files listed on the right side of the window, expand (but don't check the checkboxes) the necessary directories on the left.
Verify that the "Into folder" is lab1/src (the folder you want to import into) - fix it if necessary.
If you are successful, you'll now see the five files listed under your project in the "Package Explorer" tab. You may need to expand the "src" and "(default package)" items. Importing files like this copies them to your project directory - any changes you make will not affect the original copy.
You may notice that some of the files have a little red X icon next to them in the Package Explorer.
Ever helpful, Eclipse warns you about potential problems and syntax errors - the little red X indicates errors while yellow ! icons are warnings. Errors and warnings are noted in multiple places: icons in the Package Explorer show files containing errors and packages/directories/projects containing files with errors; icons on the left side of the editor flag individual lines of code with problems; red and yellow boxes on the right side of the editor window show the location of problems within the whole file; icons in the "Outline" tab on the right show classes, methods, and variables with errors; and the "Problems" tab at the bottom lists information about the errors.
You can double-click on an error in the "Problems" tab or single-click on the red/yellow box on the right side of the editor window to go that error.
Double-click on the first error in the "Problems" tab. This should take you to a line near the beginning of the main program.
Eclipse will provide suggestions about how to fix the problem, and even carry out the fix for you.
Hover the mouse over the red X icon to the left of the line with the error. Don't click! In a moment, a description of the error will pop up.
Click on the red X icon to get a list of possible fixes.
Double-click on the correct fix. In this case, the problem is that the type Guitar is used but there is no Guitar class - choose "Create class 'Guitar'".
Verify that everything is correct in the new class dialog (it should be), check the "Generate comments" box if it isn't already selected, and click "Finish".
You should have a new (but empty) class Guitar, and the first error in FindGuitarTester should have gone away (though there are now new errors).
Important: Eclipse is often right about the cause of the problem - but sometimes it isn't. Don't just always choose the first fix, or assume that the correct fix must be one of the choices! Make sure you figure out what the right solution is before selecting a fix.
To fix the remaining errors, it is necessary to implement the Guitar class.
Add declarations for the following instance variables in the Guitar class: (remember to make them private)
Now, we need a constructor to initialize all the instance variables. You could type it by hand, or you can get Eclipse to do it for you:
Position the cursor where you want the constructor to go.
Choose "Source"->"Generate Constructor Using Fields" from the menu.
Select options for the constructor: there should be a parameter for each field (make sure all of the names are checked), it should be public ("Access modifier"), and comments should be generated (check the "Generate constructor comments" box).
Voilà! A complete constructor, except for needing to fill in the comments. (You can skip that for this lab.)
Save Guitar.java. If you go back to FindGuitarTester, you should notice that some of the errors have disappeared.
Next, we need getters for each instance variable and a setter for the price. Again, you could type them by hand, but why?
Position the cursor where you want the getters to go.
Choose "Source"->"Generate Getters and Setters" from the menu.
Select the getters and setters you want - you can click the fields if you want both getters and setters, or reveal the little tab to select just the methods you want. Create getters for everything, but only the price should also have a setter.
Select additional options for the getters/setters: make them public ("Access modifier" should be "public") and generate comments (check the "Generate method comments" box).
Whee, instant getters and setters! You can again skip filling in the comments.
Save Guitar.java. All of the errors in the project should now be gone.
The guitar store is expanding, and would like to also sell mandolins.
First, create a new class:
Check over the options in the dialog box that pops up, but probably the only thing you'll need to do is fill in the name (Mandolin) in the "Name" box.
Now, implement the class:
Add the following instance variables: (make them private)
Add a constructor to initialize the instance variables from values passed in as parameters. Let Eclipse generate it for you.
Add getters and setters: getters for everything and a setter for the price. Let Eclipse generate them for you.
While you now have complete Guitar and Mandolin classes, the formatting may be a bit messy. Eclipse can fix this for you, so there's no excuse for handing in poorly formatted code.
Open Guitar in the editor.
"Source"->"Format" or press ctrl-shift-F.
Repeat for Mandolin.
At this point there shouldn't be any more errors in the program, so it is time to run it.
Right-click on the file FindGuitarTester.java (the main program) in the "Package Explorer" view and choose "Run As...->Java Application".
You should see the program's output below the editor tab - two guitars should match Erin's specifications.
Even properly formatted, Guitar and Mandolin may be a bit disorganized. To sort the elements of the class (instance variables and methods):
Open Guitar in the editor.
Choose whether or not you want to include instance variables in the sorting. It's OK in this case, so select "Sort all members".
Note that all of the instance variables are now at the beginning of the file, and the variables and methods have been sorted in alphabetical order.
Repeat for Mandolin.
Sometimes you need to change the name of something - a project, a package, a class, a method, a variable... This often involves a lot of search-and-replace while you hope you don't miss a spot - or you just decide the original name wasn't so bad after all. Eclipse makes renaming easy so there's no excuse to stick with poor naming choices.
In the example program, the name Type for the guitar type enumerated type is not the best because "Type" is pretty generic and doesn't give any clues as to what it is the type of. "GuitarType" is a more descriptive name, particularly when there may be other kinds of instruments with their own types.
Find somewhere Type is used (e.g. the declaration of the type_ instance variable in Guitar) and right-click on it. Choose "Refactor"->"Rename" from the menu.
When the "Enter new name, press Enter to refactor" prompt pops up, type in the new name. (Don't press "Enter" yet!)
Eclipse gives you control over how extensive the renaming changes are. The default is to just update references, but you can also update related usages (such as getters/setters when an instance variable name is changed, or some variable names when a class name is changed) and places where the name is used in comments and strings.
Let's get a better idea of what can be updated:
Click on the little down tab next to the "Enter new name, press Enter to refactor" prompt and choose "Open Rename Dialog...".
Check all of the boxes and click "Next>".
The next dialog box shows all the similarly named variables and methods that will be updated. You can review the choices and uncheck any that you want to leave alone.
You've decided to leave parameter and variable names alone and just change usages of the Type type - uncheck places where a variable will be renamed.
The next dialog box shows all the changes that will be made. You can review the choices and uncheck any that you want to leave alone.
Observe the results in the now-renamed GuitarType class - even the reference to Type in the constructor's comments has been updated. Also note that all other references to Type have been updated e.g. in Guitar.
"Refactoring" refers to changing a program's implementation without changing its functionality. Renaming elements is one type of refactoring; Eclipse can help in other situations as well.
Guitar and Mandolin are awfully similar - a better design might be to have an Instrument class with all of the properties common to both, and have Guitar and Mandolin extend Instrument.
Open Guitar in the editor.
Fill in the dialog box:
"Superclass name" - Instrument
Check "Use the extracted class where possible" - this will change references from Guitar (and Mandolin) to Instrument where possible
In "Types to extract a superclass from", click "Add..." and add Mandolin - we want Instrument to be the superclass for both Guitar and Mandolin.
In "Specify actions for members", select all of the instance variables and methods that you want to move to Instrument. (This should be everything except the variable and getter for the number of strings.) The action for the selected things should be "extract".
Select all of the methods to remove from the subclasses (because they'll be inherited from the new superclass). This should be everything.
The last screen allows you to review the changes that will be made. You can unselect things you don't want. Click "Finish" to actually complete the refactoring.
Review what happened - look over Guitar and Mandolin and the new Instrument class. Eclipse did a pretty good job, but there's one other change that should be made - Instrument should have (only) a constructor which initializes its instance variables.
Delete Instrument's default constructor and have Eclipse create one which initializes all the instance variables.
A couple of errors have now appeared, because the Guitar and Mandolin constructors made use of the default constructor in Instrument.
Open Guitar in the editor, and use Quick Fix to correct the problem (add parameters to the call of the superclass constructor).
Delete the now-unnecessary assignment statements from Guitar's constructor. (Leave only the one for numStrings_.)
Save the file. It should now be error-free.
You can fix Mandolin in the same way, or try another approach:
Open Mandolin in the editor.
Delete the constructor.
Have Eclipse generate a new constructor.
There should be no more errors, and running the program should produce the same results as before.
Finding where a variable, method, or class is defined is not usually all that difficult - but it still can get tricky in a large project with lots of packages, and hunting down a method can involve first hunting down the type of the variable it is invoked on. Fortunately Eclipse can do it for you!
So, imagine that you are reading through the main program for this inventory software, and you come across something that you want to know more about...
Highlight the name that you want to find the declaration for - for this example, highlight getBackWood part of the guitar.getBackWood() call in main.
Right-click on the highlighted name and choose "Declarations->Project" from the menu.
The results are displayed in the "Search" tab below the editor showing your code. (It's probably no great surprise that this method is in the Guitar class.)
Double-click on the desired entry in the "Search" tab (below the editor showing your code) to go to the declaration itself.
It is much harder to locate all the places in your code where a particular class, method, or even variable is used. Once again, Eclipse comes to the rescue.
This time you're reading through the main program and you want to know where else getBackWood is used.
Highlight the name for which you want to find usages - for this example, highlight getBackWood part of the guitar.getBackWood() call in main.
Right-click on the highlighted name and choose "References->Project" from the menu.
Once again, the results are displayed in the "Search" tab below the editor showing your code. You can double-click on the desired entry in the "Search" tab to go to the location of a particular reference.
Eclipse is a powerful tool, and does much more than has been described so far or that will be covered in this course. Feel free to explore and to experiment. A few things of particular interest:
The "Basic tutorial", found under "Getting Started" in the "Workbench User Guide" (accessible from "Help"->"Help Contents"), takes you through many of Eclipse's features.
The environment is highly configurable. Check out the other preferences available in the Preferences window, but you should avoid changing the Code Templates or Formatter settings (at least for this course).
The formatting, code generation, and refactoring tools described above are only some of what Eclipse can do. Explore the options on the "Source" and "Refactor" menus to see what other possibilities there are.
Eclipse also tries very hard to be useful, though it doesn't always succeed. You can find out about a lot of these "assistance" features by checking out "Tips and Tricks" under the "Help" menu (select "Eclipse Java Development Tools" when asked).