©2008 Felleisen, Proulx, et. al.

6  Starting in Eclipse; Understanding Constructors

6.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 Java 1.5 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 to set up your workspace and launch an Eclipse project.

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

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

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

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

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:\\eclipse\workspace

Note that z: is the drive that Windows binds your UNIX home directory.

Start the Eclipse application.

DO NOT check the box that asks if you want to make this the default workspace for eclipse

Starting a new Project

Learn to edit and save your work.

First, modify your file Examples.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.

Visibility modifiers in Java.

Notice that the Examples class definition starts with the word public. Java requires that exactly one class or interface in each file is declared as public, and the name of the file must match the name of this public interface or class.

The public keyword represents the visibility modifier that informs the Java compiler about the restrictions on what other programs may refer to the particular classes, fields, or methods. We will learn about these visibility modifiers soon.

Learn to edit the program and design the test cases.

Change the class Book so that contains the information about the date when the book is due to be returned to the library. Of course, you need to add the Date class.

Design the method isOverdue that determines whether the book is overdue on a given day. Assume throughout that each month has 30 days and there are no leap years.

Add tests for the method to the Examples class, following the technique already illustrated there.

Designing tests using the Tester test harness

The Examples class is very similar to what we have seen in ProfessorJ. let us look at the differences:

6.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 used 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:

public class Examples {
  public 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 2008. 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 (2008 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:

  public Date(int month, int day){
    this(2008, 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 2006". To make this possible, we can add another constructor:

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

6.3  Converting a larger program to an Eclipse project

When the program gets larger, we no longer want to keep all class definitions in one file. Typically, in Java every class or interface is defined in its own file, though at times we may group together related classes and interfaces.

We are almost done. The only remaining task is to convert the test cases from ProfessorJ to tests managed by the tester library.

Here is an example of how it works. The original tests for the method count were defined as:

    // * Count Examples
    boolean countTests = 
        ((check this.mtlos.count() expect 0) &&
         (check this.list1.count() expect 1) &&
         (check this.list2.count() expect 3));

They translate to the tester tests as follows:

 // * Count Examples
 void countTests(Tester t){
     t.checkExpect(this.mtlos.count(), 0);
     t.checkExpect(this.list1.count(), 1);
     t.checkExpect(this.list2.count(), 3);
 }

Last modified: Tuesday, February 12th, 2008 2:48:43am