![]()
Interaction Module
The interactions module is primarily aimed at beginners in a supervised learning environment.
Using the Interaction Module
The Interaction Module allows you to run simple Java statements. It is a useful way for beginners to get accustomed to programming.
You may use 'print x' as a sortcut for System.out.println(x)
For example, the following interaction defines a String and then prints it out:
String s="hello";
print s;
> hello
(You may write both the first two lines before clicking on the 'Enter' button)
It is possible to simply write:
s
which will be interpreted as print s but in cases such as s.length(), use 'print' to see the value:
print s.length();
> 5
You can enter import statements if required, but most basic packages are automatically imported, for convenience (awt, swing, util, net, io).
You can enter more than one line at the same time. For example:
JFrame f=new JFrame("MyFrame");
f.setBounds(0,200,100,100);
f.setVisible(true);
for (int x=0; x<600; x++) {
f.setLocation(x, 200);
}
But include only one print statement in a multi-line step (if required).
You can save an identifier for a class and call methods like this:
A a=new A();
a.go();
If method parameters are required, you can provide literals if only primitives or Strings are required, as in the following examples:
a.setName("Fred");
a.setAge(21);
or you can create objects for use as parameters:
Vector v=new Vector();
print v;
> []
v.addElement("Golf");
v.addElement("Swimming");
a.setInterests(v);
print a.getInterests();
> [Golf, Swimming]
You can use the Interaction Module to interact with your own classes too. Consider the following class:
package projects.console1;import java.io.*;public class Console1 {public static BufferedReader keyboardInput;
public Console1() {writeLine("What's your name ?");String s=readLine();writeLine("Hello "+s+" !");} // end constructor
public static String readLine() {String s="";try {s=keyboardInput.readLine();} catch (Exception ex) {}return s;} // end readLine
public static void main(String[] args) {new Console1();} // end main} //end class
If this class has been compiled, you can create a new instance of it with the following statement:
new projects.console1.Console1();
This will cause the statements in the constructor to be run. When you wish to enter your name, as requested by the program, use the 'Program Input' textfield and adjacent 'Enter' button.
A sequence of entered statements can be saved as a file and later loaded. After loading a sequence, you can choose to step through the statements or simply choose 'run' to process them all in one go. To step through a sequence a second time, reload the sequence file.
The ability to save and load sequences provides a way to achieve basic unit testing.
If you create any GUI objects, you can use the 'Stop' button to halt running the code and return to the Interactions Module.