CPSC 329 Software Development Fall 2017

CPSC 329 Lab 1: Preliminaries

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.

Objectives

Successful completion of this lab means that you:

Collaboration

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.

Due Date and Deliverables

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!


Software Installation and Setup

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.

Directory Setup

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:

Eclipse Installation

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".

Eclipse Setup

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.)

Other

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.)

Plugin Installation

Install Subversive via an update site:

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:

Install Papyrus via an update site:

Install PlantUML via an update site:

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.


Survey

Send an email to bridgeman@hws.edu with your answers to the following:

  1. name you prefer to be called

  2. CS courses you have completed (not including any others you may be taking this semester)

  3. other CS courses you are taking this semester

  4. 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)

    1. working with directories and files in Linux (creating/deleting directories and files, changing between directories, copying files, etc)
    2. using the commandline in Linux
    3. using Eclipse
    4. using a version control system (such as CVS or Subversion)
    5. unit testing
    6. JUnit
    7. using Java's Collections Framework (classes like List, ArrayList, Map, HashMap, etc)
    8. using generics (e.g. List<String>)
    9. creating a GUI using Swing components (buttons, drop-down lists, text boxes, etc)
    10. using layout managers to arrange GUI components
    11. GUI event handling in Java (setting up listeners, handling the events)
    12. using sockets
    13. client/server networking in Java
    14. writing multi-threaded programs in Java
    15. working with Java's stream classes
  5. What aspects of programming or developing programs do you feel most comfortable with?

  6. What aspects of programming or developing programs do you find most difficult? What would you like to be better at?


Using Linux

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!


Using Eclipse

Eclipse Concepts

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.

Creating a Project

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.

Importing Files

Sometimes you'll want to include already-existing code in your project. To do this, you must import a resource:

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.

Error Messages and Quick Fix

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.

Eclipse will provide suggestions about how to fix the problem, and even carry out the fix for you.

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.

Auto-Generating Code

To fix the remaining errors, it is necessary to implement the Guitar class.

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:

Voilà! A complete constructor, except for needing to fill in the comments. (You can skip that for this lab.)

Next, we need getters for each instance variable and a setter for the price. Again, you could type them by hand, but why?

Whee, instant getters and setters! You can again skip filling in the comments.

Creating New Files

The guitar store is expanding, and would like to also sell mandolins.

First, create a new class:

Now, implement the class:

Auto Formatting

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.

Running the Program

At this point there shouldn't be any more errors in the program, so it is time to run it.

You should see the program's output below the editor tab - two guitars should match Erin's specifications.


Useful Features

Cleaning Up

Even properly formatted, Guitar and Mandolin may be a bit disorganized. To sort the elements of the class (instance variables and methods):

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.

Renaming Things

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.

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:

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.

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.

More Refactoring

"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.

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.

A couple of errors have now appeared, because the Guitar and Mandolin constructors made use of the default constructor in Instrument.

You can fix Mandolin in the same way, or try another approach:

There should be no more errors, and running the program should produce the same results as before.

Locating Declarations

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...

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.)

References

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.

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.


Exploring

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:


Valid HTML 4.01!