0. Executive Summary

This is the second graded lab for CS2102.

1. Stated Aims

  1. Learn how to write a class from scratch
  2. Learn how to develop JUnit test case(s) to test the newly-defined class.
  3. Learn how to modify an existing class

2. Background knowledge

  1. Understand concept of class
  2. Understand instance variable
  3. Understand instance method

3. Setup

If you are in the Lab, perform the routine KH 202 setup or AK 120D setup; for now, ignore the "advanced setup" options. If you would like to set up  Eclipse on your personal computer, then (a) install Java JDK 1.5 if you need to as described in tools section (here); and (b) install Eclipse as described in tools section (here).

Make sure you have read pp. [177-194] from the textbook.

4. Individual Tasks

4.1 Design a Class Precinct

A Precinct represents "the lowest-level minor civil division in the United States". Each precinct has a name (i.e., "K12") an address where voters can vote (i.e., "130 Winter Street") and a total number of voters in the precinct (i.e., 673).

Now consider how instances of this class are to be used. Let's write a small program to create a new Precinct object and assign it some relevant information.

Precinct pre;

pre = new Precinct();
pre.name = new String ("K12");
pre.address = new String ("130 Winter Street");
pre.population = 673;

To complete your class, add three methods:

Now go back to your main method in PrecinctProgram, and update it to use these instance methods:

Precinct pre;

pre = new Precinct();
pre.name = new String ("K12");
pre.address = new String ("130 Winter Street");
pre.population = 673;

System.out.println ("Precinct Info:" + pre);

pre.add (20);

System.out.println ("Precinct Info:" + pre);

This "test" program is not going to be completely suitable for our purposes. We need something more formal and repeatable. The next task accomplishes this.

4.2 Design the JUnit test case

We will see how we use testing to validate each of our designs as we proceed. By starting testing early, we avoid numerous complications that occur because we either (a) run out of time to test; or (b) forget to test key units.

First, to become familiar with JUnit, follow the small example posted on the course tools page. In this tools page, you will create your first JUnit test case, to validate the add method.

For this lab, you are to:

5. Follow-up Tasks

Your goal is to turnin the assignment file(s) (Precinct.java and PrecinctTest.java) by November 8th at 11:59 PM. The name of the assignment you should use is "lab2".

6. Change History

Date Reason/Change
11/07/2006 Ready to go.
11/06/2006 Revised with more details (not complete, yet).
10/18/2006 Initial Page