Lab 1
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.
Finally, you will make sure you can use the WebCAT submission server correctly.
All the files you will need are in this one Lab1.zip file. You can download it once and extract from it the files you need later on.
Partners
Find a partner to work with during the labs and on your homework assignments.
1 Introduction
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.
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.
2 Eclipse IDE
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.
2.1 Learn to set up your workspace.
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:
http://howto.ccs.neu.edu/howto/accounts-homedirs/home-directory-access-on-linux-and-windows/
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
z:\\...\EclipseWorkspace"
Note that z: is the drive that Windows binds your 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
z:\\...\EclipseJars
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 Jaava 1.6... compiler, than install Eclipse. You may ask one of the tutors to help you.
2.2 The First Project
Download the libraries we will use.
The library you will need to run your tests 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 saysSave as type instead of WINRAR archive choose All Files. Do this for all Java libraries, otherwise Windows messes up the file.
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.
Hit Finish.
Add the Shapes.java file to your project.
Download the file Shapes.java to a 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 file Shapes.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.
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 also explains this.
You are now ready to read the program, to make changes, and save them. However, to run the program, you need to explain to Java more details:
2.3 Set up the run configuration and run the program.
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.
The program displays the data you have defined in the ExamplesShapes class and runs the tests defined there.
3 Simple Data Definitions
We will now look at the code to make sure we understand every part, make changes and additions, and run the program again.
The program consists of three class definitions: the definition of the class Circle, the class CartPt, and the class ExamplesShapes.
The description of the class definitions for the class Circle and the class CartPt is shown as a class diagram. The class diagram consists of the name of the class and a list of fields. For each field it gives us the name (identifier) we will use to refer to that field and the type of data that this field will represent.
Based on this class diagram we could make similar data definitions in DrRacket:
;; to represent a cartesian point in a plane |
;; A CartPt is (make-cart-pt Number Number) |
(define-struct cart-pt (x y)) |
|
;; to represent a circle |
;; A Circle is (make-circle CartPt Number String) |
(define-struct circle (loc rad color)) |
Our examples of data would be:
(define p50x50y (make-posn 50 50)) |
(define p20x40y (make-posn 20 40)) |
(define p30x40y (make-posn 30 40)) |
|
(define circle1 (make-circle (make-posn 50 50) 50 "red")) |
(define circle2 (make-circle (make-posn 20 40) 10 "green")) |
(define circle3 (make-circle (make-posn 30 40) 20 "blue")) |
and the tests would be:
(check-expect circle1 (make-circle p50x50y 50 "red")) |
(check-expect circle2 (make-circle p20x40y 10 "green")) |
(check-expect circle3 (make-circle p30x40y 20 "blue")) |
(You can download this code and run it if you wish.)
3.1 Understanding the data
The arrow in the class diagram that goes from the fiels loc to the class diagram for the class CartPt is called the containment arrow. It tells us that the value of this field is an instance of the class CartPt.
Look at the output for your program:
--------------------------------- |
Tests for the class: ExamplesShapes |
Tester Prima v.1.5.2.1 |
----------------------------------- |
Tests defined in the class: ExamplesShapes: |
--------------------------- |
ExamplesShapes: |
--------------- |
|
new ExamplesShapes:1( |
this.p50x50y = |
new CartPt:2( |
this.x = 50 |
this.y = 50) |
this.p20x40y = |
new CartPt:3( |
this.x = 20 |
this.y = 40) |
this.p30x40y = |
new CartPt:4( |
this.x = 30 |
this.y = 40) |
this.circle1 = |
new Circle:5( |
this.loc = |
new CartPt:6( |
this.x = 50 |
this.y = 50) |
this.rad = 50 |
this.color = "red") |
this.circle2 = |
new Circle:7( |
this.loc = |
new CartPt:8( |
this.x = 20 |
this.y = 40) |
this.rad = 10 |
this.color = "green") |
this.circle3 = |
new Circle:9( |
this.loc = |
new CartPt:10( |
this.x = 30 |
this.y = 40) |
this.rad = 20 |
this.color = "blue")) |
--------------- |
|
Ran 3 tests. |
All tests passed. |
|
--- END OF TEST RESULTS --- |
It shows you the data defined in the class ExamplesShapes using the field names we have selected.
It also runs the tests and reports that all tests passed.
Make a change in one of the tests and see how the error will be reported.
3.2 On your own
Add to this program the definition of the class rect to represent a rectangle at some location in the plane with the given width and height and again a color given as a String.
Add tests in a manner similar the what has been done for the examples of circles.
Run your program to make sure it works.
4 WebCAT: Homework Submission Server
We now want you to submit your work as Assignment 0 to the Web-CAT server.
Follow the link to our Web-CAT server and log in.
Find Assignment 0 and upload the file Shapes.java that we had worked on during the last lab.
Check that the file has been submitted correctly.