...
1. Writing Simple Java Classes (45 min)
First steps
...
- If you have not done it already, download the latest DrJava to your Desktop. [Use this link!|http://drjava.org.}
- Create a
lab07
directory within your Comp211 directory. - Set your DrJava "Language Level" to "Elementary"
- 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:
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. This .Code Block class Rectangle { double width; double height; }
The two lines beginning withdouble
define fields within the class, much like the fields of a structure in Scheme.
The worddouble
is a the name for the principal real number type in Java. So thewidth
andheight
fields of aRectangle
have typedouble
. TheRectangle
class is not very interesting because we cannot do much with it. However, it is syntactically correct and we can
be compiledcompile it. - Save the file in your
Comp211/lab07
folder under the default nameRectangle
. he 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:
This code defines a variable calledCode Block Rectangle r = new Rectangle(5, 10);
r
of typeRectangle
and binds it to a new object belonging to class
Rectangle
. - There isn't is not much we can do with the
Rectangel
class yet, but we can print the string representations ofRectangle
objets. In the Interactions pane, typer
and hit Enter. This action will print the string representation of object bound tocalc
. In the DrJava Elementary language level, the string representation of theRectangle
objectr
which is {"Rectanlge(5.,10.)"}}. DrJava automatically strips the quotation marks off string representations when it prints them. Conventional Java would return something a much more cryptic string representation for
aRectangle
object. - Take a look at your
Comp211/lab07
directory and you will see several new files. The compiler automatically created a file calledRectangle.class
containing the compiled code expressed as "Java cJava bytecode " ready execution on the Java Virtual Machine (JVM). The DrJava automatically generated 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 fileRectangle.java
which we will ignore for nowdj0
using DrJava, DrJava generates a corresponding conventional Java source file calledRectangle.java
and invokes the Java compiler to translate this Java source file to bytecode.
- In the Definitions pane (upper right pane), type the following:
- Now we are ready to add to
Rectangle
a method to compute the area of a rectangle. Change the defintion definition of theRectangle
class to read:Code Block class Rectangle { double width; double height; double area() { return width * height; } // ThisThe lineonly is the only changenew line }
- Compile All: the
Rectangle
file is automatically saved and recompiled. - To create a new an instance of class
Rectangle
, type the following in the Interactions pane:
Note: the "Compile All" command resets the Interactions pane erasing all extantCode Block Rectangle r2r = new Rectangle(4,5);
definitions of names bound to objects. So when we compiled our new definition for theRectangle
class,
we destroyed our first binding ofr
in the Interactions pane. - To "ask" the
Rectangle
object bound tor2
to compute its area, type
in the Interactions pane.Code Block r2.area();
- Experiment with using the object calc to compute the area of a few rectangles of different sizes.
- How do you instantiate another
AreaCalc
and bind it to the namerobot
?
...
- 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 answerangle, which we call base
and height
. What code should we write? Write another 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
...
to compute the area of this right triangle. 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 do it?
Write a class calledCircle
that has one double field, radius, and a method to compute the area.
...
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.
...