Comp201: Principles of Object-Oriented Programming I
Spring 2006 -- Lab01:  Java Syntax Primer, Using DrJava    


DrJava is a lightweight pedagogical environment for Java development created by Rice University. DrJava provides a way to edit and save java code with key words highlighting, curly brace matching, and an interactive environment to manipulate objects and test code without having to write the main method. It can be freely downloaded from the web. Please see the DrJava home page.  

Remember that Java itself must be installed before DrJava can be installed.   To use the latest features in DrJava, be sure to install the latest version of Java.   Version 1.5 of Java can be downloaded from http://java.sun.com/j2se/1.5.0/download.jsp   Be sure to download and install the "JDK" (Java Development Kit) version without NetBeans.   Do not install just the "JRE" (Java Runtime Engine).

Editing

Definitions Pane: When you run DrJava you will see a window appear. This window (GUI) consists of four subwindows. The top half of the GUI constitutes the Definitions pane. You type in all the class definitions here. After you type in some code, you need to click on the save button before you can compile your code. All the classes in the Definitions pane will be saved in a single file. There should only be one public class in the Definitions window, and the saved file should have the same name as that of the public class with the extension .java.

Compiling

Compiler Output Pane: You compile your Java code by clicking on the Compile All button in the menu bar at the top. Every time you compile your code, DrJava will display all compile error messages here. Clicking on an error message will highlight the line where the error is suspected to take place in the Definitions pane. If there is no compile error, DrJava will declare success in this pane.

Running

Using  the Interactions pane: There are several ways to run your Java code. For now, we will restrict ourselves to the Interaction pane at the bottom of the main GUI window. This is where you can type in any valid Java statement. Usually, you would type in code to instantiate objects of classes defined in the Definitions window, and call their methods to check whether or not they perform correctly. Typing a valid Java expression terminated with a semi-colon and then pressing the Return (Enter) key, will cause DrJava to evaluate the expression but NOT printing the result. If you want DrJava to print the value of the result in the Interactions window, you should press Return without terminating the expression with a semi-colon. There is a menu item to reset (i.e. clear) the Interactions window. Another way to clear the Interactions window is to force a re-compile by editing the Definitions pane. If your code has printing statements, the output will be displayed in the Console Output pane.

To manually reset the Interactions pane: Normally, the interactions pane resets whenever anything is recompiled.  This erases all variable declarations that had been made.   The command history (up/down arrows) is preserved however, so that you can easily re-input any statements.   However, sometimes one needs to manually reset the Interactions pane.   This can easily accomplished by right clicking the Interaction pane and selecting "Reset Interactions".  The same menu item is available off the main menu under "Tools".  

 

Testing

There are many ways to test your code. The most formal way is to use JUnit testing facilities, which we will learn in the next lab. For now, you can test your code by interacting with it in the Interactions pane.

 


A Java Primer

For a more complete reference, see the Java Resources site.

Things to remember

  • Curly braces, "{" and "}", are used to delimit sections of code that belongs together.
  • All Java statements end in a semicolon.   That is, all lines of code end in a semicolon unless they end with a right curly brace, "}". 
  • Java is case-sensitive:   Moe is not the same a moe.
  • By convention, Java class names always begin with a capital letter.
  • By convention, variables always begin with a lower case letter unless they represent constant, unchanging values, where the name is usually in all capitals.

Declaring a variable:

Declaring a variable associates a name with a particular kind of value, a "type".   Optionally, that name is also initialized to a particular value.  

int x; -- declares a variable named x that represents an integer value, initialized to zero.

int y = 5;  -- declares a variable named y that represents an integer value, initialized to 5.

double z = 3.1415926;  -- declares a variable named z that represents a floating point value (i.e. not necessarily an integer).  This "double precision" value has about 13 decimal places of accuracy.   Here, the variable is initialized to the value 3.1415926. 

Arithmetic operations:

Java supports the usual arithmetic operations:  add (+), subtract (-), multiply (*) and divide (/).  Parentheses have their usual meaning.   For example:

3 + 7

