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.
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 ’expr’ and
if ’predicate’ 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 DrRacket inventory/template is replaced by the dynamic dispatch into the class whose constructor has been used to define the specific object.
Eclipse is an integrated (program) development environment used by many professional Java programmers (as well as programmers who use other programming languages). It is an 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:
Learn to set up your workspace and launch an Eclipse project.
Learn to manage your files and save your work.
Learn how to edit your Java programs and run them, using our tester library.
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:
and follow the instructions to log into your Windows/Unix account on the work computer.
Next, set up a workspace folder in your home directory where you will keep all your Java files. This should be in
Note that z: is the drive that Windows binds your own UNIX home directory.
Next, set up another folder in your home directory where you will keep all your Java library files. This should be in
We will refer to these two folders as EclipseWorkspace and EclipseJars. Make sure the two folders EclipseWorkspace and EclipseJars are subfolders of the same folder.
Start the Eclipse application.
DO NOT check the box that asks if you want to make this the default workspace for 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.
Working at home: If your home computer does not have Java compiler installed, please, first install the Java 1.6... compiler, than install Eclipse. You may ask one of the tutors to help you.
Download the libraries we will use.
The library you will need is:
When saving the downloaded file, the dialog asks you Do you want to open or save this file. Choose save. It then comes up with a Save as window. Browse to find your EclipseJars folder and on the bottom where it says Save as type instead of WINRAR archive choose All Files. Do this for all Java libraries, otherwise Windows messes up the file.
To follow the proper coding style for this class, you should have Eclipse insert spaces for indentation instead of tabs. This ensures that the code looks the same in every editor, regardless of the tab width settings. To do so, follow these instructions:
In Eclipse, select Preferences in the Window menu.
In the navigation pane on the left side of the Preferences dialog, drill down to Java > Code Style > Formatter.
Under Tab policy, select "Spaces only".
Rename the profile to something like "Mine" and click OK.
Create a project.
In the File menu select New then Java Project. In the window that appears in the Project layout section select Create separate folders for sources and class files and select Next. We assume you have named it MyProject.
Make the libraries available to the new project.
Highlight your Project in the Project Explorer window, then select in the Project tab on the top the Preferences.
In the Java Settings pane select the Libraries tab.
On the right click on Add External JARs...
The file chooser window will be shown. Navigate to your EclipseJars folder and select all jar files you have downloaded.
Add the Shapes.java file to your project.
Download the file Shapes.java to a temporary directory or the desktop.
Download the file ExamplesShapes.java to the same temporary directory or the desktop.
In Eclipse highlight the src box under the MyProject in the Package Explorer pane.
Note: If the pane is not visible, go to Window menu, select Show View... then Package Explorer. You should also select Show View... Outline.
In the File menu select Import....
Choose the General tab, within that File System and click on Next.
Browse to the temporary directory that contains your Shapes.java file.
Click on the directory on the left, then select the Shapes.java file in the right pane and hit Finish.
View and edit the files Shapes.java and ExamplesShapes.java.
Click on the src block under MyProject in the Pacakage Explorer pane. It will reveal default package block.
Click on the default package block. It will reveal Shapes.java.
Double click on Shapes.java. The file should open in the main pane of Eclipse.
You can now edit it in the usual way. Notice that the 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.
Adjust the Eclipse settings.
If you have not done so yet, do it now. 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 Code Style section has already explained this.
Add examples of data needed to display the following image (ignore the colors). The size of the canvas (and the blue background) is 100 x 100.
Highlight MyProject in the Package Explorer pane.
In the Run menu select Run Configurations....
In the top left corner of the inner pane click on the leftmost item. When you mouse over it should show New launch configuration.
Select the name for this configuration - usually the same as the name of your project.
In the Main class: click on Search....
Among Matching items select Main - tester and hit OK.
Click on the tab (x)= Arguments. In the Program arguments text field enter ExamplesShapes.
Later, when you define your own program, you will use the class name of your Examples... class instead of ExamplesShapes.
At the bottom of the Run Configurations select Apply then Run.
Next time you want to run the same project, make sure 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.
Here is a data definition in DrRacket:
;; 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 "Ann" 19 "F"))
(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 represents this data.
Define the class Person that implements this data definition and the class ExamplesPerson that contains the examples defined above.
Run your program to make sure it works.
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.
Draw the class diagram for this data definition
Define Java classes that represent this data definition.
Pat and Kim live in Boston, MA; Tim lives in Concord, NH; Ann lives in Concord, MA; and Dan lives in Nashua, NH.
Make examples of data and add two more people.
The Soup and Salad Deli menu items include soups, salads, and breads. 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. The customer can select either a cup or a bowl of soup.
Salad also specifies the dressing.
Breads can be gluten free or not.
Define classes to represent the deli menu and make sufficient number of examples so that you will order at least two different soups, two different salads, and two different breads. Start with the class diagrams - it will help you to see the whole design at once.
The youth soccer coach need to notify all players if the game is cancelled due to rain, and so she set up a phone chain. The coach calls the team captain who starts the phone chain by calling two other players on the team. Every player who receives the call then calls two other players (or one or none, if the player is at the end of the phone chain). This continues until all players have been called.
Here is the phone chain for the Astros team:
/ \ / \
Kim Pat Ann Joy
/ \ / \ / \ / \
Tay Zoe Meg Lou Cam Eve Tam EMPTY
/ \ / \ / \ / \ / \ / \ / \
;; An Phone Chain (PC) is one of
;; -- EMPTY
;; -- (make-link Player PC PC)
A Player is (make-player String String)
(define-struct (player name phone-num))
Convert this data definition into Java classes and interfaces. Make examples of several phone chains including the representation of the phone chain shown above.
For the classes that define a person with an address, design the method with the following header and purpose statement:
// is this person from the same city as the given person?
boolean sameCity(Person other)
Make sure you follow the rule one task – one method and design each method in the class that should be responsible for it.