Comp201: Principles of Object-Oriented Programming I
Spring 2008 -- 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.   For this class, you should download the very latest version, to be found at http://www.cs.rice.edu/~javaplt/drjavarice ,

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 (click on the link for "Java SE")   Be sure to download and install the "JDK" (Java Development Kit) version without NetBeans.   Do not install just the "JRE" (Java Runtime Engine).

 

We will be using the DrJava tutorial on Connexions: http://cnx.org/content/m11659/latest/

 


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:27 CDT

©2008 Stephen Wong and Dung Nguyen