Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS5015 Object-oriented Software Development Tutorial 3: DrJava Interactions Adrian O’Riordan, 2010, revised 201, 2012, 2014 The notes on DrJava are derived from the online DrJava documentation. Note that there is no need to submit this work. One of the distinguishing features of DrJava is its Interactions pane, which supports the execution of Java programs as well as any valid Java statements or expressions by means of a source code interpreter. Until now we have compiled Java source code into bytecode (the .class files) and it is this we execute using the Java runtime (e.g. by using JDK java command). When you press the Run button in the DrJava toolbar, this results in a call to the main method of the designated “main class”. Another way of running your program is the "Run Document's Main Method" command in the Tools menu or the context menu of the file name. You enter console input in the Interactions pane, wherein you type what you want the System.in stream to read. This entered text will be in purple by default. Any console output is displayed in the Console tab. DrJava includes a Java source code interpreter accessible from the Interactions pane. This interpreter executes the source code directly. It enables you to evaluate Java expressions and run fragments of Java code without compiling, useful for learning Java and for testing fragments of code quickly. It is based a Java interpreter called DynamicJava. 1 The purpose of this tutorial is to demonstrate interaction with the source-code interpreter. DrJava displays a prompt, the > character. DrJava will attempt to evaluate the Java code as you enter; enter a valid Java expression, DrJava will display a response with the value of the expression. If the code you type is invalid, DrJava will display an appropriate error message. You can reset the Interactions pane if a method call hangs and does not return (“Reset Interactions” command from the Tools menu). Resetting removes any variables from scope and terminates any methods running in the Interactions pane. Enter some simple expressions: >1 >"hello" >3 + 6 >true && (2 < 1) Enter some statements (semicolons at the end of statements can be omitted): >System.out.print("hello") >if (2 > 1) System.out.print("It's greater!") As in many Unix shells, pressing the up arrow gives you previous commands you typed and the down arrow cycles through the command history in the opposite direction. You can declare a variable, assign it a value, and check its value: > int i > i = 5; >i 1 The source code interpreter is implemented within DrJava as a separate JVM (from your program) but this usually need not concern the user since DrJava handles all communication between the two JVMs. The interpreter in DrJava supports Java 7. The interpreter extends the Java grammar to accept many scripting features but we will not explore these here; an example is inline comments beginning with '#'. Page 1 of 3 Here is an example with a String. Note how objects will be evaluated and methods called: > String s = "A new string" > s > s.length() >"hello".length() You can import classes from the Java standard library. Here we import JFrame to create a GUI container and display it: > > > > import javax.swing.JFrame JFrame f = new JFrame("My JFrame") f.setSize(500,500) f.show() One common use of the Interactions pane is to test a line of code before it adding it to the program. To conveniently copy a working line from the Interactions pane into a .java file in the Definitions pane use the "Lift Current Interaction to Definitions" command in the Tools menu (or from context menu in Interactions pane), which copies the text at the current prompt and pastes it at the cursor position in the Definitions pane. Class Interactions You can also interact with classes using the Interactions pane. These could be classes defined in the Definitions pane, classes within a JAR file that have been including in the classpath or even classes you define in the Interactions pane. You can define a new class in the Interactions pane (but it is usually easier in the Definitions pane). For example: >class SimpleClass { int aField; void doIt(){ System.out.println("Doing"); } } Next, declare a reference, create an object, call a method, and display a field value: > SimpleClass s >s = new SimpleClass() >s.doIt() >s.aField Any class open in the Definitions pane is added to the classpath; additional classes and directories can be added using the "Extra Classpath" configuration option (Edit → Preferences → Resource Locations). View the current classpath of the Interactions pane is by selecting "View Interactions Classpath" from the context menu of Interactions pane. The following example will use a class called Die representing a gaming cube. Load the file Die.java into the main Definitions pane and compile it. Here we create a Die object with a reference d, display it as a String, call the roll method, and display the object again: >Die d = new Die() >d >d.roll() >d Create a second Die object and experiment with the getFaceValue() and setFaceValue() methods, and assigning one reference to the other. Page 2 of 3 Note that if you are working with classes that are in a package, it is necessary to make the same package declaration in the Interactions Pane.2 For example if your code defines a package called cartoon you need declare this first to interact with the code >package cartoon Interactions History Files History files can spare you from retyping statements in the Interactions pane. History files are humanreadable files that contain code for running in the Interactions pane that can be edited with any text editor. Download the file sampleinteraction.hist. Load the script into DrJava by choosing Tools → History → Load Interactions History as Script from the menu. To execute the code one statement at a time, use the Next and Execute buttons. You can skip a statement without executing it (Next, Next), repeat it (Prev, Execute), or edit it (after Next, before Execute).You can intersperse new statements with the scripted ones (by typing the new ones in the Interactions panel). To execute all the script's statements in one go us Tools → History → Execute Interactions History. In order to create your own history file choose Tools → History → Save Interactions History as Script. You will be given the option to edit the statements before saving them. A handy option is Tools → Clear Interactions History. Auto-Import Class When you use a class name in the Interactions pane not known, probably because the class has not been imported, DrJava displays an error: > File f Static Error: Undefined class 'File' DrJava will open a predictive input dialog populated with all Java API classes. The initial suggestion is a class that matches the unknown class as closely as possible. After choosing the desired class, DrJava will import that class and re-execute the line that caused the error. In the predictive input dialog, there is also a checkbox that allows you to specify the package that contains the class, e.g. java.io.* instead of just java.io.File. > import java.io.*; // auto-import There is also a default Imports facility (Edit → Preferences → Interactions Pane), where the user can add and remove classes and packages for import in the Interactions Pane. After the Interactions Pane is reset, these classes are immediately available. end. 2 There are various differences between the behaviour of the source code interpreter and standard Java (e.g. JDK). For instance, import and package statements can be interleaved with other declarations and statements. Variables (and other declarations) can be shadowed by re-declarations, and a variable can be declared without an explicit type; the type is inferred. See the DrJava documentation for more information on these. Page 3 of 3