|
Comp201: Principles of Object-Oriented Programming I
|
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).
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".
For a more complete reference, see the Java Resources site.
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.
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.
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).
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.
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