©2009 Felleisen, Proulx, et. al.

5  Starting in Eclipse; Understanding Constructors

5.1  Eclipse IDE and the tester library

Goals

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:

  1. Learn how to convert ProfessorJ programs to programs that run in Java, using the tester library.

  2. Learn to set up your workspace and launch an Eclipse project.

  3. Learn to manage your files and save your work.

  4. Learn the basics of the use of visibility modifiers in Java.

  5. Learn the basics of writing test cases using the tester library.

ProfessorJ vs Java.

The programs you have written so far follow the specification for the full Java language, with two exceptions:

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://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.

The First Project

  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. 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.

On Your Own: Learn to edit and save your work.

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.

Learn to edit the program and design the test cases.

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.

Designing tests using the Tester test harness

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.

5.2  Understanding Constructors: Data Integrity; Signaling Errors

Goals

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.

Designing constructors to assure integrity of data.

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); 
  }

Overloading constructors to provide flexibility for the user: providing defaults.

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.)

Overloading constructors to provide flexibility for the user: expanding the options.

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.

Last modified: Wednesday, October 7th, 2009 10:37:46pm