x * 23

1.4/z

(3 + 5)/2

(8 - x)/(z * y)

There is no single symbol representation for exponentiation.

Exponentiation, square root, logarithm, cosine, tangent, etc. are represented as functions ("methods") of a class called Math.  To use these methods, see the following examples:

Math.pow(x, y)  returns x to the y'th power.

Math.sqrt(x) returns the square root of x.

Math.log(z)  returns the natural logarithm of z.

Math.cos(theta)  returns the sine of theta where theta is in radians.

Assignment

In Java, the statement

x = y;

does not mean the same thing as in mathematics.

In math, the above statement means that x and y each have a value and that those values are logically equal.

In Java, however, this statement means that the value of y is assigned to be the value of x, replacing whatever previous value x might have had with the current value of y.

Thus if we look at a statement such as

 x = (3 * 5) - 8;

this means that we first calculate the value of the right hand side,  which is 7, and assign that value to be the value of x from that point onwards.

Suppose, the next statement is

x = x*x + 1;

What do you think that the value of x will be in the end?  

If you said, "50", why?

Try this out in the Interactions pane in DrJava  (leave off the semicolons so that DrJava will print out the values for you).

 

Exercises

In the Interactions Pane of DrJava, try the following (leave off the semicolons so you can see what you are doing).  If you make a mistake in declaring a variable, either manually reset the Interactions pane or simply try again using a different variable name.

  1. Do some simple arithmetic operations with numbers to prove to yourself that Java does know how to do (simple) math.
  2. Try using a couple of the functions in Math.   See the API documentation to find more methods to use.  In the API documentation, scroll the lower left pane to find "Math".   Ignore most of the stuff--just look for the name of the method you want and how many inputs, separated by commas, it takes.
  3. Declare a variable dVal1 of type double and assign it a value.
  4. Assign dVal1 a value that is a the result of an arithmetic calculation.
  5. Create another variable dVal2 of type double.  
  6. Prove that you can assign the value of one variable to the other.
  7. Create a variable called intVal of type int.
  8. Play around and see what happens if you try to assign various integer and floating point values to this variable.
  9. Make a variable called strVal of type String.    Initialize it to the value "OOP is great!", including the double quotes.  
  10. Try assigning strVal to a new String value (anything inside of double quotes).
  11. What happens if you try to assign the value of strVal to/from one of your other variables or from an arithmetic expression?
  12. Try typing the following in (including the double quote marks and spaces): 
    "This" + " is called" + " string concatenation."
  13.  Does "-", "*" or "/" do anything?   Why do you suppose?
  14. Can you "add" a String and a String variable?  e.g.   "This is a String: " + strVal
  15. Can you "add" a String and a double?    Try it with explicit numbers as well as with variables.
  16. What about Strings and ints?
  17. Can you tell if the result of concatenating Strings and numbers is a String or not?   One way to test is if you can assign the result to a variable of type String.    Try it and see if you can come to a conclusion about the resultant type of concatenatating Strings and doubles and/or ints.
  18. Sometimes, during  the running of a program, we need to get a String to print out to the screen (or the "console" or "standard out" in CS lingo).    We can use a method called System.out.println to do this.    Try typing the following:
    System.out.println("How now brown cow?");
    Even if the terminating semicolon is included, the string will appear in green the Interactions pane.   If you click the "Console" tab, you will see the output from System.out.println all by itself.
  19. Try using System.out.println with various Strings, ints, doubles, and ints as well as with concatenated results.
  20. What is the type of the return value of  System.out.println Don't jump to conclusions!  Figure out a way to test your hypothesis.    Can you explain what is going on?

 


Glossary

Definitions pane: The pane at the upper right of the DrJava window where one edits class definitions.

Interactions pane: The pane at the lower edge of the DrJava window where one can interactively execute Java statements.

Unit Test: The testing of a single class or small collection of classes (a "unit") to verify correct behavior at a fine-grained level.

 


Last Revised Thursday, 03-Jun-2010 09:50:13 CDT

©2006 Stephen Wong and Dung Nguyen