#lang scribble/manual @(require "../../web-pages/vkp-scrbl/unnumbered.rkt" "../../web-pages/vkp-scrbl/utils.rkt") @title{Lab 1} @bold{Goals:} The goals of this lab are to get familiar with our work environment: the Eclipse IDE, the WebCAT submission, the basics of running a program in Java-like languages, and program testing framework. In the second part of the lab, (the one that really teaches you something) will focus on data definitions and examples in a simple variant of Java language. @section{Introduction} We start with designing data - designing classes of data that are connected to each other in a systematic way, showing that the @emph{Design recipe for Data Definitions} can be used virually without change in a completely different language than we have used in the first part. 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 @emph{Design Recipe} we have seen before. The only difference here is that for classes that represent a union type (for example classes @tt{Circle} and #tt{Rectangle} that are both a part of the union type @tt{Shape}, the conditional statement used in @tt{DrRacket} inventory/template is replaced by the @bold{dynamic dispatch} into the class whose constructor has been used to define the specific object. @section{Eclipse IDE} @bold{Eclipse} is an integrated (program) developmnet environment used by many professional Jav programmers (as well as programmers the use other programming languages). It is an @emph{Open Source} product, which means anyone can use it freely and anyone can contribute to its development. The environment provides an editor, allows you to organize your work into several files that together comprise a project, and has a compiler so you can run your programs. Several projects form a workspace. You can probably keep all the work till the end of the semester in one workspace, with one project for each programming problem or a lab problem. There are several step in getting started: @itemlist[ @item{Learn to set up your workspace and launch an Eclipse project.} @item{Learn to manage your files and save your work.} @item{Learn how to edit your Java programs and run them, using our tester library.}] @subsection*{Learn to set up your workspace.} @itemlist[ @item{Start working on two adjacent computers, so that you can use one for looking at the documentation and the other one to do the work. Find the web page on the documentation computer: @link["http://howto.ccs.neu.edu/howto/accounts-homedirs/logging-into-ccis/"]{http://howto.ccs.neu.edu/howto/accounts-homedirs/logging-into-ccis/} and follow the instructions to log into your Windows/Unix account on the work computer.} @item{Next, set up a workspace folder in your home directory where you will keep all your Java files. This should be in @tt{z:\\...\EclipseWorkspace"} Note that z: is the drive that Windows binds your UNIX home directory.} @item{Next, set up another folder in your home directory where you will keep all your Java library files. This should be in @tt{z:\\...\EclipseJars} We will refer to these two folders as @emph{EclipseWorkspace} and @emph{EclipseJars}. Make sure the two folders @emph{EclipseWorkspace} and @emph{EclipseJars} are subfolders of the same folder.} @item{Start the Eclipse application. @bold{DO NOT check the box} that asks if you want to make this the @bold{default workspace} for @bold{Eclipse} if you are working on the lab computer. If you are working at home or using your laptop, you may want to make the selected workspace to be your default.} @item{Working at home: If your home computer does not have Java compiler installed, please, first install the Jaava 1.6... compiler, than install Eclipse. You may ask one of the tutors to help you.}] @subsection*{The First Project} @itemlist[ @item{@bold{Download the libraries we will use.} The libraries you will need are: @link["http://www.ccs.neu.edu/home/vkp/2510-sp14/Labs/tester.jar"]{tester.jar} @link["http://www.ccs.neu.edu/home/vkp/2510-sp14/Labs/javalib-1.0.3.jar"]{javalib.jar} When saving the downloaded file, the dialog asks you @emph{Do you want to open or save this file}. Choose @emph{save}. It then comes up with a @emph{Save as} window. Browse to find your @emph{EclipseJars} folder and on the bottom where it says@emph{Save as} type instead of @emph{WINRAR} archive choose @emph{All Files}. Do this for all Java libraries, otherwise Windows messes up the file.} @item{@bold{Create a project.} In the @emph{File} menu select @emph{New} then @emph{Java Project}. In the window that appears in the @emph{Project layout} section select @emph{Create separate folders for sources and class files} and select @emph{Next}. We assume you have named it @emph{MyProject}.} @item{@bold{Make the libraries available to the new project.} Highlight your @emph{Project} in the @emph{Project Explorer} window, then select in the @emph{Project} tab on the top the @emph{Preferences}. In the @emph{Java Settings} pane select the @emph{Libraries} tab. On the right click on @emph{Add External JARs...} The @emph{file chooser window} will be shown. Navigate to your @emph{EclipseJars} folder and select all @emph{jar} files you have downloaded. Hit Finish.} @item{@bold{Add the @emph{Shapes.java} file to your project.} @itemlist[ @item{Download the file @link["Shapes.java"]{Shapes.java} to a temporary directory or the desktop.} @item{@emph{In Eclipse} highlight the @emph{src} box under the @emph{MyProject} in the @emph{Package Explorer} pane. Note: If the pane is not visible, go to @emph{Window} menu, select @emph{Show View...} then @emph{Package Explorer}. You should also select @emph{Show View... Outline}.} @item{In the @emph{File} menu select @emph{Import....} Choose the @emph{General} tab, within that @emph{File System} and click on @emph{Next}.} @item{Browse to the temporary directory that contains your @emph{Shapes.java} file. Click on the directory on the left, then select the @emph{Shapes.java} file in the right pane and hit @emph{Finish}.}]} @item{@bold{View and edit the file @tt{Shapes.java}.} @itemlist[ @item{Click on the @emph{src} block under @emph{MyProject} in the @emph{Pacakage Explorer} pane. It will reveal @emph{default} package block.} @item{Click on the @emph{default} package block. It will reveal @emph{Shapes.java}.} @item{Double click on @emph{Shapes.java}. The file should open in the main pane of @emph{Eclipse}. You can now edit it in the usual way. Notice that the @emph{Outline} pane lists all classes defined in this file as well as all fields and methods. It is almost as if someone was building our templates for us.} @item{@bold{Adjust the @emph{Eclipse} settings.} The TAs will guide you through setting that will convert all tabs into spaces, and will show you how to set the editor to show you the line numbers for all lines in the code. The @bold{Code Style} section also explains this.}]}] Add examples of data needed to display the following image (ignore the colors): @image[#:scale 1.0 #:suffixes '(".png")]{car.png} @subsection*{Set up the run configuration and run the program.} @itemlist[ @item{Highlight @emph{MyProject} in the @emph{Package Explorer} pane.} @item{In the @emph{Run} menu select @emph{Run Configurations...}.} @item{In the top left corner of the inner pane click on the leftmost item. When you mouse over it should show @emph{New launch configuration}.} @item{Select the name for this configuration - usually the same as the name of your project.} @item{In the @emph{Main class:} click on @emph{Search...}.} @item{Among @emph{Matching items} select @emph{Main - tester} and hit @emph{OK}.} @item{Click on the tab @emph{(x)= Arguments}. In the @emph{Program arguments} text field enter @tt{ExamplesShapes}. Later, when you define your own program, you will use the class name of your @tt{Examples...} class instead of @tt{ExamplesShapes}.} @item{At the bottom of the @emph{Run Configurations} select @emph{Apply} then @emph{Run}. Next time you want to run the same project, make sure @emph{Shapes.java} is shown in the main pane (and is currently selected), then hit the green circle with the white triangle on the top left side of the main menu.}] @subsection*{Simple Data Definitions} @bold{Problem 1} Here is a data definition in DrRacket: @verbatim{ ;; to represent a person ;; A Person is (make-person String Number String) (define-struct (person name age gender)) (define tim (make-person "Tim" 20 "M")) (define pat (make-person "Pat" 19 "F")) (define kim (make-person "Kim" 17 "F")) (define dan (make-person "Dan" 22 "M")) } Draw the class diagram that represnts this data. Define the class @tt{Person} that implements this data definition and the class @tt{ExamplesPerson} that contains the examples defined above. Run your program to make sure it works. @subsection*{Data Definitions with Containment} @bold{Problem 2} Modify your data definitions so that for each person we also record the person's address. For person's address we only record the city and the state. @itemlist[ @item{Draw the class diagram for this data definition} @item{Define Java classes that represent this data definition.} @item{Pat and Kim live in Boston, MA; Tim lives in Warvick, RI, and Dan lives in Nashua, NH. Make examples of data and add two more people.}] @subsection*{Data Definitions for Unions of Data} @bold{Problem 3} A deli menu items include soups, salads, and sandwiches. Every item has a name and a price (in cents - so we have whole numbers only). For each soup and salad we note whether it is vegetarian or not. Salad also specifies the dressing. For a sandwich we note the kind of bread, and two fillings (e.g peanut butter and jelly; or ham and cheese). Assume that every sandwich will have two fillings, and ignore extras (mayo, mustard, tomatoes, lettuce, etc.) Define classes to represent the deli menu and make examples with at least two soups, two salads, and two sandwiches. @subsection*{Self-Referential Data} The @emph{HtDP} book includes the data definition for @emph{Ancestor Trees}: @verbatim{ ;; An Ancestor Tree (AT) is one of ;; -- unknown ;; -- (make-tree Person AT AT) A Person is (make-person String String) (define-struct (person name eyecolor)) } Convert this data definition into Java classes and interfaces. Make examples of ancestor trees that in at least one branch cover at least three generations. ß