Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. If you have not done it already, download the latest DrJava to your Desktop. Use this link.   Download the "latest developement" release!
  2. Create a lab06 directory within your Comp211 directory.
  3. Set your DrJava "Language Level" to "Functional" (aka "Elementary").  The Elementary language level simplifies the real Java syntax to facilitate the transition from Scheme to Java.
  4. Create a class to calculate the areas of rectangles in DrJava by following the steps below. We will begin the process of writing a class that represents a rectangle given its width and height. We will write the class in several small steps. At each step, we will compile the code to ensure that everything is syntactically correct. By compiling the code at each small step, we hope to avoid seeing a large number of error messages that can be rather intimidating.
    • In the Definitions pane (upper right pane), type the following:
      Code Block
      class Rectangle {
          double width;
          double height;
      }
      
      Note the placement of the curly braces: the opening brace is on the same line as the class name, while the closing brace lines up with the beginning of the class definition on a new line. This is the de-facto Java coding style. The two lines beginning with double define fields within the class, much like the fields of a structure in Scheme.
      The word double is a the name for the principal real number type in Java. So the width and height fields of a Rectangle have type double. The Rectangle class is not very interesting because we cannot do much with it. However, it is syntactically correct and we can compile it.
      Aside: The equivalent Scheme code for class Rectangle is something like:
      Code Block
      define-struct Rectangle(width height)
      
    • Save the file in your Comp211/lab06 folder under the default name Rectangle. The file will be saved as a .dj0 file, which is a DrJava Elementary level file.
    • Click the "Compile All" button in DrJava. The file should compile with no errors.
    • In the Interactions pane (bottom pane), type the following:
      Code Block
      Rectangle r = new Rectangle(5, 10);
      
      This code defines a variable called r of type Rectangle and binds it to a new object belonging to class Rectangle.
    • There is not much we can do with the Rectangel class yet, but we can print the string representations of Rectangle objets. In the Interactions pane, type r and hit Enter. This action will print the string representation of object bound to calc. In the DrJava Elementary language level, the string representation of the Rectangle object r is {"Rectanlge(5.,10.)"}}. DrJava automatically strips the quotation marks off string representations when it prints them. Conventional Java would return a much more cryptic string representation for a Rectangle object.
    • Take a look at your Comp211/lab07 directory and you will see several new files. The compiler automatically created a file called Rectangle.class containing the compiled cJava bytecode ready execution on the Java Virtual Machine (JVM). The DrJava Interactions Pane is a convenient user interface to a Java Virtual Machine. When you refer to a Java class in the Interactions pane, DrJava automatically loads the byte code for that class. When you compile the file Rectangle.dj0 using DrJava, DrJava generates a corresponding conventional Java source file called Rectangle.java and invokes the Java compiler to translate this Java source file to bytecode.
  5. Now we are ready to add to Rectangle a method to compute the area of a rectangle. Change the definition of the Rectangle class to read:
    Code Block
    class Rectangle {
        double width;
        double height;
    
        // The only new lines:
        double area() {
           return width * height;
        }
    }
    

...

  • Go to the File menu and select New Junit Test Case...
  • Create a stub test case called Test_Person. DrJava will create a stub class that looks like the following
    Code Block
    /** * A JUnit test case class.
     * Every method starting with the word "test" will be called when running
     * the test with JUnit. */
    class Test_Person extends TestCase {
      /** * A test method.
       * (Replace "X" with a name describing the test. You may write as
       * many "testSomething" methods in this class as you wish, and each
       * one will be called when running JUnit over this class.)
       */
      void testX() {
      }
    }
    
    At this point, do not worry about what "extends TestCase" means in the above. DrJava is re-using the class TestCase from the JUnit framework in some clever way without you having to deal with all the details of how to use an existing class from some other files.

At If you have the "stable release, v. 20100913-r5387, you may receive a compile error.  To remedy this,  at the top of the Test_Person file, add the line

import junit.framework.TestCase;

Or simply download and replace with a later version, e.g. the latest development version.

  • Compile this test class only (by clicking on Tools/Compile current document), and save the class in the same directory as Person. The Test_Person class should compile, though the Person class does not.

...