Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

1. Writing Simple Java Classes (45 min)

First steps

...

...

  1. If you have not done it already, download the latest DrJava to your Desktop. Use this link

...

  1. .

...

...

  1. Create a

...

  1. lab06 directory

...

4. The following is due to a bug in the current DrJava and will not be necessary in the future:

  • Copy your entire Comp211 directory to the C:\temp directory -- DrJava will not run properly when the files are saved to your U: drive.
  • At the end of lab, copy your Comp211\lab02 directory back to your U: drive and delete it off of the C: drive!

...

  1. within your Comp211 directory.
  2. Set your DrJava "Language Level" to "Elementary"

...

  1. .  The Elementary language level simplifies the real Java syntax to facilitate the transition from Scheme to Java.
  2. Create

...

  1. a class to calculate

...

  1. the areas of rectangles in DrJava

...

  1. by following the steps below. We will begin the process of writing a class that

...

  1. represents a rectangle given

...

  1. its width and

...

  1. 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.
    1. Now we are ready to add to Rectangle

    The purpose of this step is to give you some idea of what DrJava is doing "behind the scene". You do not have to concern yourself with all the code that is automatically generated at all.

    ...

    1. a method to compute the area of a rectangle.

    ...

    1. Change the definition of the Rectangle class to read:
      Code Block
      
      class 

    ...

    1. Rectangle {

    ...

    1. 
          double

    ...

    1.  width;
          double height

    ...

    1. ;
          double area() { 

    ...

    1. return width * height;

    ...

    1.  } // The only new 

    ...

    1. line
      }
      

    ...

    • Compile All: the

    ...

    • Rectangle file is automatically saved and recompiled.

    ...

    • To create

    ...

    • an instance of

    ...

    • class Rectangle, type the following in the Interactions pane:
      Code Block

    ...

    • 
      Rectangle 

    ...

    • r = 

    ...

    • newRectangle(4,5);
      
      Note: the "Compile All" command resets the Interactions pane erasing all extant
      definitions of names bound to objects. So when we compiled our new definition for the Rectangle class,
      we destroyed our first binding of r in the Interactions pane.
    • To "ask" the

    ...

    • Rectangle object

    ...

    • bound to r2 to compute

    ...

    • its area, type

    ...

    • Code Block

    ...

    • 
      r2.

    ...

    • area(

    ...

    • )

    ...

    • ;
      
      in the Interactions pane.
    • Experiment with

    ...

    • the

    ...

    • DrJava Interactions pane to create some new rectangles and computer their areas.

    Defining Right Triangles and Circles

    Suppose we want to define a class representing a right triangle given the length of the two sides forming the right angle, which we call base and

    12. How do you instantiate another AreaCalc and name it robot?

    Computing the area of a right triangle

    Suppose you want to compute the area of a right triangle given its base and its height . What code should you we write? You basically have two options.

    13. Write another class called AreaCalc2 with a method to compute the area of a right triangle given as input parameters the base and the height.

    Use the DrJava New command to
    create a new document. In this document define a class called RightTriangle with fields of type double called base and height, and a method

    Code Block
    
    double area()
    

    14. Add a method to class AreaCalc to compute the area of a this right triangle given as input parameters the base and the height. Save and compile this file. Interact with it in the Interactions pane.

    • Now

    ...

    • suppose

    ...

    • we are asked to define a class that can compute the area of a circle.

    ...

    • How would you

    ...

    Creating classes with fields

    The following are classes with fields similar to class Entry discussed in lecture #18.

    15. Write a class called Rectangle that has two double fields, width and height, and a method to compute the area.

    16. Write a class called RightTriangle that has two double fields, base and height, and a method to compute the area.

    ...

    • do it?
      Write a class called Circle that has one double field, radius, and a method to compute the area.

    2. Unit Testing with JUnit (35 min)

    ...

    Each time a function (or procedure or object method) is modified, we require that it must pass passes its existing test codesuite. Each time a test code for a function (or procedure or object method) is modified, the corresponding function (or procedure or object method) may have to be revised to satisfy the new specification. Tests and code must be developed in tandem.

    JUnit (http:-www.junit.org) (www.junit.org) is an open source framework developed to support the above concept of unit testing for Java programs. This tutorial will lead you through a simple example of how to write unit test code in developing a (simple) Java program with only one class.

    ...

    Suppose we want to model the notion of a smart person who knows his/her birthday and can compute the number of months till his/her next birthday given the current month. For our purpose, a month can simply be represented by an integer, which in Java is called int. We start by writing "stub" code for the class Person that is to represent our smart person.

    Code Block
       
    class Person {
    
        /**
        * Computes the number of months till the next birthday given the current month.
        */
        int nMonthTillBD(int currentMonth) {
            // to dotodo
        }
    
    }
    

    Notice in the above there is really no concrete code. As a matter of fact, the above would not compile. Now we must abandon everything and start writing test code for nMonthTillBD(...).

    ...

    • 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 classtestcaseclass.
       * Every method starting with the word "test" will be called when running
       * the test with JUnit.
       */
      class Test_Person extends TestCasePersonextendsTestCase {
          /**
           * A test method.
           * (Replace "X" with a name describing the test.  You may write as
           * many "testSomething" methods in thisinthisclass class as you wish, and each
           * one will be called when running JUnit over this classoverthisclass.)
           */
          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.

    ...

    • Change the name of the stub method testX() in the above to test_nMonthTillBD()and add code to make it look like the code below.
      Code Block
      class Test_PersonPersonextendsTestCase extends TestCase {
      
          void test_nMonthTillBD() {
      
              Person peter = new PersonnewPerson(9); // a person born in September.
      
              assertEquals("Calling nMonthTillBD(2).", 7, peter.nMonthTillBD(2));
              assertEquals("Calling nMonthTillBD(9).", 0, peter.nMonthTillBD(9));
              assertEquals("Calling nMonthTillBD(12).", 9, peter.nMonthTillBD(12));
          }
      } 
      
      Note that in the code for test_nMonthTillBD(), we have decided that we only need to know the birth month of a person in order to compute the number of months till the next birth day. As a result, we instantiate a Person object by passing only the birth month: new Person(9).

    ...

    • the current month is less than the birth month
    • the current month is equal to the birth month
    • the current month is greater than the birth month.

    The

    Code Block
    
    assertEquals

    method comes from the class TestCase and takes in three parameters:

    ...

    Now add the statement return 0; to the body of the method nMonthTillBD(...) and compile.

    Code Block
    class Person {
        int _bMonth;
    
        /**
        * Computes the number of months till the next birthday.
        */
        int nMonthTillBD(int currentMonthintcurrentMonth) {
            return 0return0; // to dotodo
        }
    }
    

    Step 4.

    After you have cleaned up your code for Person as shown in the above, you should be able to compile Test_Person. With Test_Person open, click on the Test button in DrJava tool bar.

    ...

    Change the formula to

    Code Block
        int nMonthTillBD(int currentMonth) {
            return _bMonth - currentMonth; // to dotodo
        }
    

    Compile all and test again.

    ...

    Change the formula to

    Code Block
        int nMonthTillBD(int currentMonth) {
            return (_bMonth - currentMonth + 12) % 12; // to dotodo
        }
    

    Compile all and test again. Now everything should pass! You may now remove the TO DO comment from the code of nMonthTillBD.

    ...

    Lecture notes on Object-Oriented Programming using Java by Dung X. Nguyen and Stephen B. Wong: http://cnx.org/content/col10213/latestImage Removed
    Here is another link for a more detailed discussion of JUnit testing uisng DrJava: http://cnx.rice.edu/content/m11707/latest/Image Removed
    The following link contains a more elaborate discussion on JUnit testting and a more involved example of testing: http://junit.sourceforge.net/doc/testinfected/testing.htmImage Removed.

    ...

    Access Permissions: (Please don't edit)

    ...