©2009 Felleisen, Proulx, et. al.
In the first part of this lab you will learn how to work in a commercial level integrated development environment IDE Eclipse, using the standard Java programming language. 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 the transition from ProfessorJ:
Learn how to convert ProfessorJ programs to programs that run in Java, using the tester library.
Learn to set up your workspace and launch an Eclipse project.
Learn to manage your files and save your work.
Learn the basics of the use of visibility modifiers in Java.
Learn the basics of writing test cases using the tester library.
The programs you have written so far follow the specification for the full Java language, with two exceptions:
The test cases in ProfessorJ use the special form check — expect that is not available in Java. Instead, we provide the tester library that allows you to write the tests in a similar way. The tester library reports on the failed test cases and provides a display of all data defined in our Examples class.
When a class implements an interface which includes method declarations, every method definition in the class that implements a method declared in the interface must be annotated with the public visibility modifier.
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://www.ccs.neu.edu/howto/howto-windows-n-unix-homedirs.html
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.
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.
Download the libraries we will use. The libraries you will need are available at a public web site at:
http://www.ccs.neu.edu/javalib/
Go to the Downloads folder and download the following libraries into your EclipseJars folder:
tester.jar
draw.jar
geometry.jar
colors.jar
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.
In the Java Settings pane select the Libraries tab.
On the right click on Add External JARs...
You will get a chooser window. Navigate to your EclipseJars folder and select all jar files you have downloaded.
Hit Finish.
Add the BlobWorld.java file to your project.
Download the file BlobWorld.java to a temporary directory.
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 BlobWorld.java file.
Click on the directory on the left, then select the BlobWorld.java file in the right pane and hit Finish.
View and edit the Java file.
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 BlobWorld.java.
Double click on BlobWorld.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.
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.
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.
At the bottom of the Run Configurations select Apply then Run.
Next time you want to run the same project, make sure BlobWorld.java is shown in the main pane, then hit the green circle with the white triangle on the top left side of the main menu.
Create a new project named Bookstore. Download the file Bookstore.java, use it as the file in your Bookstore project. Follow the same steps as before and run the program.
Now modify your file Bookstore.java adding two more examples of books to the Examples class. Run your program.
You can create an archive of your project by highlighting the project, then choose Export then select Zip archive. Eclipse will ask you for a folder where to place the zip file and will let you choose the name for the zip file.
Your project will remain in the Eclipse workspace, but now you have saved a copy that will not change as you keep working.
This is also the file that you will be submitting as your homework.
In the class Blob modify the method moveBlob so that the blob changes the color to black when the user hits the B key.
Modify the tests for the method to the Examples class, following the technique already illustrated there.
In the Tester page of the javalib web site click on User’s Guide. Use it as a guide for how to design test using the tester library. Explore the javalib site for additional information.
Include in your program a couple of test that you know will fail and observe how the errors are reported.
In this part of this lab you will practice the use of constructors in assuring data integrity and providing a better interface for the user.
We start with the Date class we may use to check for overdue books.
// to represent a calendar date class Date { int year; int month; int day; Date(int year, int month, int day){ this.year = year; this.month = month; this.day = day; } }
and a simple set of examples:
class Examples { Examples() {} // good dates Date d20060928 = new Date(2006, 9, 28); // Sept 28, 2006 Date d20071012 = new Date(2007, 10, 12); // Oct 12, 2007 // bad dates Date b34453323 = new Date(3445, 33, 23); }
Create a project Date in the Eclipse and add a new file named Examples.java. Copy into this file the definition of the class Date and the class Examples.
Import the tester library and add the tester.jar to the project as external JAR. Now run the project.
Look at the third example of a date.
Of course, the third example is pure nonsense. Only the year is possibly valid - still not really an expected value. To validate the date completely (taking into account all the special cases for different months, as well as leap years, and the change of the calendar at several times in the history) is a project on its own. For the purposes of learning about the use of constructors, we will only make sure that the month is between 1 and 12, the day is between 1 and 30, and the year is between 1000 and 2200.
Did you notice the repetition in the description of the valid parts of the date? This suggests, we start with the following methods:
method validNumber that consumes a number and the low and high bound and returns true if the number is within the bounds (inclusive).
methods validDay, validMonth, and validYear designed in a similar manner.
Design at least one of these methods - you can finish the others at home.
Once you have done so, change the constructor for the class Date as follows:
Date(int year, int month, int day){ if (this.validYear(year)) this.year = year; else throw new IllegalArgumentException("Invalid year in Date."); if (this.validMonth(month)) this.month = month; else throw new IllegalArgumentException("Invalid month in Date."); if (this.validDay(day)) this.day = day; else throw new IllegalArgumentException("Invalid day in Date."); }
This example show you how you can signal errors in Java. The class IllegalArgumentException is a subclass of the RuntimeException. Including the clause
throws new ...Exception("message");
in the code causes the program to terminate and print the specified error message. Later we will learn how we can customize the error reporting and also how to respond to errors without terminating the program execution.
Make additional examples with invalid day, invalid month, and invalid year. Run the program, then comment out one invalid example at a time, to see that all checks work correctly.
When entering dates in the current year it is tedious to always have to enter 2009. We can make avoid the need to type in the year by providing an additional constructor that requires the user to give only the day and month and assumes that the year is the current year (2009 in our case).
Remembering the single point of control rule, we make sure that the new overloaded constructor defers all of the work to the primary full constructor:
Date(int month, int day){ this(2009, month, day); }
Add examples that use only the month and day to see that the constructor works properly. Include examples with invalid month or year as well. (Of course, you will have to comment them out.)
The user may want to enter the date in the form "Oct 20 2009". To make this possible, we can add another constructor:
Date(String month, int day){ this(1, day); // make an instance with a wrong month if (month.equals("Jan")) this.month = 1; else if ... else throw new IllegalArgumentException("Invalid month in Date."); }
To check that it works, allow the user to enter only the first three months ("Jan", "Feb", and "Mar"). The rest is tedious, and in a real program would be designed differently.
Finish the work at home and save it as a part of your portfolio.