CS 2510 Spring 2012: Lab 2

copyright 2012 Felleisen, Proulx, et. al.

Goals

The goals of this lab are to get familiar with our work environment: the svn repository, the Eclipse IDE, and the basics of running a program in Java-like languages. In the second part of the lab, (the one that really teaches you something) will focus on data definitions and examples in a simple subset of Java language.

1  Setup of svn and Eclipse Workspace

We start with designing data - designing classes of data that are connected to each other in a systematic way, showing that the Design recipe for Data Definitions can be used virually without change in a completely different language than we have used in the first part.

We start designing methods in the functional style, i.e., every method produces a new value (primitive or a new object) and makes no changes in the existing data. This allows us to use a very simple language, one where the only two statements are return and if (condition) then statement else statement.

The programs we provide give you examples of the progressively more complex data (class) definitions, and illustrate the design of methods for these class Hierarchies.

The design of methods follows the same Design Recipe we have seen before. The only difference here is that for classes that represent a union type (for example classes Circle and Rectangle that are both a part of the union type Shape, the conditional statement used in TeachScheme! inventory/template is replaced by the dynamic dispatch into the class whose constructor has been used to define the specific object.

Managing your work

Professional programmers need to keep track of the changes to their programs in a systematic way. Several different version control systems are designed to keep track of all changes in the program over its lifetime. They allow programmer to access their code from different computers, to return to an earlier version if the new changes prove to be ill conceived, and allow others to see what changes have been made over the lifetime of the program.a

We will use the svn version control system to manage the work of each homework pair. It will help you from loosing your work if a computer crashes, it will allow the partners to collaborate on the project, and will let us see your progress, aswell as the final completed project.

We will start the lab getting set up with the svn. Make sure you ask questions, try to submit at least a simple program to the system, and see how it handles your work.

First of all, you will need to have an CCIS user id.

Log into the lab computer using your CCIS credentials.

Open your web browser to the following web page for information on how to access your Unix/Linux directories. (likely not necessary, if you trust us)

http://howto.ccs.neu.edu/howto/accounts-homedirs/home-directory-access-on-linux-and-windows/

Navigate your way to the Z: drive. If you can't find Computer on the desktop, then double-click the Recycle Bin and click on Computer in the left pane, then double-click the network drive myhome ... (Z:).

You should see the various directories and files that make up your Unix home directory. Right-click on the classes directory and select SVN Checkout... from the pop-up menu.

Visit this page to locate your HW partern's information:

http://www.ccs.neu.edu/course/cs2510/lab-pairs.txt

From there you should be able to locate your HW partner's information, and the number of your pair. Back to the checkout... for the URL of the repository, enter:

https://trac.ccs.neu.edu/svn/cs2510spring2012/pairXXX

where XXX is your pair number from the HW submission page. For the Checkout directory, enter:

Z:\classes\CS2510-Workspace

Or name it something you like better. Click OK. It should ask you for your CCIS username and password. Once you enter those a and it's finished, click OK, then navigate into the classes directory. You should be able to see your new SVN workspace directory!

Or name it something you like better. Click OK. It should ask you for your CCIS username and password. Once you enter those a and it's finished, click OK, then navigate into the classes directory. You should be able to see your new SVN workspace directory!

Create another directory in classes where you will keep all library (JAR) files, call it EclipseJars. For the rest of the Lab/term we will refer to these two directories as EclipseWorkspace and EclipseJars. Keep the file/explorer window open, we'll be using it later in the Lab.

1.2  Eclipse IDE and a simple Java-like language.

Eclipse includes an editor and allows you to organize your work into many files that together make up a project. It has an "incremental" compiler that so you can edit and run your programs while getting relatively fast error feedback. Your Eclipse workspace can contain many projects, so you should be able to keep all your work in one workspace, with one project for each assignment or lab.

Setting up your workspace

Start Eclipse. It should ask you where you want your workspace to be... so enter (or click Browse and navigate to) the workspace directory you created (saw that coming). Feel free to check the "Use this as the default..." box. Click OK.

Once Eclipse starts, close the annoying Welcome screen if it comes up.

Zeroth things First

There's a few settings we need to get out of the way before you start. If/when you work from your own computer you'll have to adjust these settings too, otherwise the graders might be angry... and you wouldn't like them when they're angry.

Select Preferences under the Window menu and change the following settings. You should also be able to follow along with your friendly Lab Leader.

  1. Type "tab" in the search box at the upper-left to minimize the available selections.

  2. Select "Text Editors" on the left. Make sure the "Insert spaces for tabs" check-box is checked.

  3. Check the box "Show print margin" and select 80 spaces.

  4. Check the box "Show line numbers"

  5. When you say OK it will force you to create a name for the profile... you can just say "Mine" or some other cool name.

Great... now let's get on to Java-like programming!

Create a new project, and Check it into SVN

We will be submitting HWs via SVN through the Trac repository workspace that you just setup.

Go back to your file/explorer window, and enter your workspace folder Your project directory should be visible... right-click on it and select TortoiseSVN > Add.... Uncheck all entries, then select the entries for the EclipseWorkspace, the Project folder, the src folder inside of the Project folder, and all files inside the src folder. Click OK, then click OK again, when the action completes.

Right-click the project directory again and select SVN Commit.... Enter a message like "First checkin", then click OK. In general the msage should be meaningful, so that your partner can tell what you were doing when you changed things. After the action completes you now have all you need to submit assignments!

See the HW Handin link off the course webpage for more information and details on HW submission and organization.

A Java-like Experience

  1. Now that you're an Eclipse and SVN expert, download the library we need for running the tests and examples from the lab's index page.

          tester.jar

    Save it --- but be careful --- on the bottom of the window select Save As All Files not Save As WinArchive --- save it in your EclipseJars directory you have created before. The JAR provides output and testing functionality you saw in the lecture.

    Right-click on your project directory in the Package Explorer pane and select Build Path > Add External Archives.... Navigate to your EclipseJars directory and select your tester.jar.

  2. Add the Shapes.java file to your project

  3. Set up an run configuration

    You should see a few messages in the Console, and a display of the ExamplesShapes class with the example instance(s).

    If the program does not run and reports that it cannot find java or javac, open your Run Configuration and click on the tab Environment then select New and enter the following information:

    Then click OK and try running again.

  4. View, Edit, and rerun Shapes.java

2  Practice with FunJava

We are building an App that will help us find places in a city. We first need to record addresses of interesting places. Here's a Racket data definition for an address:

    ;; An Address is: (make-addr String Number String)
    (define-struct addr (street no city))

    ;; Example Addresses:
    (define a1 (make-addr "Beacon" 150 Boston)) 
    (define a2 (make-addr "Boylston" 150 Boston))
    (define a3 (make-addr "Chestnut" 25 Cambridge))

  1. Draw the class diagram for this data definition

  2. Create a new file in your Eclipse Lab project named Address.java and convert the data definition/class diagram into a FunJava class definition.

  3. Create an Examples class, and include instances of Addresss. See Shapes.java for the necessary format(s).

  4. Create a new Run Configuration and run the examples.

2.1  Classes containing classes

We now want to keep track of hotels in the city. For each hotel we need to know its name and its address. (We could include additional information, but for now, this is sufficient. Note that there may be a Hilton hotel at two different addresses --- that is OK.

    ;; A Hotel is: (make-hotel String Address)
    (define-struct hotel (name addr))

    ;; Example Hotels:
    (define h1 (make-hotel "Hilton" a1))
    (define h2 (make-hotel "Hilton" a2)) 
    (define h3 (make-hotel "Marriot" a3))

  1. Draw the class diagram for these data definitions

  2. Add the Hotel class to your file and add the corresponding data definitions.

  3. Run the examples to make sure everything is correct.

2.2  Unions of Classes (of data)

We certainly are interested in more than just hotels. We would like to know about other attractions, for example restaurants (each has a name and the type of food they serve, e.g. French, Mexican, Italian, Vegetarian; we would like to be able to find movie theaters (maybe with the number of show places each has), and other attractions.

Create an interface to represent various kinds of attractions call it IAttraction. The union of classes of IAttraction will be made up of three different FunJava classes... feel as described above. Feel free to add more information to each, or to add another class for your favorite kind of atttration.

If you change the name of the class Hotel to HotelAttr for the hotel data that are a part of the new union, you can use the same java file, just include new examples in your Examples class

  1. Draw a class diagram for the class hierarchy that represents these three types of Attractions

  2. Design data definitions (i.e., classes) for each of the definitions (including the interface)

  3. Add examples of each kind of IAttraction to your Examples class. Be sure to use IAttraction as the type of the example fields.

2.3  Self-Referential Class Hierarchies

Remember the ancestor tree example from last semester? Brings back fond memories of recursion doesn't it? Let's re-live a little... here's something like the definitions we had last semester:

    ;; An Ancestor Tree (IAT) is one of:
    ;;  - 'unknown
    ;;  - (make-at String IAT IAT)
    (define-struct at (name mom dad))

    ;; Example IATs:
    (define at1 (make-at 'nknown))
    (define at2 (make-at "Pete" (make-at "Jan" at1) (make-at "John" (make-at "Mary" at1) at1)))

3  Wrapping up, checking in

Last, but not least, save your file(s), and add them to SVN. Right click on the file and choose TortoiseSVN > Add.... When you're done select Commit for the entire workspace directory.